-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathset.rs
More file actions
51 lines (41 loc) · 1.47 KB
/
set.rs
File metadata and controls
51 lines (41 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
use serde::{Deserialize, Serialize};
use serde_diff::{Apply, Diff, SerdeDiff};
use std::collections::HashSet;
#[derive(SerdeDiff, Serialize, Deserialize, Debug, Default, PartialEq, Clone)]
struct TestStruct {
test: bool,
//#[serde_diff(opaque)]
set: HashSet<String>,
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut empty = TestStruct::default();
empty.test = true;
let mut a = TestStruct::default();
a.set.insert("a".to_string());
let mut ab = TestStruct::default();
ab.set.insert("a".to_string());
ab.set.insert("b".to_string());
let mut b = TestStruct::default();
b.set.insert("b".to_string());
let mut c = TestStruct::default();
c.set.insert("c".to_string());
let add_a = serde_json::to_string(&Diff::serializable(&empty, &a))?;
let add_b = serde_json::to_string(&Diff::serializable(&a, &ab))?;
let del_a = serde_json::to_string(&Diff::serializable(&ab, &b))?;
let rep_b_c = serde_json::to_string(&Diff::serializable(&b, &c))?;
let no_change = serde_json::to_string(&Diff::serializable(&c, &c))?;
let mut built = TestStruct::default();
for (diff, after) in &[
(add_a, a),
(add_b, ab),
(del_a, b),
(rep_b_c, c.clone()),
(no_change, c),
] {
println!("{}", diff);
let mut deserializer = serde_json::Deserializer::from_str(&diff);
Apply::apply(&mut deserializer, &mut built)?;
assert_eq!(after, &built);
}
Ok(())
}