Skip to content

Commit

Permalink
Move Add to add feature
Browse files Browse the repository at this point in the history
  • Loading branch information
yanganto committed Aug 9, 2024
1 parent e379b8b commit ea1aa68
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 28 deletions.
1 change: 1 addition & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ jobs:
nix develop -c cargo run --no-default-features --example json
nix develop -c cargo run --no-default-features --example rename-patch-struct
nix develop -c cargo run --no-default-features --example patch-attr
nix develop -c cargo run --features=add --example add
nix develop -c cargo test --no-default-features
- name: Test with default features
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ The [examples][examples] demo following scenarios.
- check a patch is empty or not
- add attribute to patch struct

## Features
- status (default): provide `is_empty` method
- add (optional): provide `+` operator between instance and patch


[crates-badge]: https://img.shields.io/crates/v/struct-patch.svg
[crate-url]: https://crates.io/crates/struct-patch
Expand Down
1 change: 1 addition & 0 deletions struct-patch-derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ syn = { version = "2.0", features = ["parsing"] }

[features]
status = []
add = []

[dev-dependencies]
pretty_assertions_sorted = "1.2.3"
5 changes: 4 additions & 1 deletion struct-patch-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,10 @@ impl Patch {
#[cfg(not(feature = "status"))]
let patch_status_impl = quote!();

#[cfg(feature = "add")]
let add_impl = quote! {
impl core::ops::Add<#name #generics> for #struct_name #generics #where_clause {
type Output = #struct_name;
type Output = Self;

fn add(mut self, rhs: #name #generics) -> Self {
self.apply(rhs);
Expand All @@ -120,6 +121,8 @@ impl Patch {
}
}
};
#[cfg(not(feature = "add"))]
let add_impl = quote!();

let patch_impl = quote! {
impl #generics struct_patch::traits::Patch< #name #generics > for #struct_name #generics #where_clause {
Expand Down
3 changes: 3 additions & 0 deletions struct-patch/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,7 @@ default = ["status"]
status = [
"struct-patch-derive/status"
]
add = [
"struct-patch-derive/add"
]

31 changes: 4 additions & 27 deletions struct-patch/examples/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use struct_patch::Patch;
#[derive(Default, Patch)]
#[patch(attribute(derive(Debug, Default)))]
struct Item {
field_complete: bool,
field_bool: bool,
field_int: usize,
field_string: String,
}
Expand All @@ -12,7 +12,7 @@ struct Item {
//
// #[derive(Debug, Default)] // pass by patch(attribute(...))
// struct ItemPatch {
// field_complete: Option<bool>,
// field_bool: Option<bool>,
// field_int: Option<usize>,
// field_string: Option<String>,
// }
Expand All @@ -26,35 +26,12 @@ fn main() {

assert_eq!(
format!("{patch:?}"),
"ItemPatch { field_complete: None, field_int: Some(7), field_string: None }"
"ItemPatch { field_bool: None, field_int: Some(7), field_string: None }"
);

item.apply(patch);

assert_eq!(item.field_complete, false);
assert_eq!(item.field_bool, false);
assert_eq!(item.field_int, 7);
assert_eq!(item.field_string, "");

let another_patch = ItemPatch {
field_complete: None,
field_int: None,
field_string: Some("from another patch".into()),
};
let new_item = item + another_patch;

assert_eq!(new_item.field_complete, false);
assert_eq!(new_item.field_int, 7);
assert_eq!(new_item.field_string, "from another patch");

let the_other_patch = ItemPatch {
field_complete: Some(true),
field_int: None,
field_string: None,
};
let final_item = the_other_patch + new_item;
assert_eq!(final_item.field_complete, true);
assert_eq!(final_item.field_int, 7);
assert_eq!(final_item.field_string, "from another patch");

println!("instance example run passed")
}

0 comments on commit ea1aa68

Please sign in to comment.