-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathjson.rs
More file actions
47 lines (40 loc) · 1000 Bytes
/
json.rs
File metadata and controls
47 lines (40 loc) · 1000 Bytes
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
use serde::Deserialize;
use struct_patch::Patch;
#[derive(Default, Debug, PartialEq, Patch)]
#[patch(attribute(derive(Debug, Default, Deserialize)))]
struct Item {
field_bool: bool,
field_int: usize,
field_string: String,
sub: SubItem,
}
#[derive(Default, Debug, PartialEq, Patch, Deserialize)]
#[patch(attribute(derive(Debug, Default, Deserialize)))]
struct SubItem {
inner_int: usize,
}
fn main() {
let mut item = Item {
field_bool: true,
field_int: 42,
field_string: String::from("hello"),
sub: SubItem { inner_int: 0 },
};
let data = r#"{
"field_int": 7,
"sub": {
"inner_int": 7
}
}"#;
let patch: ItemPatch = serde_json::from_str(data).unwrap();
item.apply(patch);
assert_eq!(
item,
Item {
field_bool: true,
field_int: 7,
field_string: String::from("hello"),
sub: SubItem { inner_int: 7 },
}
);
}