From 45acd4f5b592536f013b94084faca41b42e48c13 Mon Sep 17 00:00:00 2001 From: Vincent Prouillet Date: Tue, 25 Apr 2017 13:57:35 +0900 Subject: Deserialize enum in Value --- src/de.rs | 4 ++- src/value.rs | 87 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 89 insertions(+), 2 deletions(-) diff --git a/src/de.rs b/src/de.rs index b29fc98..abb7cda 100644 --- a/src/de.rs +++ b/src/de.rs @@ -209,7 +209,7 @@ impl<'de, 'b> de::Deserializer<'de> for &'b mut Deserializer<'de> { if self.peek_char()? == '"' { // Visit a unit variant. match self.next()?.unwrap() { - Token::String { ref val, ..} => { + Token::String { ref val, .. } => { visitor.visit_enum(val.clone().into_deserializer()) }, _ => Err(Error::from_kind(ErrorKind::ExpectedString)) @@ -602,6 +602,7 @@ impl<'de> de::MapAccess<'de> for InlineTableDeserializer<'de> { } } + impl<'a> Deserializer<'a> { /// Creates a new deserializer which will be deserializing the string /// provided. @@ -1229,6 +1230,7 @@ impl<'a> Header<'a> { } } +#[derive(Debug)] enum Value<'a> { Integer(i64), Float(f64), diff --git a/src/value.rs b/src/value.rs index f499ff6..ebb14fc 100644 --- a/src/value.rs +++ b/src/value.rs @@ -8,6 +8,7 @@ use std::vec; use serde::ser; use serde::de; +use serde::de::IntoDeserializer; pub use datetime::{Datetime, DatetimeParseError}; use datetime::{DatetimeFromString, SERDE_STRUCT_FIELD_NAME}; @@ -505,6 +506,31 @@ impl<'de> de::Deserializer<'de> for Value { } } + #[inline] + fn deserialize_enum( + self, + _name: &str, + _variants: &'static [&'static str], + visitor: V, + ) -> Result + where + V: de::Visitor<'de>, + { + let (variant, value) = match self { + Value::String(variant) => (variant, None), + _ => { + return Err(de::Error::invalid_type(de::Unexpected::UnitVariant, &"string only"),); + } + }; + + visitor.visit_enum( + EnumDeserializer { + variant: variant, + value: value, + }, + ) +} + // `None` is interpreted as a missing field so be sure to implement `Some` // as a present field. fn deserialize_option(self, visitor: V) -> Result @@ -516,10 +542,69 @@ impl<'de> de::Deserializer<'de> for Value { forward_to_deserialize_any! { bool u8 u16 u32 u64 i8 i16 i32 i64 f32 f64 char str string unit seq bytes byte_buf map unit_struct tuple_struct struct - tuple ignored_any enum newtype_struct identifier + tuple ignored_any newtype_struct identifier + } +} +struct EnumDeserializer { + variant: String, + value: Option, +} + +impl<'de> de::EnumAccess<'de> for EnumDeserializer { + type Error = ::de::Error; + type Variant = VariantDeserializer; + + fn variant_seed(self, seed: V) -> Result<(V::Value, VariantDeserializer), Self::Error> + where + V: de::DeserializeSeed<'de>, + { + let variant = self.variant.into_deserializer(); + let visitor = VariantDeserializer { value: self.value }; + seed.deserialize(variant).map(|v| (v, visitor)) } } +struct VariantDeserializer { + value: Option, +} + +impl<'de> de::VariantAccess<'de> for VariantDeserializer { + type Error = ::de::Error; + + fn unit_variant(self) -> Result<(), Self::Error> { + match self.value { + Some(value) => de::Deserialize::deserialize(value), + None => Ok(()), + } + } + + fn newtype_variant_seed(self, _: T) -> Result + where + T: de::DeserializeSeed<'de>, + { + Err(de::Error::invalid_type(de::Unexpected::UnitVariant, &"newtype variant")) + } + + fn tuple_variant(self, _len: usize, _: V) -> Result + where + V: de::Visitor<'de>, + { + Err(de::Error::invalid_type(de::Unexpected::UnitVariant, &"tuple variant")) + } + + fn struct_variant( + self, + _fields: &'static [&'static str], + _: V, + ) -> Result + where + V: de::Visitor<'de>, + { + Err(de::Error::invalid_type(de::Unexpected::UnitVariant, &"struct variant")) + } +} + + struct SeqDeserializer { iter: vec::IntoIter, } -- cgit v1.2.3