Skip to content

Commit

Permalink
Allow to deserialize struct variants of untagged and adjacently tagge…
Browse files Browse the repository at this point in the history
…d enums from seq
  • Loading branch information
Mingun committed May 28, 2023
1 parent 1aebdc2 commit 77757c4
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 19 deletions.
20 changes: 6 additions & 14 deletions serde_derive/src/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ fn deserialize_body(cont: &Container, params: &Parameters) -> Fragment {
match &cont.data {
Data::Enum(variants) => deserialize_enum(params, variants, &cont.attrs),
Data::Struct(Style::Struct, fields) => {
deserialize_struct(None, params, fields, &cont.attrs, None, &Untagged::No)
deserialize_struct(None, params, fields, &cont.attrs, None)
}
Data::Struct(Style::Tuple, fields) | Data::Struct(Style::Newtype, fields) => {
deserialize_tuple(None, params, fields, &cont.attrs, None)
Expand Down Expand Up @@ -893,18 +893,12 @@ fn deserialize_newtype_struct_in_place(params: &Parameters, field: &Field) -> To
}
}

enum Untagged {
Yes,
No,
}

fn deserialize_struct(
variant_ident: Option<&syn::Ident>,
params: &Parameters,
fields: &[Field],
cattrs: &attr::Container,
deserializer: Option<TokenStream>,
untagged: &Untagged,
) -> Fragment {
let is_enum = variant_ident.is_some();

Expand Down Expand Up @@ -986,17 +980,18 @@ fn deserialize_struct(

// 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 *untagged {
Untagged::No if !cattrs.has_flatten() => Some(quote! {
let visit_seq = if cattrs.has_flatten() {
None
} else {
Some(quote! {
#[inline]
fn visit_seq<__A>(self, #visitor_var: __A) -> _serde::__private::Result<Self::Value, __A::Error>
where
__A: _serde::de::SeqAccess<#delife>,
{
#visit_seq
}
}),
_ => None,
})
};

let visitor_seed = if need_seed && is_enum && cattrs.has_flatten() {
Expand Down Expand Up @@ -1745,7 +1740,6 @@ fn deserialize_externally_tagged_variant(
&variant.fields,
cattrs,
None,
&Untagged::No,
),
}
}
Expand Down Expand Up @@ -1790,7 +1784,6 @@ fn deserialize_internally_tagged_variant(
&variant.fields,
cattrs,
Some(deserializer),
&Untagged::No,
),
Style::Tuple => unreachable!("checked in serde_derive_internals"),
}
Expand Down Expand Up @@ -1849,7 +1842,6 @@ fn deserialize_untagged_variant(
&variant.fields,
cattrs,
Some(deserializer),
&Untagged::Yes,
),
}
}
Expand Down
9 changes: 4 additions & 5 deletions test_suite/tests/test_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -628,6 +628,10 @@ fn test_untagged_enum() {
Token::StructEnd,
],
);
assert_de_tokens(
&Untagged::A { a: 1 },
&[Token::Tuple { len: 1 }, Token::U8(1), Token::TupleEnd],
);

assert_tokens(
&Untagged::B { b: 2 },
Expand Down Expand Up @@ -660,11 +664,6 @@ fn test_untagged_enum() {
],
);

assert_de_tokens_error::<Untagged>(
&[Token::Tuple { len: 1 }, Token::U8(1), Token::TupleEnd],
"data did not match any variant of untagged enum Untagged",
);

assert_de_tokens_error::<Untagged>(
&[
Token::Tuple { len: 3 },
Expand Down

0 comments on commit 77757c4

Please sign in to comment.