Skip to content

Commit f5c05cf

Browse files
committed
Make sure to consume "Exit" command when applying an enum variant diff.
1 parent 8bb8707 commit f5c05cf

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

examples/vec.rs

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
use serde::{Deserialize, Serialize};
2+
use serde_diff::{Apply, Diff, SerdeDiff};
3+
4+
#[derive(SerdeDiff, Serialize, Deserialize, Debug, PartialEq, Clone)]
5+
enum Value {
6+
Str(String),
7+
Int(i32),
8+
}
9+
10+
impl From<&str> for Value {
11+
fn from(other: &str) -> Self {
12+
Self::Str(other.into())
13+
}
14+
}
15+
16+
#[derive(SerdeDiff, Serialize, Deserialize, Debug, Default, PartialEq, Clone)]
17+
struct TestStruct {
18+
test: bool,
19+
//#[serde_diff(opaque)]
20+
list: Vec<Value>,
21+
}
22+
23+
fn main() -> Result<(), Box<dyn std::error::Error>> {
24+
let mut empty = TestStruct::default();
25+
empty.test = true;
26+
27+
let mut a = TestStruct::default();
28+
a.list.push("a".into());
29+
30+
let mut abc = TestStruct::default();
31+
abc.list.push("a".into());
32+
abc.list.push("b".into());
33+
abc.list.push("c".into());
34+
35+
let mut cba = TestStruct::default();
36+
cba.list.push("c".into());
37+
cba.list.push("b".into());
38+
cba.list.push("a".into());
39+
40+
let mut c = TestStruct::default();
41+
c.list.push("c".into());
42+
43+
let add_a = serde_json::to_string(&Diff::serializable(&empty, &a))?;
44+
let add_b = serde_json::to_string(&Diff::serializable(&a, &abc))?;
45+
let del_a = serde_json::to_string(&Diff::serializable(&abc, &cba))?;
46+
let rep_b_c = serde_json::to_string(&Diff::serializable(&cba, &c))?;
47+
let no_change = serde_json::to_string(&Diff::serializable(&c, &c))?;
48+
49+
let mut built = TestStruct::default();
50+
for (diff, after) in &[
51+
(add_a, a),
52+
(add_b, abc),
53+
(del_a, cba),
54+
(rep_b_c, c.clone()),
55+
(no_change, c),
56+
] {
57+
println!("{}", diff);
58+
59+
let mut deserializer = serde_json::Deserializer::from_str(&diff);
60+
Apply::apply(&mut deserializer, &mut built)?;
61+
62+
assert_eq!(after, &built);
63+
}
64+
Ok(())
65+
}

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

+2
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,8 @@ fn generate(
386386
#(#apply_match_arms)*
387387
_ => ctx.skip_value(seq)?,
388388
}
389+
// Consume the extra Exit command generated at the end of enums.
390+
ctx.next_path_element(seq)?;
389391
Ok(__changed__)
390392
}
391393
}

0 commit comments

Comments
 (0)