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

Support flattened externally tagged enums with default attributes #2687

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
79 changes: 72 additions & 7 deletions serde/src/private/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::de::{
};

#[cfg(any(feature = "std", feature = "alloc"))]
use crate::de::{MapAccess, Unexpected};
use crate::de::{Expected, MapAccess, StdError, Unexpected};

#[cfg(any(feature = "std", feature = "alloc"))]
pub use self::content::{
Expand Down Expand Up @@ -2586,6 +2586,72 @@ where
}
}

#[cfg(any(feature = "std", feature = "alloc"))]
#[derive(Debug)]
pub enum FlatMapDeserializerError<E> {
Inner(E),
NoVariantFoundInFlattenedData(&'static str),
}

#[cfg(any(feature = "std", feature = "alloc"))]
impl<E> Display for FlatMapDeserializerError<E>
where
E: Display,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
FlatMapDeserializerError::Inner(e) => write!(f, "{}", e),
FlatMapDeserializerError::NoVariantFoundInFlattenedData(name) => {
write!(f, "no variant of enum {} found in flattened data", name)
}
}
}
}

#[cfg(any(feature = "std", feature = "alloc"))]
impl<E> StdError for FlatMapDeserializerError<E> where E: StdError {}

#[cfg(any(feature = "std", feature = "alloc"))]
impl<E> Error for FlatMapDeserializerError<E>
where
E: Error,
{
fn custom<T>(msg: T) -> Self
where
T: Display,
{
FlatMapDeserializerError::Inner(Error::custom(msg))
}

fn invalid_type(unexp: Unexpected, exp: &Expected) -> Self {
FlatMapDeserializerError::Inner(Error::invalid_type(unexp, exp))
}

fn invalid_value(unexp: Unexpected, exp: &Expected) -> Self {
FlatMapDeserializerError::Inner(Error::invalid_value(unexp, exp))
}

fn invalid_length(len: usize, exp: &Expected) -> Self {
FlatMapDeserializerError::Inner(Error::invalid_length(len, exp))
}

fn unknown_variant(variant: &str, exp: &'static [&'static str]) -> Self {
FlatMapDeserializerError::Inner(Error::unknown_variant(variant, exp))
}

fn unknown_field(field: &str, exp: &'static [&'static str]) -> Self {
FlatMapDeserializerError::Inner(Error::unknown_field(field, exp))
}

fn missing_field(field: &'static str) -> Self {
FlatMapDeserializerError::Inner(Error::missing_field(field))
}

fn duplicate_field(field: &'static str) -> Self {
FlatMapDeserializerError::Inner(Error::duplicate_field(field))
}
}

#[cfg(any(feature = "std", feature = "alloc"))]
pub struct FlatMapDeserializer<'a, 'de: 'a, E>(
pub &'a mut Vec<Option<(Content<'de>, Content<'de>)>>,
Expand All @@ -2597,7 +2663,7 @@ impl<'a, 'de, E> FlatMapDeserializer<'a, 'de, E>
where
E: Error,
{
fn deserialize_other<V>() -> Result<V, E> {
fn deserialize_other<V>() -> Result<V, FlatMapDeserializerError<E>> {
Err(Error::custom("can only flatten structs and maps"))
}
}
Expand All @@ -2621,7 +2687,7 @@ impl<'a, 'de, E> Deserializer<'de> for FlatMapDeserializer<'a, 'de, E>
where
E: Error,
{
type Error = E;
type Error = FlatMapDeserializerError<E>;

fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where
Expand All @@ -2645,10 +2711,9 @@ where
}
}

Err(Error::custom(format_args!(
"no variant of enum {} found in flattened data",
name
)))
Err(FlatMapDeserializerError::NoVariantFoundInFlattenedData(
name,
))
}

fn deserialize_map<V>(self, visitor: V) -> Result<V::Value, Self::Error>
Expand Down
29 changes: 27 additions & 2 deletions serde_derive/src/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
};
}
});

Expand Down
Loading