Skip to content

Commit

Permalink
Introduce a dedicated function for generating Field enum
Browse files Browse the repository at this point in the history
(the enum that represents all fields of a struct)
  • Loading branch information
Mingun committed Aug 6, 2023
1 parent b6685cf commit 2a36d11
Showing 1 changed file with 16 additions and 12 deletions.
28 changes: 16 additions & 12 deletions serde_derive/src/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -965,12 +965,7 @@ fn deserialize_struct(
)
})
.collect();
let field_visitor = Stmts(deserialize_generated_identifier(
&field_names_idents,
cattrs,
false,
None,
));
let field_visitor = deserialize_field_identifier(&field_names_idents, cattrs);

// untagged struct variants do not get a visit_seq method. The same applies to
// structs that only have a map representation.
Expand Down Expand Up @@ -1128,12 +1123,7 @@ fn deserialize_struct_in_place(
})
.collect();

let field_visitor = Stmts(deserialize_generated_identifier(
&field_names_idents,
cattrs,
false,
None,
));
let field_visitor = deserialize_field_identifier(&field_names_idents, cattrs);

let mut_seq = if field_names_idents.is_empty() {
quote!(_)
Expand Down Expand Up @@ -2052,6 +2042,20 @@ fn deserialize_generated_identifier(
}
}

/// Generates enum and its `Deserialize` implementation that represents each
/// non-skipped field of the struct
fn deserialize_field_identifier(
fields: &[(&str, Ident, &BTreeSet<String>)],
cattrs: &attr::Container,
) -> Stmts {
Stmts(deserialize_generated_identifier(
fields,
cattrs,
false,
None,
))
}

// Generates `Deserialize::deserialize` body for an enum with
// `serde(field_identifier)` or `serde(variant_identifier)` attribute.
fn deserialize_custom_identifier(
Expand Down

0 comments on commit 2a36d11

Please sign in to comment.