From 6acbe14b89ced8beeabc04fbeb522caa9b7f6770 Mon Sep 17 00:00:00 2001 From: Andrew Toth Date: Fri, 2 Feb 2024 17:43:19 -0500 Subject: [PATCH] Support flattened enum with default field attributes --- serde_derive/src/de.rs | 33 +++++++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/serde_derive/src/de.rs b/serde_derive/src/de.rs index e3b737c61c..4471e95795 100644 --- a/serde_derive/src/de.rs +++ b/serde_derive/src/de.rs @@ -2594,11 +2594,36 @@ fn deserialize_map( } Some(path) => quote!(#path), }; + + let no_variant_expr = if field.attrs.default().is_none() { + let span = field.original.span(); + quote_spanned!(span=> + return _serde::__private::Err( + _serde::de::Error::custom(_e.to_string()) + ); + ) + } else { + let is_missing = Expr(expr_is_missing(field, cattrs)); + quote!(#is_missing) + }; + quote! { - let #name: #field_ty = #func( - _serde::__private::de::FlatMapDeserializer( - &mut __collect, - _serde::__private::PhantomData))?; + let #name: #field_ty = match #func( + _serde::__private::de::FlatMapDeserializer(&mut __collect)) { + _serde::__private::Ok(#name) => #name, + _serde::__private::Err( + _e @ _serde::__private::de::FlatMapDeserializerError::NoVariantFoundInFlattenedData(..) + ) => { + #no_variant_expr + } + _serde::__private::Err( + _serde::__private::de::FlatMapDeserializerError::Message(s) + ) => { + return _serde::__private::Err( + _serde::de::Error::custom(s) + ); + } + }; } });