struct-patch 0.12.6

A library that helps you implement partial updates for your structs.
Documentation
use struct_patch::Patch;

#[derive(Default, Patch)]
#[patch(attribute(derive(Debug, Default)))]
struct Item {
    field_complete: bool,
    field_int: usize,
    field_string: String,
    #[patch(skip_wrap)]
    field_option_vec: Option<Vec<u32>>,
}

// 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>,
//     // `skip_wrap` keeps `Option<Vec<u32>>` as-is (no extra `Option` wrap).
//     field_option_vec: Option<Vec<u32>>,
// }

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, field_option_vec: None }"
    );

    item.apply(patch);

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

    // `skip_wrap`: `None` in the patch means "no change".
    let noop_patch = ItemPatch {
        field_complete: None,
        field_int: None,
        field_string: None,
        field_option_vec: None,
    };
    item.field_option_vec = Some(vec![1, 2, 3]);
    item.apply(noop_patch);
    assert_eq!(item.field_option_vec, Some(vec![1, 2, 3]));

    // `skip_wrap`: `Some(vec)` in the patch replaces the field with `Some(vec)`.
    let set_patch = ItemPatch {
        field_complete: None,
        field_int: None,
        field_string: None,
        field_option_vec: Some(vec![9, 9]),
    };
    item.apply(set_patch);
    assert_eq!(item.field_option_vec, Some(vec![9, 9]));

    // `skip_wrap`: `Some(vec![])` still applies and clears the vector.
    let clear_patch = ItemPatch {
        field_complete: None,
        field_int: None,
        field_string: None,
        field_option_vec: Some(vec![]),
    };
    item.apply(clear_patch);
    assert_eq!(item.field_option_vec, Some(vec![]));

    #[cfg(feature = "op")]
    {
        let another_patch = ItemPatch {
            field_complete: None,
            field_int: None,
            field_string: Some("from another patch".into()),
            field_option_vec: None,
        };
        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");
        assert_eq!(new_item.field_option_vec, Some(vec![]));

        let the_other_patch = ItemPatch {
            field_complete: Some(true),
            field_int: None,
            field_string: None,
            field_option_vec: Some(vec![4, 2]),
        };
        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");
        assert_eq!(final_item.field_option_vec, Some(vec![4, 2]));
    }
}