Skip to content

Commit 5b175ae

Browse files
committed
add Filler for Option fields
1 parent ff3270e commit 5b175ae

File tree

12 files changed

+1115
-618
lines changed

12 files changed

+1115
-618
lines changed

.github/workflows/test.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ jobs:
2020
- name: Test no default features
2121
run: |
2222
nix develop -c cargo run --no-default-features --example instance
23+
nix develop -c cargo run --no-default-features --example filler
2324
nix develop -c cargo run --no-default-features --example diff
2425
nix develop -c cargo run --no-default-features --example json
2526
nix develop -c cargo run --no-default-features --example rename-patch-struct
@@ -30,6 +31,7 @@ jobs:
3031
- name: Test with std features
3132
run: |
3233
nix develop -c cargo run --features=std --example instance
34+
nix develop -c cargo run --features=std --example filler
3335
nix develop -c cargo run --features=std --example diff
3436
nix develop -c cargo run --features=std --example json
3537
nix develop -c cargo run --features=std --example rename-patch-struct

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ members = [
77

88
[workspace.package]
99
authors = ["Antonio Yang <[email protected]>"]
10-
version = "0.8.7"
10+
version = "0.9.0"
1111
edition = "2021"
1212
categories = ["development-tools"]
1313
keywords = ["struct", "patch", "macro", "derive", "overlay"]

README.md

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,15 @@
66
A lib help you patch Rust instance, and easy to partial update configures.
77

88
## 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)
1014

15+
## Quick Example
1116
Deriving `Patch` on a struct will generate a struct similar to the original one, but with all fields wrapped in an `Option`.
1217
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
1518
```rust
1619
use struct_patch::Patch;
1720
use serde::{Deserialize, Serialize};
@@ -61,6 +64,32 @@ fn patch_json() {
6164
}
6265
```
6366

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+
6493
## Documentation and Examples
6594
Also, you can modify the patch structure by defining `#[patch(...)]` attributes on the original struct or fields.
6695

@@ -89,7 +118,7 @@ The [examples][examples] demo following scenarios.
89118

90119
## Features
91120
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.
93122
- `op` (default): provide operators `<<` between instance and patch, and `+` for patches
94123
- 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.
95124
- `merge` (optional): implements the `Merge` trait for the patch struct, which provides the `merge` method, and `<<` between patches.

flake.lock

Lines changed: 9 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)