From 19fcb794eb6ac1a3f78cc0887a2501c02f11ea21 Mon Sep 17 00:00:00 2001 From: Antonio Yang Date: Mon, 12 Aug 2024 15:56:32 +0800 Subject: [PATCH] Setup `op` feature --- .github/workflows/test.yml | 2 +- README.md | 3 +-- struct-patch-derive/Cargo.toml | 2 +- struct-patch-derive/src/lib.rs | 3 +++ struct-patch/Cargo.toml | 6 ++--- struct-patch/examples/instance.rs | 43 ++++++++++++++++-------------- struct-patch/examples/op.rs | 4 +++ struct-patch/src/lib.rs | 44 +++++++++++++++++++++++++++++++ 8 files changed, 80 insertions(+), 27 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 8a2e91a..aa283ea 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -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 diff --git a/README.md b/README.md index 7343664..63eff34 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/struct-patch-derive/Cargo.toml b/struct-patch-derive/Cargo.toml index 253de0f..5a65c7e 100644 --- a/struct-patch-derive/Cargo.toml +++ b/struct-patch-derive/Cargo.toml @@ -20,7 +20,7 @@ syn = { version = "2.0", features = ["parsing"] } [features] status = [] -add = [] +op = [] [dev-dependencies] pretty_assertions_sorted = "1.2.3" diff --git a/struct-patch-derive/src/lib.rs b/struct-patch-derive/src/lib.rs index 66e5f4b..a2e2111 100644 --- a/struct-patch-derive/src/lib.rs +++ b/struct-patch-derive/src/lib.rs @@ -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; @@ -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 { diff --git a/struct-patch/Cargo.toml b/struct-patch/Cargo.toml index 0ab2302..8424da7 100644 --- a/struct-patch/Cargo.toml +++ b/struct-patch/Cargo.toml @@ -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" ] diff --git a/struct-patch/examples/instance.rs b/struct-patch/examples/instance.rs index e417915..7835a64 100644 --- a/struct-patch/examples/instance.rs +++ b/struct-patch/examples/instance.rs @@ -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"); + } } diff --git a/struct-patch/examples/op.rs b/struct-patch/examples/op.rs index aec846b..ade9449 100644 --- a/struct-patch/examples/op.rs +++ b/struct-patch/examples/op.rs @@ -8,6 +8,7 @@ struct Item { field_string: String, } +#[cfg(feature = "op")] fn main() { let mut item = Item::default(); @@ -55,3 +56,6 @@ fn main() { let final_item_series_patch = item << another_patch << the_other_patch; assert_eq!(final_item_series_patch.field_int, 2); } + +#[cfg(not(feature = "op"))] +fn main() {} diff --git a/struct-patch/src/lib.rs b/struct-patch/src/lib.rs index 99ff4a0..0151ada 100644 --- a/struct-patch/src/lib.rs +++ b/struct-patch/src/lib.rs @@ -281,6 +281,7 @@ mod tests { ); } + #[cfg(feature = "op")] #[test] fn test_shl() { #[derive(Patch, Debug, PartialEq)] @@ -307,6 +308,7 @@ mod tests { ); } + #[cfg(feature = "op")] #[test] fn test_shl_on_patch() { #[derive(Patch, Debug, PartialEq)] @@ -339,4 +341,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; + } }