Skip to content

Commit 0b57358

Browse files
committed
Fix bug in SerdeDerive macro implementation
1 parent e461da4 commit 0b57358

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed

serde-diff-derive/src/serde_diff/mod.rs

+3
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,9 @@ fn generate(
332332
match (self, other) {
333333
#(#diff_match_arms)*
334334
}
335+
if (__changed__) {
336+
ctx.save_exit()?;
337+
}
335338
Ok(__changed__)
336339
}
337340
}

src/difference.rs

+5
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,11 @@ impl<'a, S: SerializeSeq> DiffContext<'a, S> {
148148
self.save_command(&DiffCommandRef::Value(value), true, true)
149149
}
150150

151+
/// Write exit command
152+
pub fn save_exit(&mut self) -> Result<(), S::Error> {
153+
self.save_command::<()>(&DiffCommandRef::Exit, true, false)
154+
}
155+
151156
/// Stores an arbitrary DiffCommand to be handled by the type.
152157
/// Any custom sequence of DiffCommands must be followed by Exit.
153158
pub fn save_command<'b, T: Serialize>(

src/tests.rs

+17
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
use crate as serde_diff;
22
use crate::{Apply, Diff, SerdeDiff};
33
use serde::{Deserialize, Serialize};
4+
use std::collections::HashMap;
45
use std::fmt::Debug;
6+
use std::iter::FromIterator;
57

68
#[derive(SerdeDiff, Serialize, Deserialize, PartialEq, Debug, Copy, Clone)]
79
struct TestStruct {
@@ -15,6 +17,9 @@ fn roundtrip<T: SerdeDiff + Serialize + for<'a> Deserialize<'a> + PartialEq + De
1517
) {
1618
let diff = Diff::serializable(&old, &new);
1719
let json_diff = serde_json::to_string(&diff).unwrap();
20+
21+
println!("{}", json_diff);
22+
1823
let mut deserializer = serde_json::Deserializer::from_str(&json_diff);
1924
let mut target = old.clone();
2025
Apply::apply(&mut deserializer, &mut target).unwrap();
@@ -59,6 +64,18 @@ fn test_option() {
5964
Some(TestStruct { a: 52, b: 32. }),
6065
Some(TestStruct { a: 42, b: 12. }),
6166
);
67+
roundtrip(
68+
HashMap::from_iter([
69+
(1, TestStruct { a: 1, b: 1. }),
70+
(2, TestStruct { a: 2, b: 2. }),
71+
(3, TestStruct { a: 3, b: 3. }),
72+
]),
73+
HashMap::from_iter([
74+
(1, TestStruct { a: 1, b: 1. }),
75+
(3, TestStruct { a: 4, b: 4. }),
76+
(4, TestStruct { a: 1, b: 1. }),
77+
]),
78+
);
6279

6380
partial(
6481
Some(TestStruct { a: 5, b: 2. }),

0 commit comments

Comments
 (0)