Skip to content

add missing visit_enum #2836

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

Open
wants to merge 11 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
55 changes: 55 additions & 0 deletions serde/src/de/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2010,6 +2010,18 @@ pub trait EnumAccess<'de>: Sized {
}
}

/// Variant Hint for VariantAccess
pub enum VariantHint {
/// variant with no values
Unit,
/// variant with a single value
Newtype,
/// vuple-like variant with the length of the tuple
Tuple(usize),
/// struct-like variant. with names of the fields of the struct variant
Struct(&'static [&'static str]),
}

/// `VariantAccess` is a visitor that is created by the `Deserializer` and
/// passed to the `Deserialize` to deserialize the content of a particular enum
/// variant.
Expand Down Expand Up @@ -2127,6 +2139,49 @@ pub trait VariantAccess<'de>: Sized {
self.newtype_variant_seed(PhantomData)
}

/// Variant type hint
/// ```edition2021
/// # use serde::de::{self, value, DeserializeSeed, Visitor, VariantAccess, VariantHint, Unexpected};
/// #
/// # enum X {
/// # Unit,
/// # Newtype,
/// # }
/// #
/// # impl<'de> VariantAccess<'de> for X {
/// # type Error = value::Error;
/// #
/// # fn unit_variant(self) -> Result<(), Self::Error> {
/// # unimplemented!()
/// # }
/// #
/// fn hint(&self) -> Option<VariantHint> {
/// Some(match self {
/// Self::Unit => VariantHint::Unit,
/// Self::Newtype => VariantHint::Newtype,
/// })
/// }
/// #
/// # fn newtype_variant_seed<T>(self, _seed: T) -> Result<T::Value, Self::Error>
/// # where
/// # T: DeserializeSeed<'de>,
/// # { unimplemented!() }
/// #
/// # fn tuple_variant<V>(self, _: usize, _: V) -> Result<V::Value, Self::Error>
/// # where
/// # V: Visitor<'de>,
/// # { unimplemented!() }
/// #
/// # fn struct_variant<V>(self, _: &[&str], _: V) -> Result<V::Value, Self::Error>
/// # where
/// # V: Visitor<'de>,
/// # { unimplemented!() }
/// # }
/// ```
fn hint(&self) -> Option<VariantHint> {
None
}

/// Called when deserializing a tuple-like variant.
///
/// The `len` is the number of fields expected in the tuple variant.
Expand Down
16 changes: 12 additions & 4 deletions serde/src/private/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ mod content {
use crate::de::value::{MapDeserializer, SeqDeserializer};
use crate::de::{
self, size_hint, Deserialize, DeserializeSeed, Deserializer, EnumAccess, Expected,
IgnoredAny, MapAccess, SeqAccess, Unexpected, Visitor,
IgnoredAny, MapAccess, SeqAccess, Unexpected, VariantAccess, Visitor,
};

/// Used from generated code to buffer the contents of the Deserializer when
Expand Down Expand Up @@ -525,13 +525,21 @@ mod content {
Ok(Content::Map(vec))
}

fn visit_enum<V>(self, _visitor: V) -> Result<Self::Value, V::Error>
fn visit_enum<V>(self, visitor: V) -> Result<Self::Value, V::Error>
where
V: EnumAccess<'de>,
{
Err(de::Error::custom(
let (key, data) = tri!(visitor.variant::<Content>());
let variant_hint = tri!(data.hint().ok_or(de::Error::custom(
"untagged and internally tagged enums do not support enum input",
))
)));
let data = match variant_hint {
de::VariantHint::Unit => Content::Unit,
de::VariantHint::Newtype => tri!(data.newtype_variant()),
de::VariantHint::Tuple(len) => tri!(data.tuple_variant(len, self)),
de::VariantHint::Struct(fields) => tri!(data.struct_variant(fields, self)),
};
Ok(Content::Map([(key, data)].into()))
}
}

Expand Down