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

Rename field_names_idents -> field_idents_aliases #2842

Merged
merged 2 commits into from
Oct 22, 2024
Merged
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
66 changes: 40 additions & 26 deletions serde_derive/src/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -966,7 +966,7 @@ fn deserialize_struct(
};
let expecting = cattrs.expecting().unwrap_or(&expecting);

let field_names_idents: Vec<_> = fields
let field_idents_aliases: Vec<_> = fields
.iter()
.enumerate()
// Skip fields that shouldn't be deserialized or that were flattened,
Expand All @@ -976,15 +976,15 @@ fn deserialize_struct(
.collect();

let has_flatten = has_flatten(fields);
let field_visitor = deserialize_field_identifier(&field_names_idents, cattrs, has_flatten);
let field_visitor = deserialize_field_identifier(&field_idents_aliases, cattrs, has_flatten);

// untagged struct variants do not get a visit_seq method. The same applies to
// structs that only have a map representation.
let visit_seq = match form {
StructForm::Untagged(..) => None,
_ if has_flatten => None,
_ => {
let mut_seq = if field_names_idents.is_empty() {
let mut_seq = if field_idents_aliases.is_empty() {
quote!(_)
} else {
quote!(mut __seq)
Expand Down Expand Up @@ -1032,7 +1032,9 @@ fn deserialize_struct(
let fields_stmt = if has_flatten {
None
} else {
let field_names = field_names_idents.iter().flat_map(|&(_, aliases)| aliases);
let field_names = field_idents_aliases
.iter()
.flat_map(|&(_, aliases)| aliases);

Some(quote! {
#[doc(hidden)]
Expand Down Expand Up @@ -1125,23 +1127,25 @@ fn deserialize_struct_in_place(
let expecting = format!("struct {}", params.type_name());
let expecting = cattrs.expecting().unwrap_or(&expecting);

let field_names_idents: Vec<_> = fields
let field_idents_aliases: Vec<_> = fields
.iter()
.enumerate()
.filter(|&(_, field)| !field.attrs.skip_deserializing())
.map(|(i, field)| (field_i(i), field.attrs.aliases()))
.collect();

let field_visitor = deserialize_field_identifier(&field_names_idents, cattrs, false);
let field_visitor = deserialize_field_identifier(&field_idents_aliases, cattrs, false);

let mut_seq = if field_names_idents.is_empty() {
let mut_seq = if field_idents_aliases.is_empty() {
quote!(_)
} else {
quote!(mut __seq)
};
let visit_seq = Stmts(deserialize_seq_in_place(params, fields, cattrs, expecting));
let visit_map = Stmts(deserialize_map_in_place(params, fields, cattrs));
let field_names = field_names_idents.iter().flat_map(|&(_, aliases)| aliases);
let field_names = field_idents_aliases
.iter()
.flat_map(|&(_, aliases)| aliases);
let type_name = cattrs.name().deserialize_name();

let in_place_impl_generics = de_impl_generics.in_place();
Expand Down Expand Up @@ -1248,12 +1252,12 @@ fn prepare_enum_variant_enum(variants: &[Variant]) -> (TokenStream, Stmts) {
}
};

let variant_names_idents: Vec<_> = deserialized_variants
let variant_idents_aliases: Vec<_> = deserialized_variants
.map(|(i, variant)| (field_i(i), variant.attrs.aliases()))
.collect();

let variant_visitor = Stmts(deserialize_generated_identifier(
&variant_names_idents,
&variant_idents_aliases,
false, // variant identifiers do not depend on the presence of flatten fields
true,
None,
Expand Down Expand Up @@ -1991,18 +1995,21 @@ fn deserialize_untagged_newtype_variant(
}

fn deserialize_generated_identifier(
fields: &[(Ident, &BTreeSet<String>)],
field_idents_aliases: &[(Ident, &BTreeSet<String>)],
has_flatten: bool,
is_variant: bool,
ignore_variant: Option<TokenStream>,
fallthrough: Option<TokenStream>,
) -> Fragment {
let this_value = quote!(__Field);
let field_idents: &Vec<_> = &fields.iter().map(|(ident, _)| ident).collect();
let field_idents: &Vec<_> = &field_idents_aliases
.iter()
.map(|(ident, _)| ident)
.collect();

let visitor_impl = Stmts(deserialize_identifier(
&this_value,
fields,
field_idents_aliases,
is_variant,
fallthrough,
None,
Expand Down Expand Up @@ -2048,7 +2055,7 @@ fn deserialize_generated_identifier(
/// Generates enum and its `Deserialize` implementation that represents each
/// non-skipped field of the struct
fn deserialize_field_identifier(
fields: &[(Ident, &BTreeSet<String>)],
field_idents_aliases: &[(Ident, &BTreeSet<String>)],
cattrs: &attr::Container,
has_flatten: bool,
) -> Stmts {
Expand All @@ -2065,7 +2072,7 @@ fn deserialize_field_identifier(
};

Stmts(deserialize_generated_identifier(
fields,
field_idents_aliases,
has_flatten,
false,
ignore_variant,
Expand Down Expand Up @@ -2123,12 +2130,12 @@ fn deserialize_custom_identifier(
(variants, None, None)
};

let names_idents: Vec<_> = ordinary
let idents_aliases: Vec<_> = ordinary
.iter()
.map(|variant| (variant.ident.clone(), variant.attrs.aliases()))
.collect();

let names = names_idents.iter().flat_map(|&(_, aliases)| aliases);
let names = idents_aliases.iter().flat_map(|&(_, aliases)| aliases);

let names_const = if fallthrough.is_some() {
None
Expand All @@ -2151,7 +2158,7 @@ fn deserialize_custom_identifier(
let delife = params.borrowed.de_lifetime();
let visitor_impl = Stmts(deserialize_identifier(
&this_value,
&names_idents,
&idents_aliases,
is_variant,
fallthrough,
fallthrough_borrowed,
Expand Down Expand Up @@ -2184,18 +2191,18 @@ fn deserialize_custom_identifier(

fn deserialize_identifier(
this_value: &TokenStream,
fields: &[(Ident, &BTreeSet<String>)],
field_idents_aliases: &[(Ident, &BTreeSet<String>)],
is_variant: bool,
fallthrough: Option<TokenStream>,
fallthrough_borrowed: Option<TokenStream>,
collect_other_fields: bool,
expecting: Option<&str>,
) -> Fragment {
let str_mapping = fields.iter().map(|(ident, aliases)| {
let str_mapping = field_idents_aliases.iter().map(|(ident, aliases)| {
// `aliases` also contains a main name
quote!(#(#aliases)|* => _serde::__private::Ok(#this_value::#ident))
});
let bytes_mapping = fields.iter().map(|(ident, aliases)| {
let bytes_mapping = field_idents_aliases.iter().map(|(ident, aliases)| {
// `aliases` also contains a main name
let aliases = aliases
.iter()
Expand Down Expand Up @@ -2350,17 +2357,24 @@ fn deserialize_identifier(
}
}
} else {
let u64_mapping = fields.iter().enumerate().map(|(i, (ident, _))| {
let i = i as u64;
quote!(#i => _serde::__private::Ok(#this_value::#ident))
});
let u64_mapping = field_idents_aliases
.iter()
.enumerate()
.map(|(i, (ident, _))| {
let i = i as u64;
quote!(#i => _serde::__private::Ok(#this_value::#ident))
});

let u64_fallthrough_arm_tokens;
let u64_fallthrough_arm = if let Some(fallthrough) = &fallthrough {
fallthrough
} else {
let index_expecting = if is_variant { "variant" } else { "field" };
let fallthrough_msg = format!("{} index 0 <= i < {}", index_expecting, fields.len());
let fallthrough_msg = format!(
"{} index 0 <= i < {}",
index_expecting,
field_idents_aliases.len(),
);
u64_fallthrough_arm_tokens = quote! {
_serde::__private::Err(_serde::de::Error::invalid_value(
_serde::de::Unexpected::Unsigned(__value),
Expand Down