Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion patchable-macro/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ readme.workspace = true
proc-macro = true

[features]
impl_from = []
serde = []
cloneable = []
impl_from = []

[dependencies]
proc-macro2.workspace = true
Expand Down
31 changes: 19 additions & 12 deletions patchable-macro/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use syn::{
};

pub const IS_SERDE_ENABLED: bool = cfg!(feature = "serde");
pub const IS_CLONEABLE_ENABLED: bool = cfg!(feature = "cloneable");

static PATCHABLE: &str = "patchable";

Expand Down Expand Up @@ -156,17 +157,17 @@ impl<'a> MacroContext<'a> {
quote! {;},
);
let patch_name = &self.patch_struct_name;
let derives = if IS_SERDE_ENABLED {
quote! {
#[derive(::core::clone::Clone, ::core::cmp::PartialEq, ::serde::Deserialize)]
}
} else {
quote! {
#[derive(::core::clone::Clone, ::core::cmp::PartialEq)]
}
};
let mut derives_list = Vec::with_capacity(3);
if IS_CLONEABLE_ENABLED {
derives_list.push(quote! { ::core::clone::Clone });
}
derives_list.push(quote! { ::core::cmp::PartialEq });
if IS_SERDE_ENABLED {
derives_list.push(quote! { ::serde::Deserialize });
}

quote! {
#derives
#[derive(#(#derives_list),*)]
pub struct #patch_name #body
}
}
Expand Down Expand Up @@ -341,10 +342,16 @@ impl<'a> MacroContext<'a> {
let t = &param.ident;
match self.preserved_types.get(t) {
Some(TypeUsage::Patchable) => {
bounded_types.push(quote! { #t: #bound + ::core::clone::Clone });
if IS_CLONEABLE_ENABLED {
bounded_types.push(quote! { #t: #bound + ::core::clone::Clone });
} else {
bounded_types.push(quote! { #t: #bound });
}
}
Some(TypeUsage::NotPatchable) => {
bounded_types.push(quote! { #t: ::core::clone::Clone });
if IS_CLONEABLE_ENABLED {
bounded_types.push(quote! { #t: ::core::clone::Clone });
}
}
None => {}
}
Expand Down
3 changes: 2 additions & 1 deletion patchable/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ serde_json.workspace = true
anyhow.workspace = true

[features]
default = ["serde"]
default = ["serde", "cloneable"]
serde = ["patchable-macro/serde"]
cloneable = ["patchable-macro/cloneable"]
impl_from = ["patchable-macro/impl_from"]

[dev-dependencies]
Expand Down
2 changes: 1 addition & 1 deletion patchable/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ pub use patchable_macro::{Patch, Patchable, patchable_model};
/// Declares the associated patch type.
pub trait Patchable: Sized {
/// The type of patch associated with this structure.
type Patch: Clone;
type Patch;
}

/// A type that can be updated using its companion patch.
Expand Down