Skip to content

Commit

Permalink
Remove default requirement (#7)
Browse files Browse the repository at this point in the history
- remove `Default` requirement
- `default_patch` -> `new_empty_patch`
  • Loading branch information
yanganto authored Jan 21, 2023
1 parent 61375fd commit 94dbf08
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 16 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ members = [

[workspace.package]
authors = ["Antonio Yang <[email protected]>"]
version = "0.1.5"
version = "0.2.0"
edition = "2021"
categories = ["Accessibility"]
keywords = ["struct", "patch", "macro", "derive", "overlay"]
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ Following are attributes you can easy to use patch a struct as you want
The [`struct_patch::traits::Patch`][patch-trait] will implement, you can check the docs for details.
- `apply`: apply the patch, only update the existing fields
- `into_patch_by_diff`: diff on a previous state and get the patch instance
- `default_patch`: get an empty patch instance
- `new_empty_patch`: get an empty patch instance


## Methods for patch structure
With `status` feature, the patch struct will implement [`PatchStatus`][patch-status-trait] trait and providing following methods:
Expand Down
15 changes: 12 additions & 3 deletions struct-patch-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use quote::quote;
/// ```rust
/// use struct_patch::traits::Patch;
/// let mut item = Item::default();
/// let mut patch = Item::default_patch();
/// let mut patch = Item::new_empty_patch();
/// assert(patch.is_empty());
///
/// patch.field_int = Some(7);
Expand Down Expand Up @@ -132,6 +132,8 @@ pub fn derive_patch(item: TokenStream) -> TokenStream {
#[cfg(feature = "status")]
let field_names_clone2 = field_names.clone();
let field_names_clone3 = field_names.clone();
let field_names_clone4 = field_names.clone();

let wrapped_types = wrapped_fields.iter().map(|(_, t)| t);

let mut output = if let Some(patch_derive) = patch_derive {
Expand All @@ -143,7 +145,6 @@ pub fn derive_patch(item: TokenStream) -> TokenStream {
)
} else {
quote::quote!(
#[derive(Default)]
pub struct #patch_struct_name {
#(pub #field_names: #wrapped_types,)*
}
Expand Down Expand Up @@ -178,14 +179,22 @@ pub fn derive_patch(item: TokenStream) -> TokenStream {
}

fn into_patch_by_diff(self, previous_struct: Self) -> #patch_struct_name {
let mut p = Self::default_patch();
let mut p = Self::new_empty_patch();
#(
if self.#field_names_clone3 != previous_struct.#field_names_clone3 {
p.#field_names_clone3 = Some(self.#field_names_clone3);
}
)*
p
}

fn new_empty_patch() -> #patch_struct_name {
#patch_struct_name {
#(
#field_names_clone4: None,
)*
}
}
}
)
.to_string();
Expand Down
6 changes: 2 additions & 4 deletions struct-patch-trait/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
/// Define the behavior between patch struct and original sturct
pub mod traits {
/// The trait can apply patch and generete corresponding patch instance
pub trait Patch<P: Default> {
pub trait Patch<P> {
/// Apply the patch, only update the existing fields
fn apply(&mut self, patch: P);

/// Diff on a previous state and into the patch instance
fn into_patch_by_diff(self, previous_struct: Self) -> P;

/// Get an empty patch instance
fn default_patch() -> P {
P::default()
}
fn new_empty_patch() -> P;
}

#[cfg(feature = "status")]
Expand Down
4 changes: 2 additions & 2 deletions struct-patch/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ readme.workspace = true


[dependencies]
struct-patch-trait = { version = "=0.1.5", path = "../struct-patch-trait" }
struct-patch-derive = { version = "=0.1.5", path = "../struct-patch-derive" }
struct-patch-trait = { version = "=0.2.0", path = "../struct-patch-trait" }
struct-patch-derive = { version = "=0.2.0", path = "../struct-patch-derive" }

[dev-dependencies]
serde_json = "1.0"
Expand Down
4 changes: 1 addition & 3 deletions struct-patch/examples/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ fn main() {

let mut item = Item::default();

let mut patch = Item::default_patch();
// the same as
// let mut patch = ItemPatch::default();
let mut patch = Item::new_empty_patch();

patch.field_int = Some(7);

Expand Down
2 changes: 1 addition & 1 deletion struct-patch/examples/rename-patch-struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ struct Item {
fn main() {
use struct_patch::traits::Patch;

let patch = Item::default_patch();
let patch = Item::new_empty_patch();

assert_eq!(
format!("{patch:?}"),
Expand Down
2 changes: 1 addition & 1 deletion struct-patch/examples/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ fn main() {
use struct_patch::traits::Patch;
use struct_patch::traits::PatchStatus;

let mut patch = Item::default_patch();
let mut patch = Item::new_empty_patch();

assert!(patch.is_empty()); // provided by PatchStatus
patch.field_int = Some(7);
Expand Down

0 comments on commit 94dbf08

Please sign in to comment.