-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathvec.rs
More file actions
65 lines (53 loc) · 1.73 KB
/
vec.rs
File metadata and controls
65 lines (53 loc) · 1.73 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
use serde::{Deserialize, Serialize};
use serde_diff::{Apply, Diff, SerdeDiff};
#[derive(SerdeDiff, Serialize, Deserialize, Debug, PartialEq, Clone)]
enum Value {
Str(String),
Int(i32),
}
impl From<&str> for Value {
fn from(other: &str) -> Self {
Self::Str(other.into())
}
}
#[derive(SerdeDiff, Serialize, Deserialize, Debug, Default, PartialEq, Clone)]
struct TestStruct {
test: bool,
//#[serde_diff(opaque)]
list: Vec<Value>,
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut empty = TestStruct::default();
empty.test = true;
let mut a = TestStruct::default();
a.list.push("a".into());
let mut abc = TestStruct::default();
abc.list.push("a".into());
abc.list.push("b".into());
abc.list.push("c".into());
let mut cba = TestStruct::default();
cba.list.push("c".into());
cba.list.push("b".into());
cba.list.push("a".into());
let mut c = TestStruct::default();
c.list.push("c".into());
let add_a = serde_json::to_string(&Diff::serializable(&empty, &a))?;
let add_b = serde_json::to_string(&Diff::serializable(&a, &abc))?;
let del_a = serde_json::to_string(&Diff::serializable(&abc, &cba))?;
let rep_b_c = serde_json::to_string(&Diff::serializable(&cba, &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, abc),
(del_a, cba),
(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(())
}