Expand description
This crate provides the Patch
and Filler
traits and accompanying derive macro.
Deriving Patch
on a struct will generate a struct similar to the original one, but with all fields wrapped in an Option
.
An instance of such a patch struct can be applied onto the original struct, replacing values only if they are set to Some
, leaving them unchanged otherwise.
The following code shows how struct-patch
can be used together with serde
to patch structs with JSON objects.
use struct_patch::Patch;
use serde::{Deserialize, Serialize};
#[derive(Default, Debug, PartialEq, Patch)]
#[patch(attribute(derive(Debug, Default, Deserialize, Serialize)))]
struct Item {
field_bool: bool,
field_int: usize,
field_string: String,
}
fn patch_json() {
let mut item = Item {
field_bool: true,
field_int: 42,
field_string: String::from("hello"),
};
let data = r#"{
"field_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")
}
);
}
More details on how to use the the derive macro, including what attributes are available, are
available under Patch
Deriving Filler
on a struct will generate a struct similar to the original one with the
field with Option
, BTreeMap
, BTreeSet
, BinaryHeap
,HashMap
, HashSet
, LinkedList
,
VecDeque
or Vec
.
Any struct implement Default
, Extend
, IntoIterator
, is_empty
can be used with
#[filler(extenable)]
.
Unlike Patch
, the Filler
only work on the empty fields of instance.
use struct_patch::Filler;
#[derive(Filler)]
struct Item {
field_int: usize,
maybe_field_int: Option<usize>,
}
let mut item = Item {
field_int: 0,
maybe_field_int: None,
};
let filler_1 = ItemFiller{ maybe_field_int: Some(7), };
item.apply(filler_1);
assert_eq!(item.maybe_field_int, Some(7));
let filler_2 = ItemFiller{ maybe_field_int: Some(100), };
item.apply(filler_2);
assert_eq!(item.maybe_field_int, Some(7));
Re-exports§
pub use traits::*;