From 099d7b66836bc994b26e78b4066f28f32b82ffe9 Mon Sep 17 00:00:00 2001 From: Vincent Prouillet Date: Mon, 24 Apr 2017 21:48:02 +0900 Subject: Allow to deserialize/serialize into enums Close #164 --- src/de.rs | 135 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- src/ser.rs | 2 +- src/value.rs | 2 +- 3 files changed, 136 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/de.rs b/src/de.rs index 4578c9a..7e534f5 100644 --- a/src/de.rs +++ b/src/de.rs @@ -11,6 +11,7 @@ use std::str; use std::vec; use serde::de; +use serde::de::IntoDeserializer; use tokens::{Tokenizer, Token, Error as TokenError}; use datetime::{SERDE_STRUCT_FIELD_NAME, SERDE_STRUCT_NAME}; @@ -121,6 +122,12 @@ enum ErrorKind { /// type. Custom, + /// TODO + ExpectedMapEnd, + ExpectedEnum, + ExpectedMapColon, + ExpectedString, + #[doc(hidden)] __Nonexhaustive, } @@ -145,6 +152,7 @@ impl<'de, 'b> de::Deserializer<'de> for &'b mut Deserializer<'de> { values: None, array: false, }; + while let Some(line) = self.line()? { match line { Line::Table { at, mut header, array } => { @@ -192,9 +200,40 @@ impl<'de, 'b> de::Deserializer<'de> for &'b mut Deserializer<'de> { }) } + fn deserialize_enum( + self, + _name: &'static str, + _variants: &'static [&'static str], + visitor: V + ) -> Result + where V: de::Visitor<'de> + { + if self.peek_char()? == '"' { + // Visit a unit variant. + match self.next()?.unwrap() { + Token::String { ref val, ..} => { + visitor.visit_enum(val.clone().into_deserializer()) + }, + _ => Err(Error::from_kind(ErrorKind::ExpectedString)) + } + } else if self.next_char()? == '{' { + // Visit a newtype variant, tuple variant, or struct variant. + let value = visitor.visit_enum(Enum::new(self))?; + // Parse the matching close brace. + if self.next_char()? == '}' { + Ok(value) + } else { + Err(Error::from_kind(ErrorKind::ExpectedMapEnd)) + } + } else { + Err(Error::from_kind(ErrorKind::ExpectedEnum)) + } + } + + forward_to_deserialize_any! { bool u8 u16 u32 u64 i8 i16 i32 i64 f32 f64 char str string seq - bytes byte_buf map struct unit enum newtype_struct + bytes byte_buf map struct unit newtype_struct ignored_any unit_struct tuple_struct tuple option identifier } } @@ -574,6 +613,82 @@ impl<'de> de::MapAccess<'de> for InlineTableDeserializer<'de> { } } +struct Enum<'a, 'de: 'a> { + de: &'a mut Deserializer<'de>, +} + +impl<'a, 'de> Enum<'a, 'de> { + fn new(de: &'a mut Deserializer<'de>) -> Self { + Enum { de: de } + } +} + +// `EnumAccess` is provided to the `Visitor` to give it the ability to determine +// which variant of the enum is supposed to be deserialized. +// +// Note that all enum deserialization methods in Serde refer exclusively to the +// "externally tagged" enum representation. +impl<'de, 'a> de::EnumAccess<'de> for Enum<'a, 'de> { + type Error = Error; + type Variant = Self; + + fn variant_seed(self, seed: V) -> Result<(V::Value, Self::Variant), Error> + where V: de::DeserializeSeed<'de> + { + // The `deserialize_enum` method parsed a `{` character so we are + // currently inside of a map. The seed will be deserializing itself from + // the key of the map. + let val = seed.deserialize(&mut *self.de)?; + // Parse the colon separating map key from value. + if self.de.next_char()? == ':' { + Ok((val, self)) + } else { + Err(Error::from_kind(ErrorKind::ExpectedMapColon)) + } + } +} + +// `VariantAccess` is provided to the `Visitor` to give it the ability to see +// the content of the single variant that it decided to deserialize. +impl<'de, 'a> de::VariantAccess<'de> for Enum<'a, 'de> { + type Error = Error; + + // If the `Visitor` expected this variant to be a unit variant, the input + // should have been the plain string case handled in `deserialize_enum`. + fn unit_variant(self) -> Result<(), Error> { + Err(Error::from_kind(ErrorKind::ExpectedString)) + } + + // Newtype variants are represented in JSON as `{ NAME: VALUE }` so + // deserialize the value here. + fn newtype_variant_seed(self, seed: T) -> Result + where T: de::DeserializeSeed<'de> + { + seed.deserialize(self.de) + } + + // Tuple variants are represented in JSON as `{ NAME: [DATA...] }` so + // deserialize the sequence of data here. + fn tuple_variant(self, _len: usize, visitor: V) -> Result + where V: de::Visitor<'de> + { + de::Deserializer::deserialize_seq(self.de, visitor) + } + + // Struct variants are represented in JSON as `{ NAME: { K: V, ... } }` so + // deserialize the inner map here. + fn struct_variant( + self, + _fields: &'static [&'static str], + visitor: V, + ) -> Result + where V: de::Visitor<'de> + { + de::Deserializer::deserialize_map(self.de, visitor) + } +} + + impl<'a> Deserializer<'a> { /// Creates a new deserializer which will be deserializing the string /// provided. @@ -959,6 +1074,16 @@ impl<'a> Deserializer<'a> { self.tokens.peek().map_err(|e| self.token_error(e)) } + fn peek_char(&mut self) -> Result { + self.input.chars().next().ok_or(Error::from_kind(ErrorKind::UnexpectedEof)) + } + + fn next_char(&mut self) -> Result { + let ch = self.peek_char()?; + self.input = &self.input[ch.len_utf8()..]; + Ok(ch) + } + fn eof(&self) -> Error { self.error(self.input.len(), ErrorKind::UnexpectedEof) } @@ -1092,6 +1217,10 @@ impl fmt::Display for Error { ErrorKind::RedefineAsArray => "table redefined as array".fmt(f)?, ErrorKind::EmptyTableKey => "empty table key found".fmt(f)?, ErrorKind::Custom => self.inner.message.fmt(f)?, + ErrorKind::ExpectedMapEnd => "expected end of map".fmt(f)?, + ErrorKind::ExpectedEnum => "expected enum".fmt(f)?, + ErrorKind::ExpectedMapColon => "expected map colon".fmt(f)?, + ErrorKind::ExpectedString => "expected string".fmt(f)?, ErrorKind::__Nonexhaustive => panic!(), } @@ -1134,6 +1263,10 @@ impl error::Error for Error { ErrorKind::RedefineAsArray => "table redefined as array", ErrorKind::EmptyTableKey => "empty table key found", ErrorKind::Custom => "a custom error", + ErrorKind::ExpectedMapEnd => "expected end of map", + ErrorKind::ExpectedEnum => "expected enum", + ErrorKind::ExpectedMapColon => "expected map colon", + ErrorKind::ExpectedString => "expected string", ErrorKind::__Nonexhaustive => panic!(), } } diff --git a/src/ser.rs b/src/ser.rs index e4b62f7..2365b38 100644 --- a/src/ser.rs +++ b/src/ser.rs @@ -423,7 +423,7 @@ impl<'a, 'b> ser::Serializer for &'b mut Serializer<'a> { _variant_index: u32, _variant: &'static str) -> Result<(), Self::Error> { - Err(Error::UnsupportedType) + self.serialize_str(_variant) } fn serialize_newtype_struct(self, _name: &'static str, value: &T) diff --git a/src/value.rs b/src/value.rs index 7c2b3eb..f499ff6 100644 --- a/src/value.rs +++ b/src/value.rs @@ -694,7 +694,7 @@ impl ser::Serializer for Serializer { _variant_index: u32, _variant: &'static str) -> Result { - Err(::ser::Error::UnsupportedType) + self.serialize_str(_variant) } fn serialize_newtype_struct(self, -- cgit v1.2.3 From 9b7fbd316a680fb0f1cad46af7c65eba80526318 Mon Sep 17 00:00:00 2001 From: Vincent Prouillet Date: Tue, 25 Apr 2017 12:06:04 +0900 Subject: Remove everything other than enum variant code --- src/de.rs | 97 --------------------------------------------------------------- 1 file changed, 97 deletions(-) (limited to 'src') diff --git a/src/de.rs b/src/de.rs index 7e534f5..b29fc98 100644 --- a/src/de.rs +++ b/src/de.rs @@ -123,9 +123,7 @@ enum ErrorKind { Custom, /// TODO - ExpectedMapEnd, ExpectedEnum, - ExpectedMapColon, ExpectedString, #[doc(hidden)] @@ -216,15 +214,6 @@ impl<'de, 'b> de::Deserializer<'de> for &'b mut Deserializer<'de> { }, _ => Err(Error::from_kind(ErrorKind::ExpectedString)) } - } else if self.next_char()? == '{' { - // Visit a newtype variant, tuple variant, or struct variant. - let value = visitor.visit_enum(Enum::new(self))?; - // Parse the matching close brace. - if self.next_char()? == '}' { - Ok(value) - } else { - Err(Error::from_kind(ErrorKind::ExpectedMapEnd)) - } } else { Err(Error::from_kind(ErrorKind::ExpectedEnum)) } @@ -613,82 +602,6 @@ impl<'de> de::MapAccess<'de> for InlineTableDeserializer<'de> { } } -struct Enum<'a, 'de: 'a> { - de: &'a mut Deserializer<'de>, -} - -impl<'a, 'de> Enum<'a, 'de> { - fn new(de: &'a mut Deserializer<'de>) -> Self { - Enum { de: de } - } -} - -// `EnumAccess` is provided to the `Visitor` to give it the ability to determine -// which variant of the enum is supposed to be deserialized. -// -// Note that all enum deserialization methods in Serde refer exclusively to the -// "externally tagged" enum representation. -impl<'de, 'a> de::EnumAccess<'de> for Enum<'a, 'de> { - type Error = Error; - type Variant = Self; - - fn variant_seed(self, seed: V) -> Result<(V::Value, Self::Variant), Error> - where V: de::DeserializeSeed<'de> - { - // The `deserialize_enum` method parsed a `{` character so we are - // currently inside of a map. The seed will be deserializing itself from - // the key of the map. - let val = seed.deserialize(&mut *self.de)?; - // Parse the colon separating map key from value. - if self.de.next_char()? == ':' { - Ok((val, self)) - } else { - Err(Error::from_kind(ErrorKind::ExpectedMapColon)) - } - } -} - -// `VariantAccess` is provided to the `Visitor` to give it the ability to see -// the content of the single variant that it decided to deserialize. -impl<'de, 'a> de::VariantAccess<'de> for Enum<'a, 'de> { - type Error = Error; - - // If the `Visitor` expected this variant to be a unit variant, the input - // should have been the plain string case handled in `deserialize_enum`. - fn unit_variant(self) -> Result<(), Error> { - Err(Error::from_kind(ErrorKind::ExpectedString)) - } - - // Newtype variants are represented in JSON as `{ NAME: VALUE }` so - // deserialize the value here. - fn newtype_variant_seed(self, seed: T) -> Result - where T: de::DeserializeSeed<'de> - { - seed.deserialize(self.de) - } - - // Tuple variants are represented in JSON as `{ NAME: [DATA...] }` so - // deserialize the sequence of data here. - fn tuple_variant(self, _len: usize, visitor: V) -> Result - where V: de::Visitor<'de> - { - de::Deserializer::deserialize_seq(self.de, visitor) - } - - // Struct variants are represented in JSON as `{ NAME: { K: V, ... } }` so - // deserialize the inner map here. - fn struct_variant( - self, - _fields: &'static [&'static str], - visitor: V, - ) -> Result - where V: de::Visitor<'de> - { - de::Deserializer::deserialize_map(self.de, visitor) - } -} - - impl<'a> Deserializer<'a> { /// Creates a new deserializer which will be deserializing the string /// provided. @@ -1078,12 +991,6 @@ impl<'a> Deserializer<'a> { self.input.chars().next().ok_or(Error::from_kind(ErrorKind::UnexpectedEof)) } - fn next_char(&mut self) -> Result { - let ch = self.peek_char()?; - self.input = &self.input[ch.len_utf8()..]; - Ok(ch) - } - fn eof(&self) -> Error { self.error(self.input.len(), ErrorKind::UnexpectedEof) } @@ -1217,9 +1124,7 @@ impl fmt::Display for Error { ErrorKind::RedefineAsArray => "table redefined as array".fmt(f)?, ErrorKind::EmptyTableKey => "empty table key found".fmt(f)?, ErrorKind::Custom => self.inner.message.fmt(f)?, - ErrorKind::ExpectedMapEnd => "expected end of map".fmt(f)?, ErrorKind::ExpectedEnum => "expected enum".fmt(f)?, - ErrorKind::ExpectedMapColon => "expected map colon".fmt(f)?, ErrorKind::ExpectedString => "expected string".fmt(f)?, ErrorKind::__Nonexhaustive => panic!(), } @@ -1263,9 +1168,7 @@ impl error::Error for Error { ErrorKind::RedefineAsArray => "table redefined as array", ErrorKind::EmptyTableKey => "empty table key found", ErrorKind::Custom => "a custom error", - ErrorKind::ExpectedMapEnd => "expected end of map", ErrorKind::ExpectedEnum => "expected enum", - ErrorKind::ExpectedMapColon => "expected map colon", ErrorKind::ExpectedString => "expected string", ErrorKind::__Nonexhaustive => panic!(), } -- cgit v1.2.3 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(-) (limited to 'src') 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 From 082ee7090212e8a377b2145fe82712cc41431fee Mon Sep 17 00:00:00 2001 From: Vincent Prouillet Date: Fri, 28 Apr 2017 13:00:37 +0900 Subject: Address comment and make test pass --- src/de.rs | 36 ++++++++++++++++------------ src/ser.rs | 4 ++-- src/value.rs | 78 ++++-------------------------------------------------------- 3 files changed, 28 insertions(+), 90 deletions(-) (limited to 'src') diff --git a/src/de.rs b/src/de.rs index abb7cda..223d492 100644 --- a/src/de.rs +++ b/src/de.rs @@ -122,8 +122,7 @@ enum ErrorKind { /// type. Custom, - /// TODO - ExpectedEnum, + /// A struct was expected but something else was found ExpectedString, #[doc(hidden)] @@ -206,16 +205,15 @@ impl<'de, 'b> de::Deserializer<'de> for &'b mut Deserializer<'de> { ) -> Result where V: de::Visitor<'de> { - if self.peek_char()? == '"' { - // Visit a unit variant. - match self.next()?.unwrap() { - Token::String { ref val, .. } => { - visitor.visit_enum(val.clone().into_deserializer()) + if let Some(next) = self.next()? { + match next { + Token::String { val, .. } => { + visitor.visit_enum(val.into_deserializer()) }, _ => Err(Error::from_kind(ErrorKind::ExpectedString)) } } else { - Err(Error::from_kind(ErrorKind::ExpectedEnum)) + Err(Error::from_kind(ErrorKind::UnexpectedEof)) } } @@ -517,10 +515,24 @@ impl<'de> de::Deserializer<'de> for ValueDeserializer<'de> { visitor.visit_some(self) } + fn deserialize_enum( + self, + _name: &'static str, + _variants: &'static [&'static str], + visitor: V + ) -> Result + where V: de::Visitor<'de> + { + match self.value { + Value::String(val) => visitor.visit_enum(val.into_deserializer()), + _ => Err(Error::from_kind(ErrorKind::ExpectedString)) + } + } + forward_to_deserialize_any! { bool u8 u16 u32 u64 i8 i16 i32 i64 f32 f64 char str string seq bytes byte_buf map unit newtype_struct identifier - ignored_any unit_struct tuple_struct tuple enum + ignored_any unit_struct tuple_struct tuple } } @@ -988,10 +1000,6 @@ impl<'a> Deserializer<'a> { self.tokens.peek().map_err(|e| self.token_error(e)) } - fn peek_char(&mut self) -> Result { - self.input.chars().next().ok_or(Error::from_kind(ErrorKind::UnexpectedEof)) - } - fn eof(&self) -> Error { self.error(self.input.len(), ErrorKind::UnexpectedEof) } @@ -1125,7 +1133,6 @@ impl fmt::Display for Error { ErrorKind::RedefineAsArray => "table redefined as array".fmt(f)?, ErrorKind::EmptyTableKey => "empty table key found".fmt(f)?, ErrorKind::Custom => self.inner.message.fmt(f)?, - ErrorKind::ExpectedEnum => "expected enum".fmt(f)?, ErrorKind::ExpectedString => "expected string".fmt(f)?, ErrorKind::__Nonexhaustive => panic!(), } @@ -1169,7 +1176,6 @@ impl error::Error for Error { ErrorKind::RedefineAsArray => "table redefined as array", ErrorKind::EmptyTableKey => "empty table key found", ErrorKind::Custom => "a custom error", - ErrorKind::ExpectedEnum => "expected enum", ErrorKind::ExpectedString => "expected string", ErrorKind::__Nonexhaustive => panic!(), } diff --git a/src/ser.rs b/src/ser.rs index 2365b38..4896f8a 100644 --- a/src/ser.rs +++ b/src/ser.rs @@ -421,9 +421,9 @@ impl<'a, 'b> ser::Serializer for &'b mut Serializer<'a> { fn serialize_unit_variant(self, _name: &'static str, _variant_index: u32, - _variant: &'static str) + variant: &'static str) -> Result<(), Self::Error> { - self.serialize_str(_variant) + self.serialize_str(variant) } fn serialize_newtype_struct(self, _name: &'static str, value: &T) diff --git a/src/value.rs b/src/value.rs index ebb14fc..dcc3567 100644 --- a/src/value.rs +++ b/src/value.rs @@ -516,20 +516,11 @@ impl<'de> de::Deserializer<'de> for Value { 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, - }, - ) -} + match self { + Value::String(variant) => visitor.visit_enum(variant.into_deserializer()), + _ => Err(de::Error::invalid_type(de::Unexpected::UnitVariant, &"string only")), + } + } // `None` is interpreted as a missing field so be sure to implement `Some` // as a present field. @@ -545,65 +536,6 @@ impl<'de> de::Deserializer<'de> for Value { 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