A lib help you patch Rust instance, and easy to partial update configures.
This crate provides the Patch
trait and an 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.
```rust use struct_patch::Patch; use serde::{Deserialize, Serialize};
struct Item { fieldbool: bool, fieldint: usize, field_string: String, }
fn patchjson() { let mut item = Item { fieldbool: true, fieldint: 42, fieldstring: 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")
}
);
} ```