Skip to content

Commit

Permalink
Add Generic Support
Browse files Browse the repository at this point in the history
  • Loading branch information
crzysdrs committed Mar 9, 2020
1 parent d8d4b1f commit 57e6866
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
18 changes: 18 additions & 0 deletions examples/struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ struct Test2Struct(u32, u32, u32);
#[derive(SerdeDiff, Serialize, Deserialize, PartialEq, Debug)]
struct Test3Struct;

#[derive(SerdeDiff, Serialize, Deserialize, PartialEq, Debug)]
struct Test4Struct<T>
where T: SerdeDiff
{
a: T,
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
{
let old = TestStruct { a: 5, b: 2. };
Expand Down Expand Up @@ -52,5 +59,16 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let result = Test3Struct;
assert_eq!(result, target);
}
{
let old = Test4Struct { a: 5};
let new = Test4Struct { a: 7};
let mut target = Test4Struct { a: 10};
let json_data = serde_json::to_string(&Diff::serializable(&old, &new))?;
let mut deserializer = serde_json::Deserializer::from_str(&json_data);
Apply::apply(&mut deserializer, &mut target)?;

let result = Test4Struct { a: 7};
assert_eq!(result, target);
}
Ok(())
}
2 changes: 2 additions & 0 deletions serde-diff-derive/src/serde_diff/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ pub struct SerdeDiffStructArgs {
/// Whether the struct is opaque or not
#[darling(default)]
pub opaque: bool,

pub generics: syn::Generics,
}

/// Metadata from the struct's field annotations
Expand Down
14 changes: 8 additions & 6 deletions serde-diff-derive/src/serde_diff/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub fn macro_derive(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
generate_opaque(&input, struct_args)
} else {
// Go ahead and generate the code
match generate_enum(&input, struct_args) {
match generate(&input, struct_args) {
//Ok(v) => {eprintln!("{}", v); v},
Ok(v) => v,
Err(v) => v,
Expand All @@ -30,7 +30,7 @@ pub fn macro_derive(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
generate_opaque(&input, struct_args)
} else {
// Go ahead and generate the code
match generate_enum(&input, struct_args) {
match generate(&input, struct_args) {
//Ok(v) => {eprintln!("{}", v); v},
Ok(v) => v,
Err(v) => v,
Expand Down Expand Up @@ -63,7 +63,7 @@ struct ParsedField {
field_args: args::SerdeDiffFieldArgs,
}

fn generate_enum_fields_diff(
fn generate_fields_diff(
parsed_fields: &[ParsedField],
matching : bool,
) -> proc_macro2::TokenStream {
Expand Down Expand Up @@ -183,7 +183,7 @@ fn generate_arms(name: &syn::Ident, variant: Option<&syn::Ident>, fields: &syn::
let mut diff_match_arms = vec![];
let mut apply_match_arms = vec![];
let parsed_fields = ok_fields(&fields)?;
let diffs = generate_enum_fields_diff(
let diffs = generate_fields_diff(
&parsed_fields,
matching,
);
Expand Down Expand Up @@ -292,7 +292,7 @@ fn generate_arms(name: &syn::Ident, variant: Option<&syn::Ident>, fields: &syn::
Ok((diff_match_arms, apply_match_arms))
}

fn generate_enum(
fn generate(
input: &syn::DeriveInput,
struct_args: args::SerdeDiffStructArgs,
) -> Result<proc_macro::TokenStream, proc_macro::TokenStream> {
Expand Down Expand Up @@ -379,8 +379,10 @@ fn generate_enum(

// Generate the impl block with the diff and apply functions within it
let struct_name = &struct_args.ident;
let generics = &struct_args.generics.params;
let where_clause = &struct_args.generics.where_clause;
let diff_impl = quote! {
impl serde_diff::SerdeDiff for #struct_name {
impl <#generics> serde_diff::SerdeDiff for #struct_name < #generics> #where_clause {
#diff_fn
#apply_fn
}
Expand Down

0 comments on commit 57e6866

Please sign in to comment.