Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use indices for fields and variants when that option is enabled. #33

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
25 changes: 18 additions & 7 deletions serde-diff-derive/src/serde_diff/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ fn generate_fields_diff(
let right = format_ident!("r{}", field_idx);

let push = if let Some(_) = ident {
quote!{ctx.push_field(#ident_as_str);}
quote!{ctx.push_field(#field_idx, #ident_as_str);}
} else {
quote!{ctx.push_field_index(#field_idx);}
};
Expand Down Expand Up @@ -170,7 +170,7 @@ fn ok_fields(fields : &syn::Fields) -> Result<Vec<ParsedField>, proc_macro::Toke
}
}

fn generate_arms(name: &syn::Ident, variant: Option<&syn::Ident>, fields: &syn::Fields, matching: bool)
fn generate_arms(name: &syn::Ident, variant: Option<(u16, &syn::Ident)>, fields: &syn::Fields, matching: bool)
-> Result<(Vec<proc_macro2::TokenStream>, Vec<proc_macro2::TokenStream>),
proc_macro2::TokenStream>
{
Expand All @@ -182,14 +182,15 @@ fn generate_arms(name: &syn::Ident, variant: Option<&syn::Ident>, fields: &syn::
matching,
);
let (left, right) = enum_fields(&fields, false);
let variant_specifier = if let Some(id) = variant {
let variant_specifier = if let Some((_, id)) = variant {
quote!{ :: #id}
} else {
quote!{}
};

let variant_as_str = variant.map(|i| i.to_string());
let push_variant = variant.map(|_| quote!{ctx.push_variant(#variant_as_str);});
let variant_idx = variant.map(|(i, _)| i);
let variant_as_str = variant.map(|(_, i)| i.to_string());
let push_variant = variant.map(|_| quote!{ctx.push_variant(#variant_idx, #variant_as_str);});
let pop_variant = variant.map(|_| quote!{ctx.pop_path_element()?;});

let left = if matching {
Expand Down Expand Up @@ -259,6 +260,16 @@ fn generate_arms(name: &syn::Ident, variant: Option<&syn::Ident>, fields: &syn::
}

if let Some(_) = variant {
apply_match_arms.push(quote!{
( &mut #name #variant_specifier #left, Some(serde_diff::DiffPathElementValue::EnumVariantIndex(#variant_idx))) => {
while let Some(element) = ctx.next_path_element(seq)? {
match element {
#(#apply_fn_field_handlers)*
_ => ctx.skip_value(seq)?
}
}
}
});
apply_match_arms.push(quote!{
( &mut #name #variant_specifier #left, Some(serde_diff::DiffPathElementValue::EnumVariant(variant))) if variant == #variant_as_str => {
while let Some(element) = ctx.next_path_element(seq)? {
Expand Down Expand Up @@ -299,8 +310,8 @@ fn generate(
let has_variants = match &input.data {
Data::Enum(e) => {
for matching in &[true, false] {
for v in &e.variants {
let (diff, apply) = generate_arms(&struct_args.ident, Some(&v.ident), &v.fields, *matching)?;
for (i, v) in e.variants.iter().enumerate() {
let (diff, apply) = generate_arms(&struct_args.ident, Some((i as u16, &v.ident)), &v.fields, *matching)?;
diff_match_arms.extend(diff);
apply_match_arms.extend(apply);
}
Expand Down
36 changes: 27 additions & 9 deletions src/difference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,21 +58,38 @@ impl<'a, S: SerializeSeq> DiffContext<'a, S> {
/// Called when we visit a field. If the structure is recursive (i.e. struct within struct,
/// elements within an array) this may be called more than once before a corresponding pop_path_element
/// is called. See `pop_path_element`
pub fn push_field(&mut self, field_name: &'static str) {
self.element_stack
.as_mut()
.unwrap()
.push(ElementStackEntry::PathElement(DiffPathElementValue::Field(
Cow::Borrowed(field_name),
)));
pub fn push_field(&mut self, field_idx: u16, field_name: &'static str) {
if matches!(self.field_path_mode, FieldPathMode::Index) {
self.push_field_index(field_idx);
} else {
self.element_stack
.as_mut()
.unwrap()
.push(ElementStackEntry::PathElement(DiffPathElementValue::Field(
Cow::Borrowed(field_name),
)));
}
}

pub fn push_variant(&mut self, variant_idx: u16, variant_name: &'static str) {
if matches!(self.field_path_mode, FieldPathMode::Index) {
self.push_variant_index(variant_idx);
} else {
self.element_stack
.as_mut()
.unwrap()
.push(ElementStackEntry::PathElement(
DiffPathElementValue::EnumVariant(Cow::Borrowed(variant_name)),
));
}
}

pub fn push_variant(&mut self, variant_name: &'static str) {
pub fn push_variant_index(&mut self, variant_idx: u16) {
self.element_stack
.as_mut()
.unwrap()
.push(ElementStackEntry::PathElement(
DiffPathElementValue::EnumVariant(Cow::Borrowed(variant_name)),
DiffPathElementValue::EnumVariantIndex(variant_idx),
));
}

Expand Down Expand Up @@ -601,6 +618,7 @@ pub enum DiffPathElementValue<'a> {
Field(Cow<'a, str>),
FieldIndex(u16),
EnumVariant(Cow<'a, str>),
EnumVariantIndex(u16),
FullEnumVariant,
CollectionIndex(usize),
AddToCollection,
Expand Down
8 changes: 4 additions & 4 deletions src/implementation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ macro_rules! tuple_impls {
) -> Result<bool, S::Error> {
let mut changed = false;
$(
ctx.push_field(stringify!($n));
ctx.push_field_index($n);
changed |= <$name as $crate::SerdeDiff>::diff(&self.$n, ctx, &other.$n)?;
ctx.pop_path_element()?;
)+
Expand All @@ -112,10 +112,10 @@ macro_rules! tuple_impls {
A: serde::de::SeqAccess<'de>,
{
let mut changed = false;
while let Some($crate::difference::DiffPathElementValue::Field(element)) = ctx.next_path_element(seq)? {
match element.as_ref() {
while let Some($crate::difference::DiffPathElementValue::FieldIndex(element)) = ctx.next_path_element(seq)? {
match element {
$(
stringify!($n) => changed |= <$name as $crate::SerdeDiff>::apply(&mut self.$n, seq, ctx)?,
$n => changed |= <$name as $crate::SerdeDiff>::apply(&mut self.$n, seq, ctx)?,
)+
_ => ctx.skip_value(seq)?,
}
Expand Down