File: instance.rs

package info (click to toggle)
rust-struct-patch 0.9.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 188 kB
  • sloc: makefile: 2
file content (61 lines) | stat: -rw-r--r-- 1,612 bytes parent folder | download
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
use struct_patch::Patch;

#[derive(Default, Patch)]
#[patch(attribute(derive(Debug, Default)))]
struct Item {
    field_complete: bool,
    field_int: usize,
    field_string: String,
}

// Generated by Patch derive macro
//
// #[derive(Debug, Default)] // pass by patch(attribute(...))
// struct ItemPatch {
//     field_complete: Option<bool>,
//     field_int: Option<usize>,
//     field_string: Option<String>,
// }

fn main() {
    let mut item = Item::default();

    let mut patch: ItemPatch = Item::new_empty_patch();

    patch.field_int = Some(7);

    assert_eq!(
        format!("{patch:?}"),
        "ItemPatch { field_complete: None, field_int: Some(7), field_string: None }"
    );

    item.apply(patch);

    assert!(!item.field_complete);
    assert_eq!(item.field_int, 7);
    assert_eq!(item.field_string, "");

    #[cfg(feature = "op")]
    {
        let another_patch = ItemPatch {
            field_complete: None,
            field_int: None,
            field_string: Some("from another patch".into()),
        };
        let new_item = item << another_patch;

        assert!(!new_item.field_complete);
        assert_eq!(new_item.field_int, 7);
        assert_eq!(new_item.field_string, "from another patch");

        let the_other_patch = ItemPatch {
            field_complete: Some(true),
            field_int: None,
            field_string: None,
        };
        let final_item = new_item << the_other_patch;
        assert!(final_item.field_complete);
        assert_eq!(final_item.field_int, 7);
        assert_eq!(final_item.field_string, "from another patch");
    }
}