From 0c1b047834da51d5527822b2174af550418e977a Mon Sep 17 00:00:00 2001 From: Andrew Toth Date: Sat, 3 Feb 2024 14:47:30 -0500 Subject: [PATCH] Support flattened enum with default attributes --- serde_derive/src/de.rs | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/serde_derive/src/de.rs b/serde_derive/src/de.rs index e3b737c61..b48e9fed7 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() && cattrs.default().is_none() { + let span = field.original.span(); + quote_spanned!(span=> + return _serde::__private::Err( + _serde::de::Error::custom(_e) + ); + ) + } else { + let is_missing = Expr(expr_is_missing(field, cattrs)); + quote!(#is_missing) + }; + quote! { - let #name: #field_ty = #func( + let #name: #field_ty = match #func( _serde::__private::de::FlatMapDeserializer( &mut __collect, - _serde::__private::PhantomData))?; + _serde::__private::PhantomData)) { + _serde::__private::Ok(#name) => #name, + _serde::__private::Err( + _e @ _serde::__private::de::FlatMapDeserializerError::NoVariantFoundInFlattenedData(..) + ) => { + #no_variant_expr + } + _serde::__private::Err( + _serde::__private::de::FlatMapDeserializerError::Inner(e) + ) => { + return _serde::__private::Err(e); + } + }; } });