|
6 | 6 | A lib help you patch Rust instance, and easy to partial update configures.
|
7 | 7 |
|
8 | 8 | ## Introduction
|
9 |
| -This crate provides the `Patch` trait and an accompanying derive macro. |
| 9 | +This crate provides the `Patch`, `Filler` traits and accompanying derive macro. |
| 10 | +If the any field in `Patch` is some then it will overwrite the field of instance when apply. |
| 11 | +If the any field in the instance is none then it will try to fill the field with the `Filler`. |
| 12 | +Currently, `Filler` only support `Option` field, and the `Vec` and other field will implement later. |
| 13 | +The detail discussion is in [issue #81](https://github.com/yanganto/struct-patch/issues/81) |
10 | 14 |
|
| 15 | +## Quick Example |
11 | 16 | Deriving `Patch` on a struct will generate a struct similar to the original one, but with all fields wrapped in an `Option`.
|
12 | 17 | 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.
|
13 |
| - |
14 |
| -## Quick Example |
15 | 18 | ```rust
|
16 | 19 | use struct_patch::Patch;
|
17 | 20 | use serde::{Deserialize, Serialize};
|
@@ -61,6 +64,32 @@ fn patch_json() {
|
61 | 64 | }
|
62 | 65 | ```
|
63 | 66 |
|
| 67 | +Deriving `Filler` on a struct will generate a struct similar to the original one with the field with `Option`. Unlike `Patch`, the `Filler` only work on the empty fields of instance. |
| 68 | + |
| 69 | +```rust |
| 70 | +use struct_patch::Filler; |
| 71 | + |
| 72 | +#[derive(Filler)] |
| 73 | +struct Item { |
| 74 | + field_int: usize, |
| 75 | + maybe_field_int: Option<usize>, |
| 76 | +} |
| 77 | +let mut item = Item { |
| 78 | + field_int: 0, |
| 79 | + maybe_field_int: None, |
| 80 | +}; |
| 81 | + |
| 82 | +let filler_1 = ItemFiller{ maybe_field_int: Some(7), }; |
| 83 | +item.apply(filler_1); |
| 84 | +assert_eq!(item.maybe_field_int, Some(7)); |
| 85 | + |
| 86 | +let filler_2 = ItemFiller{ maybe_field_int: Some(100), }; |
| 87 | + |
| 88 | +// The field is not empty, so the filler has not effect. |
| 89 | +item.apply(filler_2); |
| 90 | +assert_eq!(item.maybe_field_int, Some(7)); |
| 91 | +``` |
| 92 | + |
64 | 93 | ## Documentation and Examples
|
65 | 94 | Also, you can modify the patch structure by defining `#[patch(...)]` attributes on the original struct or fields.
|
66 | 95 |
|
@@ -89,7 +118,7 @@ The [examples][examples] demo following scenarios.
|
89 | 118 |
|
90 | 119 | ## Features
|
91 | 120 | This crate also includes the following optional features:
|
92 |
| -- `status`(default): implements the `PatchStatus` trait for the patch struct, which provides the `is_empty` method. |
| 121 | +- `status`(default): implements the `Status` trait for the patch struct, which provides the `is_empty` method. |
93 | 122 | - `op` (default): provide operators `<<` between instance and patch, and `+` for patches
|
94 | 123 | - default: when there is a field conflict between patches, `+` will add together if the `#[patch(addable)]` or `#[patch(add=fn)]` is provided, else it will panic.
|
95 | 124 | - `merge` (optional): implements the `Merge` trait for the patch struct, which provides the `merge` method, and `<<` between patches.
|
|
0 commit comments