Skip to content

Commit

Permalink
Setup op feature
Browse files Browse the repository at this point in the history
  • Loading branch information
yanganto committed Aug 12, 2024
1 parent e5cadca commit 7a6a345
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 27 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ 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 --no-default-features --example op
nix develop -c cargo test --no-default-features
- name: Test with default features
run: |
nix develop -c cargo run --example status
nix develop -c cargo run --example op
nix develop -c cargo test
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,7 @@ The [examples][examples] demo following scenarios.

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

- op (default): provide operators `<<` for instance and patch, and `+` for patches

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

[features]
status = []
add = []
op = []

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

#[cfg(feature = "op")]
let op_impl = quote! {
impl #generics core::ops::Shl<#name #generics> for #struct_name #generics #where_clause {
type Output = Self;
Expand Down Expand Up @@ -156,6 +157,8 @@ impl Patch {
}
}
};
#[cfg(not(feature = "op"))]
let op_impl = quote!();

let patch_impl = quote! {
impl #generics struct_patch::traits::Patch< #name #generics > for #struct_name #generics #where_clause {
Expand Down
6 changes: 3 additions & 3 deletions struct-patch/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ serde = { version = "1", features = ["derive"] }
serde_with = "3.9.0"

[features]
default = ["status"]
default = ["status", "op"]
status = [
"struct-patch-derive/status"
]
add = [
"struct-patch-derive/add"
op = [
"struct-patch-derive/op"
]

43 changes: 23 additions & 20 deletions struct-patch/examples/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,24 +35,27 @@ fn main() {
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 = new_item << the_other_patch;
assert_eq!(final_item.field_complete, true);
assert_eq!(final_item.field_int, 7);
assert_eq!(final_item.field_string, "from another patch");
#[cfg(feature = "op")]
{
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 = new_item << the_other_patch;
assert_eq!(final_item.field_complete, true);
assert_eq!(final_item.field_int, 7);
assert_eq!(final_item.field_string, "from another patch");
}
}
42 changes: 42 additions & 0 deletions struct-patch/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,4 +339,46 @@ mod tests {
}
);
}

#[cfg(feature = "op")]
#[test]
fn test_add_patches() {
#[derive(Patch)]
#[patch(attribute(derive(Debug, PartialEq)))]
struct Item {
field: u32,
other: String,
}

let patch = ItemPatch {
field: Some(1),
other: None,
};
let patch2 = ItemPatch {
field: None,
other: Some(String::from("hello")),
};
let overall_patch = patch + patch2;
assert_eq!(
overall_patch,
ItemPatch {
field: Some(1),
other: Some(String::from("hello")),
}
);
}

#[cfg(feature = "op")]
#[test]
#[should_panic]
fn test_add_conflict_patches_panic() {
#[derive(Patch, Debug, PartialEq)]
struct Item {
field: u32,
}

let patch = ItemPatch { field: Some(1) };
let patch2 = ItemPatch { field: Some(2) };
let _overall_patch = patch + patch2;
}
}

0 comments on commit 7a6a345

Please sign in to comment.