Skip to content

Commit

Permalink
Fix bug in SerdeDerive macro implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
PPakalns committed Sep 8, 2021
1 parent e461da4 commit f99cea0
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
3 changes: 3 additions & 0 deletions serde-diff-derive/src/serde_diff/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,9 @@ fn generate(
match (self, other) {
#(#diff_match_arms)*
}
if (__changed__) {
ctx.save_exit()?;
}
Ok(__changed__)
}
}
Expand Down
5 changes: 5 additions & 0 deletions src/difference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,11 @@ impl<'a, S: SerializeSeq> DiffContext<'a, S> {
self.save_command(&DiffCommandRef::Value(value), true, true)
}

/// Write exit command
pub fn save_exit(&mut self) -> Result<(), S::Error> {
self.save_command::<()>(&DiffCommandRef::Exit, true, false)
}

/// Stores an arbitrary DiffCommand to be handled by the type.
/// Any custom sequence of DiffCommands must be followed by Exit.
pub fn save_command<'b, T: Serialize>(
Expand Down
17 changes: 17 additions & 0 deletions src/tests.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use crate as serde_diff;
use crate::{Apply, Diff, SerdeDiff};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fmt::Debug;
use std::iter::FromIterator;

#[derive(SerdeDiff, Serialize, Deserialize, PartialEq, Debug, Copy, Clone)]
struct TestStruct {
Expand All @@ -15,6 +17,9 @@ fn roundtrip<T: SerdeDiff + Serialize + for<'a> Deserialize<'a> + PartialEq + De
) {
let diff = Diff::serializable(&old, &new);
let json_diff = serde_json::to_string(&diff).unwrap();

println!("{}", json_diff);

let mut deserializer = serde_json::Deserializer::from_str(&json_diff);
let mut target = old.clone();
Apply::apply(&mut deserializer, &mut target).unwrap();
Expand Down Expand Up @@ -59,6 +64,18 @@ fn test_option() {
Some(TestStruct { a: 52, b: 32. }),
Some(TestStruct { a: 42, b: 12. }),
);
roundtrip(
HashMap::from_iter([
(1, TestStruct { a: 1, b: 1. }),
(2, TestStruct { a: 2, b: 2. }),
(3, TestStruct { a: 3, b: 3. }),
]),
HashMap::from_iter([
(1, TestStruct { a: 1, b: 1. }),
(3, TestStruct { a: 4, b: 4. }),
(4, TestStruct { a: 1, b: 1. }),
]),
);

partial(
Some(TestStruct { a: 5, b: 2. }),
Expand Down

0 comments on commit f99cea0

Please sign in to comment.