diff --git a/serde/src/private/de.rs b/serde/src/private/de.rs index 50ae6ed15..a394f987b 100644 --- a/serde/src/private/de.rs +++ b/serde/src/private/de.rs @@ -209,10 +209,10 @@ mod content { use crate::lib::*; use crate::actually_private; - use crate::de::value::{MapDeserializer, SeqDeserializer}; + use crate::de::value::{MapDeserializer, SeqDeserializer, UnitDeserializer}; use crate::de::{ - self, size_hint, Deserialize, DeserializeSeed, Deserializer, EnumAccess, Expected, - IgnoredAny, MapAccess, SeqAccess, Unexpected, Visitor, + self, size_hint, Deserialize, DeserializeSeed, Deserializer, EnumAccess, IgnoredAny, + MapAccess, SeqAccess, Unexpected, Visitor, }; /// Used from generated code to buffer the contents of the Deserializer when @@ -261,33 +261,6 @@ mod content { _ => None, } } - - #[cold] - fn unexpected(&self) -> Unexpected { - match *self { - Content::Bool(b) => Unexpected::Bool(b), - Content::U8(n) => Unexpected::Unsigned(n as u64), - Content::U16(n) => Unexpected::Unsigned(n as u64), - Content::U32(n) => Unexpected::Unsigned(n as u64), - Content::U64(n) => Unexpected::Unsigned(n), - Content::I8(n) => Unexpected::Signed(n as i64), - Content::I16(n) => Unexpected::Signed(n as i64), - Content::I32(n) => Unexpected::Signed(n as i64), - Content::I64(n) => Unexpected::Signed(n), - Content::F32(f) => Unexpected::Float(f as f64), - Content::F64(f) => Unexpected::Float(f), - Content::Char(c) => Unexpected::Char(c), - Content::String(ref s) => Unexpected::Str(s), - Content::Str(s) => Unexpected::Str(s), - Content::ByteBuf(ref b) => Unexpected::Bytes(b), - Content::Bytes(b) => Unexpected::Bytes(b), - Content::None | Content::Some(_) => Unexpected::Option, - Content::Unit => Unexpected::Unit, - Content::Newtype(_) => Unexpected::NewtypeStruct, - Content::Seq(_) => Unexpected::Seq, - Content::Map(_) => Unexpected::Map, - } - } } impl<'de> Deserialize<'de> for Content<'de> { @@ -1060,52 +1033,6 @@ mod content { err: PhantomData, } - impl<'de, E> ContentDeserializer<'de, E> - where - E: de::Error, - { - #[cold] - fn invalid_type(self, exp: &Expected) -> E { - de::Error::invalid_type(self.content.unexpected(), exp) - } - - fn deserialize_integer(self, visitor: V) -> Result - where - V: Visitor<'de>, - { - match self.content { - Content::U8(v) => visitor.visit_u8(v), - Content::U16(v) => visitor.visit_u16(v), - Content::U32(v) => visitor.visit_u32(v), - Content::U64(v) => visitor.visit_u64(v), - Content::I8(v) => visitor.visit_i8(v), - Content::I16(v) => visitor.visit_i16(v), - Content::I32(v) => visitor.visit_i32(v), - Content::I64(v) => visitor.visit_i64(v), - _ => Err(self.invalid_type(&visitor)), - } - } - - fn deserialize_float(self, visitor: V) -> Result - where - V: Visitor<'de>, - { - match self.content { - Content::F32(v) => visitor.visit_f32(v), - Content::F64(v) => visitor.visit_f64(v), - Content::U8(v) => visitor.visit_u8(v), - Content::U16(v) => visitor.visit_u16(v), - Content::U32(v) => visitor.visit_u32(v), - Content::U64(v) => visitor.visit_u64(v), - Content::I8(v) => visitor.visit_i8(v), - Content::I16(v) => visitor.visit_i16(v), - Content::I32(v) => visitor.visit_i32(v), - Content::I64(v) => visitor.visit_i64(v), - _ => Err(self.invalid_type(&visitor)), - } - } - } - fn visit_content_seq<'de, V, E>(content: Vec>, visitor: V) -> Result where V: Visitor<'de>, @@ -1139,6 +1066,12 @@ mod content { { type Error = E; + forward_to_deserialize_any! { + bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string + bytes byte_buf seq tuple + tuple_struct map struct identifier + } + fn deserialize_any(self, visitor: V) -> Result where V: Visitor<'de>, @@ -1169,147 +1102,23 @@ mod content { } } - fn deserialize_bool(self, visitor: V) -> Result - where - V: Visitor<'de>, - { - match self.content { - Content::Bool(v) => visitor.visit_bool(v), - _ => Err(self.invalid_type(&visitor)), - } - } - - fn deserialize_i8(self, visitor: V) -> Result - where - V: Visitor<'de>, - { - self.deserialize_integer(visitor) - } - - fn deserialize_i16(self, visitor: V) -> Result - where - V: Visitor<'de>, - { - self.deserialize_integer(visitor) - } - - fn deserialize_i32(self, visitor: V) -> Result - where - V: Visitor<'de>, - { - self.deserialize_integer(visitor) - } - - fn deserialize_i64(self, visitor: V) -> Result - where - V: Visitor<'de>, - { - self.deserialize_integer(visitor) - } - - fn deserialize_u8(self, visitor: V) -> Result - where - V: Visitor<'de>, - { - self.deserialize_integer(visitor) - } - - fn deserialize_u16(self, visitor: V) -> Result - where - V: Visitor<'de>, - { - self.deserialize_integer(visitor) - } - - fn deserialize_u32(self, visitor: V) -> Result - where - V: Visitor<'de>, - { - self.deserialize_integer(visitor) - } - - fn deserialize_u64(self, visitor: V) -> Result - where - V: Visitor<'de>, - { - self.deserialize_integer(visitor) - } - - fn deserialize_f32(self, visitor: V) -> Result - where - V: Visitor<'de>, - { - self.deserialize_float(visitor) - } - - fn deserialize_f64(self, visitor: V) -> Result - where - V: Visitor<'de>, - { - self.deserialize_float(visitor) - } - - fn deserialize_char(self, visitor: V) -> Result - where - V: Visitor<'de>, - { - match self.content { - Content::Char(v) => visitor.visit_char(v), - Content::String(v) => visitor.visit_string(v), - Content::Str(v) => visitor.visit_borrowed_str(v), - _ => Err(self.invalid_type(&visitor)), - } - } - - fn deserialize_str(self, visitor: V) -> Result - where - V: Visitor<'de>, - { - self.deserialize_string(visitor) - } - - fn deserialize_string(self, visitor: V) -> Result - where - V: Visitor<'de>, - { - match self.content { - Content::String(v) => visitor.visit_string(v), - Content::Str(v) => visitor.visit_borrowed_str(v), - Content::ByteBuf(v) => visitor.visit_byte_buf(v), - Content::Bytes(v) => visitor.visit_borrowed_bytes(v), - _ => Err(self.invalid_type(&visitor)), - } - } - - fn deserialize_bytes(self, visitor: V) -> Result - where - V: Visitor<'de>, - { - self.deserialize_byte_buf(visitor) - } - - fn deserialize_byte_buf(self, visitor: V) -> Result - where - V: Visitor<'de>, - { - match self.content { - Content::String(v) => visitor.visit_string(v), - Content::Str(v) => visitor.visit_borrowed_str(v), - Content::ByteBuf(v) => visitor.visit_byte_buf(v), - Content::Bytes(v) => visitor.visit_borrowed_bytes(v), - Content::Seq(v) => visit_content_seq(v, visitor), - _ => Err(self.invalid_type(&visitor)), - } - } - fn deserialize_option(self, visitor: V) -> Result where V: Visitor<'de>, { + // Covered by tests/test_annotations.rs + // flatten::with_optional_field::* match self.content { Content::None => visitor.visit_none(), Content::Some(v) => visitor.visit_some(ContentDeserializer::new(*v)), Content::Unit => visitor.visit_unit(), + // This case is to support data formats which do not encode an + // indication whether a value is optional. An example of such a + // format is JSON, and a counterexample is RON. When requesting + // `deserialize_any` in JSON, the data format never performs + // `Visitor::visit_some` but we still must be able to + // deserialize the resulting Content into data structures with + // optional fields. _ => visitor.visit_some(self), } } @@ -1318,31 +1127,8 @@ mod content { where V: Visitor<'de>, { - match self.content { - Content::Unit => visitor.visit_unit(), - - // Allow deserializing newtype variant containing unit. - // - // #[derive(Deserialize)] - // #[serde(tag = "result")] - // enum Response { - // Success(T), - // } - // - // We want {"result":"Success"} to deserialize into Response<()>. - Content::Map(ref v) if v.is_empty() => visitor.visit_unit(), - _ => Err(self.invalid_type(&visitor)), - } - } - - fn deserialize_unit_struct( - self, - _name: &'static str, - visitor: V, - ) -> Result - where - V: Visitor<'de>, - { + // Covered by tests/test_enum_internally_tagged.rs + // newtype_unit match self.content { // As a special case, allow deserializing untagged newtype // variant containing unit struct. @@ -1358,78 +1144,59 @@ mod content { // // We want {"topic":"Info"} to deserialize even though // ordinarily unit structs do not deserialize from empty map/seq. + // + // Allow deserializing newtype variant containing unit. + // + // #[derive(Deserialize)] + // #[serde(tag = "result")] + // enum Response { + // Success(T), + // } + // + // We want {"result":"Success"} to deserialize into Response<()>. Content::Map(ref v) if v.is_empty() => visitor.visit_unit(), Content::Seq(ref v) if v.is_empty() => visitor.visit_unit(), _ => self.deserialize_any(visitor), } } - fn deserialize_newtype_struct( - self, - _name: &str, - visitor: V, - ) -> Result - where - V: Visitor<'de>, - { - match self.content { - Content::Newtype(v) => visitor.visit_newtype_struct(ContentDeserializer::new(*v)), - _ => visitor.visit_newtype_struct(self), - } - } - - fn deserialize_seq(self, visitor: V) -> Result - where - V: Visitor<'de>, - { - match self.content { - Content::Seq(v) => visit_content_seq(v, visitor), - _ => Err(self.invalid_type(&visitor)), - } - } - - fn deserialize_tuple(self, _len: usize, visitor: V) -> Result - where - V: Visitor<'de>, - { - self.deserialize_seq(visitor) - } - - fn deserialize_tuple_struct( + fn deserialize_unit_struct( self, _name: &'static str, - _len: usize, visitor: V, ) -> Result where V: Visitor<'de>, { - self.deserialize_seq(visitor) - } - - fn deserialize_map(self, visitor: V) -> Result - where - V: Visitor<'de>, - { - match self.content { - Content::Map(v) => visit_content_map(v, visitor), - _ => Err(self.invalid_type(&visitor)), - } + // Covered by tests/test_enum_internally_tagged.rs + // newtype_unit_struct + self.deserialize_unit(visitor) } - fn deserialize_struct( + fn deserialize_newtype_struct( self, - _name: &'static str, - _fields: &'static [&'static str], + _name: &str, visitor: V, ) -> Result where V: Visitor<'de>, { match self.content { - Content::Seq(v) => visit_content_seq(v, visitor), - Content::Map(v) => visit_content_map(v, visitor), - _ => Err(self.invalid_type(&visitor)), + // Covered by tests/test_enum_adjacently_tagged.rs + // newtype_with_newtype + Content::Newtype(v) => visitor.visit_newtype_struct(ContentDeserializer::new(*v)), + // Covered by tests/test_enum_internally_tagged.rs + // newtype_newtype + // This case is to support data formats that encode newtype + // structs and their underlying data the same, with no + // indication whether a newtype wrapper was present. For example + // JSON does this, while RON does not. In RON a newtype's name + // is included in the serialized representation and it knows to + // call `Visitor::visit_newtype_struct` from `deserialize_any`. + // JSON's `deserialize_any` never calls `visit_newtype_struct` + // but in this code we still must be able to deserialize the + // resulting Content into newtypes. + _ => visitor.visit_newtype_struct(self), } } @@ -1443,6 +1210,11 @@ mod content { V: Visitor<'de>, { let (variant, value) = match self.content { + // Covered by tests/test_enum_internally_tagged.rs + // newtype_enum::unit + // newtype_enum::newtype + // newtype_enum::tuple + // newtype_enum::struct_ Content::Map(value) => { let mut iter = value.into_iter(); let (variant, value) = match iter.next() { @@ -1463,39 +1235,26 @@ mod content { } (variant, Some(value)) } + // String() covered by tests/test_annotations.rs + // flatten::enum_::adjacently_tagged::newtype + // flatten::enum_::adjacently_tagged::struct_ + // String() covered by tests/test_enum_internally_tagged.rs + // struct_enum::unit + // String() covered by tests/test_macros.rs + // test_internally_tagged_struct_with_flattened_field s @ Content::String(_) | s @ Content::Str(_) => (s, None), - other => { - return Err(de::Error::invalid_type( - other.unexpected(), - &"string or map", - )); - } + _ => return self.deserialize_any(visitor), }; visitor.visit_enum(EnumDeserializer::new(variant, value)) } - fn deserialize_identifier(self, visitor: V) -> Result + fn deserialize_ignored_any(self, visitor: V) -> Result where V: Visitor<'de>, { - match self.content { - Content::String(v) => visitor.visit_string(v), - Content::Str(v) => visitor.visit_borrowed_str(v), - Content::ByteBuf(v) => visitor.visit_byte_buf(v), - Content::Bytes(v) => visitor.visit_borrowed_bytes(v), - Content::U8(v) => visitor.visit_u8(v), - Content::U64(v) => visitor.visit_u64(v), - _ => Err(self.invalid_type(&visitor)), - } - } - - fn deserialize_ignored_any(self, visitor: V) -> Result - where - V: Visitor<'de>, - { - drop(self); - visitor.visit_unit() + drop(self); + visitor.visit_unit() } fn __deserialize_content( @@ -1506,6 +1265,8 @@ mod content { where V: Visitor<'de, Value = Content<'de>>, { + // Covered by tests/test_enum_internally_tagged.rs + // containing_flatten let _ = visitor; Ok(self.content) } @@ -1525,9 +1286,8 @@ mod content { where E: de::Error, { - variant: Content<'de>, - value: Option>, - err: PhantomData, + variant: ContentDeserializer<'de, E>, + value: Option>, } impl<'de, E> EnumDeserializer<'de, E> @@ -1536,9 +1296,8 @@ mod content { { pub fn new(variant: Content<'de>, value: Option>) -> EnumDeserializer<'de, E> { EnumDeserializer { - variant, - value, - err: PhantomData, + variant: ContentDeserializer::new(variant), + value: value.map(ContentDeserializer::new), } } } @@ -1554,12 +1313,8 @@ mod content { where V: de::DeserializeSeed<'de>, { - let visitor = VariantDeserializer { - value: self.value, - err: PhantomData, - }; - seed.deserialize(ContentDeserializer::new(self.variant)) - .map(|v| (v, visitor)) + let visitor = VariantDeserializer { de: self.value }; + seed.deserialize(self.variant).map(|v| (v, visitor)) } } @@ -1567,8 +1322,7 @@ mod content { where E: de::Error, { - value: Option>, - err: PhantomData, + de: Option>, } impl<'de, E> de::VariantAccess<'de> for VariantDeserializer<'de, E> @@ -1578,8 +1332,17 @@ mod content { type Error = E; fn unit_variant(self) -> Result<(), E> { - match self.value { - Some(value) => de::Deserialize::deserialize(ContentDeserializer::new(value)), + match self.de { + // Covered by tests/test_enum_internally_tagged.rs + // newtype_enum::unit + Some(de) => de::Deserialize::deserialize(de), + // Covered by tests/test_annotations.rs + // flatten::enum_::adjacently_tagged::newtype + // flatten::enum_::adjacently_tagged::struct_ + // Covered by tests/test_enum_internally_tagged.rs + // struct_enum::unit + // Covered by tests/test_macros.rs + // test_internally_tagged_struct_with_flattened_field None => Ok(()), } } @@ -1588,12 +1351,11 @@ mod content { where T: de::DeserializeSeed<'de>, { - match self.value { - Some(value) => seed.deserialize(ContentDeserializer::new(value)), - None => Err(de::Error::invalid_type( - de::Unexpected::UnitVariant, - &"newtype variant", - )), + match self.de { + // Covered by tests/test_enum_internally_tagged.rs + // newtype_enum::newtype + Some(de) => seed.deserialize(de), + None => seed.deserialize(UnitDeserializer::new()), } } @@ -1601,18 +1363,11 @@ mod content { where V: de::Visitor<'de>, { - match self.value { - Some(Content::Seq(v)) => { - de::Deserializer::deserialize_any(SeqDeserializer::new(v.into_iter()), visitor) - } - Some(other) => Err(de::Error::invalid_type( - other.unexpected(), - &"tuple variant", - )), - None => Err(de::Error::invalid_type( - de::Unexpected::UnitVariant, - &"tuple variant", - )), + match self.de { + // Covered by tests/test_enum_internally_tagged.rs + // newtype_enum::tuple + Some(de) => de.deserialize_any(visitor), + None => visitor.visit_unit(), } } @@ -1624,21 +1379,11 @@ mod content { where V: de::Visitor<'de>, { - match self.value { - Some(Content::Map(v)) => { - de::Deserializer::deserialize_any(MapDeserializer::new(v.into_iter()), visitor) - } - Some(Content::Seq(v)) => { - de::Deserializer::deserialize_any(SeqDeserializer::new(v.into_iter()), visitor) - } - Some(other) => Err(de::Error::invalid_type( - other.unexpected(), - &"struct variant", - )), - None => Err(de::Error::invalid_type( - de::Unexpected::UnitVariant, - &"struct variant", - )), + match self.de { + // Covered by tests/test_enum_internally_tagged.rs + // newtype_enum::struct_ + Some(de) => de.deserialize_any(visitor), + None => visitor.visit_unit(), } } } @@ -1649,52 +1394,6 @@ mod content { err: PhantomData, } - impl<'a, 'de, E> ContentRefDeserializer<'a, 'de, E> - where - E: de::Error, - { - #[cold] - fn invalid_type(self, exp: &Expected) -> E { - de::Error::invalid_type(self.content.unexpected(), exp) - } - - fn deserialize_integer(self, visitor: V) -> Result - where - V: Visitor<'de>, - { - match *self.content { - Content::U8(v) => visitor.visit_u8(v), - Content::U16(v) => visitor.visit_u16(v), - Content::U32(v) => visitor.visit_u32(v), - Content::U64(v) => visitor.visit_u64(v), - Content::I8(v) => visitor.visit_i8(v), - Content::I16(v) => visitor.visit_i16(v), - Content::I32(v) => visitor.visit_i32(v), - Content::I64(v) => visitor.visit_i64(v), - _ => Err(self.invalid_type(&visitor)), - } - } - - fn deserialize_float(self, visitor: V) -> Result - where - V: Visitor<'de>, - { - match *self.content { - Content::F32(v) => visitor.visit_f32(v), - Content::F64(v) => visitor.visit_f64(v), - Content::U8(v) => visitor.visit_u8(v), - Content::U16(v) => visitor.visit_u16(v), - Content::U32(v) => visitor.visit_u32(v), - Content::U64(v) => visitor.visit_u64(v), - Content::I8(v) => visitor.visit_i8(v), - Content::I16(v) => visitor.visit_i16(v), - Content::I32(v) => visitor.visit_i32(v), - Content::I64(v) => visitor.visit_i64(v), - _ => Err(self.invalid_type(&visitor)), - } - } - } - fn visit_content_seq_ref<'a, 'de, V, E>( content: &'a [Content<'de>], visitor: V, @@ -1738,6 +1437,12 @@ mod content { { type Error = E; + forward_to_deserialize_any! { + bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string + bytes byte_buf unit unit_struct seq tuple + tuple_struct map struct identifier + } + fn deserialize_any(self, visitor: V) -> Result where V: Visitor<'de>, @@ -1770,139 +1475,6 @@ mod content { } } - fn deserialize_bool(self, visitor: V) -> Result - where - V: Visitor<'de>, - { - match *self.content { - Content::Bool(v) => visitor.visit_bool(v), - _ => Err(self.invalid_type(&visitor)), - } - } - - fn deserialize_i8(self, visitor: V) -> Result - where - V: Visitor<'de>, - { - self.deserialize_integer(visitor) - } - - fn deserialize_i16(self, visitor: V) -> Result - where - V: Visitor<'de>, - { - self.deserialize_integer(visitor) - } - - fn deserialize_i32(self, visitor: V) -> Result - where - V: Visitor<'de>, - { - self.deserialize_integer(visitor) - } - - fn deserialize_i64(self, visitor: V) -> Result - where - V: Visitor<'de>, - { - self.deserialize_integer(visitor) - } - - fn deserialize_u8(self, visitor: V) -> Result - where - V: Visitor<'de>, - { - self.deserialize_integer(visitor) - } - - fn deserialize_u16(self, visitor: V) -> Result - where - V: Visitor<'de>, - { - self.deserialize_integer(visitor) - } - - fn deserialize_u32(self, visitor: V) -> Result - where - V: Visitor<'de>, - { - self.deserialize_integer(visitor) - } - - fn deserialize_u64(self, visitor: V) -> Result - where - V: Visitor<'de>, - { - self.deserialize_integer(visitor) - } - - fn deserialize_f32(self, visitor: V) -> Result - where - V: Visitor<'de>, - { - self.deserialize_float(visitor) - } - - fn deserialize_f64(self, visitor: V) -> Result - where - V: Visitor<'de>, - { - self.deserialize_float(visitor) - } - - fn deserialize_char(self, visitor: V) -> Result - where - V: Visitor<'de>, - { - match *self.content { - Content::Char(v) => visitor.visit_char(v), - Content::String(ref v) => visitor.visit_str(v), - Content::Str(v) => visitor.visit_borrowed_str(v), - _ => Err(self.invalid_type(&visitor)), - } - } - - fn deserialize_str(self, visitor: V) -> Result - where - V: Visitor<'de>, - { - match *self.content { - Content::String(ref v) => visitor.visit_str(v), - Content::Str(v) => visitor.visit_borrowed_str(v), - Content::ByteBuf(ref v) => visitor.visit_bytes(v), - Content::Bytes(v) => visitor.visit_borrowed_bytes(v), - _ => Err(self.invalid_type(&visitor)), - } - } - - fn deserialize_string(self, visitor: V) -> Result - where - V: Visitor<'de>, - { - self.deserialize_str(visitor) - } - - fn deserialize_bytes(self, visitor: V) -> Result - where - V: Visitor<'de>, - { - match *self.content { - Content::String(ref v) => visitor.visit_str(v), - Content::Str(v) => visitor.visit_borrowed_str(v), - Content::ByteBuf(ref v) => visitor.visit_bytes(v), - Content::Bytes(v) => visitor.visit_borrowed_bytes(v), - Content::Seq(ref v) => visit_content_seq_ref(v, visitor), - _ => Err(self.invalid_type(&visitor)), - } - } - - fn deserialize_byte_buf(self, visitor: V) -> Result - where - V: Visitor<'de>, - { - self.deserialize_bytes(visitor) - } - fn deserialize_option(self, visitor: V) -> Result where V: Visitor<'de>, @@ -1924,27 +1496,6 @@ mod content { } } - fn deserialize_unit(self, visitor: V) -> Result - where - V: Visitor<'de>, - { - match *self.content { - Content::Unit => visitor.visit_unit(), - _ => Err(self.invalid_type(&visitor)), - } - } - - fn deserialize_unit_struct( - self, - _name: &'static str, - visitor: V, - ) -> Result - where - V: Visitor<'de>, - { - self.deserialize_unit(visitor) - } - fn deserialize_newtype_struct(self, _name: &str, visitor: V) -> Result where V: Visitor<'de>, @@ -1968,61 +1519,6 @@ mod content { } } - fn deserialize_seq(self, visitor: V) -> Result - where - V: Visitor<'de>, - { - match *self.content { - Content::Seq(ref v) => visit_content_seq_ref(v, visitor), - _ => Err(self.invalid_type(&visitor)), - } - } - - fn deserialize_tuple(self, _len: usize, visitor: V) -> Result - where - V: Visitor<'de>, - { - self.deserialize_seq(visitor) - } - - fn deserialize_tuple_struct( - self, - _name: &'static str, - _len: usize, - visitor: V, - ) -> Result - where - V: Visitor<'de>, - { - self.deserialize_seq(visitor) - } - - fn deserialize_map(self, visitor: V) -> Result - where - V: Visitor<'de>, - { - match *self.content { - Content::Map(ref v) => visit_content_map_ref(v, visitor), - _ => Err(self.invalid_type(&visitor)), - } - } - - fn deserialize_struct( - self, - _name: &'static str, - _fields: &'static [&'static str], - visitor: V, - ) -> Result - where - V: Visitor<'de>, - { - match *self.content { - Content::Seq(ref v) => visit_content_seq_ref(v, visitor), - Content::Map(ref v) => visit_content_map_ref(v, visitor), - _ => Err(self.invalid_type(&visitor)), - } - } - fn deserialize_enum( self, _name: &str, @@ -2033,6 +1529,9 @@ mod content { V: Visitor<'de>, { let (variant, value) = match *self.content { + // Covered by tests/test_annotations.rs + // test_partially_untagged_enum + // test_partially_untagged_enum_desugared Content::Map(ref value) => { let mut iter = value.iter(); let (variant, value) = match iter.next() { @@ -2053,37 +1552,26 @@ mod content { } (variant, Some(value)) } + // String() covered by tests/test_enum_adjacently_tagged.rs + // partially_untagged + // String() covered by tests/test_enum_untagged.rs + // newtype_enum ref s @ Content::String(_) | ref s @ Content::Str(_) => (s, None), - ref other => { - return Err(de::Error::invalid_type( - other.unexpected(), - &"string or map", - )); - } + // Bool() covered by tests/test_annotations.rs + // test_partially_untagged_enum_generic + // U32() covered by tests/test_annotations.rs + // test_partially_untagged_enum_desugared + // Seq() covered by tests/test_annotations.rs + // test_partially_untagged_enum + _ => return self.deserialize_any(visitor), }; visitor.visit_enum(EnumRefDeserializer { - variant, - value, - err: PhantomData, + variant: ContentRefDeserializer::new(variant), + value: value.map(ContentRefDeserializer::new), }) } - fn deserialize_identifier(self, visitor: V) -> Result - where - V: Visitor<'de>, - { - match *self.content { - Content::String(ref v) => visitor.visit_str(v), - Content::Str(v) => visitor.visit_borrowed_str(v), - Content::ByteBuf(ref v) => visitor.visit_bytes(v), - Content::Bytes(v) => visitor.visit_borrowed_bytes(v), - Content::U8(v) => visitor.visit_u8(v), - Content::U64(v) => visitor.visit_u64(v), - _ => Err(self.invalid_type(&visitor)), - } - } - fn deserialize_ignored_any(self, visitor: V) -> Result where V: Visitor<'de>, @@ -2099,6 +1587,16 @@ mod content { where V: Visitor<'de, Value = Content<'de>>, { + // Covered by tests/test_annotations.rs + // flatten::enum_::internally_tagged::structs + // flatten::enum_::internally_tagged::unit_enum_with_unknown_fields + // flatten::enum_::untagged::straightforward + // flatten::enum_::untagged::struct_ + // test_partially_untagged_enum + // Covered by tests/test_enum_internally_tagged.rs + // untagged_variant + // Covered by tests/test_enum_untagged.rs + // contains_flatten_with_integer_key let _ = visitor; Ok(self.content.clone()) } @@ -2126,9 +1624,8 @@ mod content { where E: de::Error, { - variant: &'a Content<'de>, - value: Option<&'a Content<'de>>, - err: PhantomData, + variant: ContentRefDeserializer<'a, 'de, E>, + value: Option>, } impl<'de, 'a, E> de::EnumAccess<'de> for EnumRefDeserializer<'a, 'de, E> @@ -2142,12 +1639,8 @@ mod content { where V: de::DeserializeSeed<'de>, { - let visitor = VariantRefDeserializer { - value: self.value, - err: PhantomData, - }; - seed.deserialize(ContentRefDeserializer::new(self.variant)) - .map(|v| (v, visitor)) + let visitor = VariantRefDeserializer { de: self.value }; + seed.deserialize(self.variant).map(|v| (v, visitor)) } } @@ -2155,8 +1648,7 @@ mod content { where E: de::Error, { - value: Option<&'a Content<'de>>, - err: PhantomData, + de: Option>, } impl<'de, 'a, E> de::VariantAccess<'de> for VariantRefDeserializer<'a, 'de, E> @@ -2166,10 +1658,10 @@ mod content { type Error = E; fn unit_variant(self) -> Result<(), E> { - match self.value { - Some(value) => de::Deserialize::deserialize(ContentRefDeserializer::new(value)), - // Covered by tests/test_annotations.rs - // test_partially_untagged_adjacently_tagged_enum + match self.de { + Some(de) => de::Deserialize::deserialize(de), + // Covered by tests/test_enum_adjacently_tagged.rs + // partially_untagged // Covered by tests/test_enum_untagged.rs // newtype_enum::unit None => Ok(()), @@ -2180,17 +1672,14 @@ mod content { where T: de::DeserializeSeed<'de>, { - match self.value { + match self.de { // Covered by tests/test_annotations.rs // test_partially_untagged_enum_desugared // test_partially_untagged_enum_generic // Covered by tests/test_enum_untagged.rs // newtype_enum::newtype - Some(value) => seed.deserialize(ContentRefDeserializer::new(value)), - None => Err(de::Error::invalid_type( - de::Unexpected::UnitVariant, - &"newtype variant", - )), + Some(de) => seed.deserialize(de), + None => seed.deserialize(UnitDeserializer::new()), } } @@ -2198,22 +1687,15 @@ mod content { where V: de::Visitor<'de>, { - match self.value { - // Covered by tests/test_annotations.rs + match self.de { + // Seq() covered by tests/test_annotations.rs // test_partially_untagged_enum // test_partially_untagged_enum_desugared - // Covered by tests/test_enum_untagged.rs + // Seq() covered by tests/test_enum_untagged.rs // newtype_enum::tuple0 // newtype_enum::tuple2 - Some(Content::Seq(v)) => visit_content_seq_ref(v, visitor), - Some(other) => Err(de::Error::invalid_type( - other.unexpected(), - &"tuple variant", - )), - None => Err(de::Error::invalid_type( - de::Unexpected::UnitVariant, - &"tuple variant", - )), + Some(de) => de.deserialize_any(visitor), + None => visitor.visit_unit(), } } @@ -2225,22 +1707,14 @@ mod content { where V: de::Visitor<'de>, { - match self.value { - // Covered by tests/test_enum_untagged.rs + match self.de { + // Map() covered by tests/test_enum_untagged.rs // newtype_enum::struct_from_map - Some(Content::Map(v)) => visit_content_map_ref(v, visitor), - // Covered by tests/test_enum_untagged.rs + // Seq() covered by tests/test_enum_untagged.rs // newtype_enum::struct_from_seq // newtype_enum::empty_struct_from_seq - Some(Content::Seq(v)) => visit_content_seq_ref(v, visitor), - Some(other) => Err(de::Error::invalid_type( - other.unexpected(), - &"struct variant", - )), - None => Err(de::Error::invalid_type( - de::Unexpected::UnitVariant, - &"struct variant", - )), + Some(de) => de.deserialize_any(visitor), + None => visitor.visit_unit(), } } } @@ -2543,7 +2017,10 @@ where { for entry in self.0 { if let Some((key, value)) = flat_map_take_entry(entry, variants) { - return visitor.visit_enum(EnumDeserializer::new(key, Some(value))); + return visitor.visit_enum(FlatEnumDeserializer { + variant: ContentDeserializer::new(key), + value: ContentDeserializer::new(value), + }); } } @@ -2751,6 +2228,90 @@ fn flat_map_take_entry<'de>( } } +#[cfg(any(feature = "std", feature = "alloc"))] +struct FlatEnumDeserializer<'de, E> +where + E: Error, +{ + variant: ContentDeserializer<'de, E>, + value: ContentDeserializer<'de, E>, +} + +#[cfg(any(feature = "std", feature = "alloc"))] +impl<'de, E> EnumAccess<'de> for FlatEnumDeserializer<'de, E> +where + E: Error, +{ + type Error = E; + type Variant = FlatVariantDeserializer<'de, Self::Error>; + + fn variant_seed(self, seed: V) -> Result<(V::Value, Self::Variant), E> + where + V: DeserializeSeed<'de>, + { + // Covered by tests/test_annotations.rs + // flatten::enum_::externally_tagged::newtype + // flatten::enum_::externally_tagged::tuple + // flatten::enum_::externally_tagged::struct_from_map + // flatten::enum_::externally_tagged::struct_from_seq + let visitor = FlatVariantDeserializer { de: self.value }; + seed.deserialize(self.variant).map(|v| (v, visitor)) + } +} + +#[cfg(any(feature = "std", feature = "alloc"))] +struct FlatVariantDeserializer<'de, E> +where + E: Error, +{ + de: ContentDeserializer<'de, E>, +} + +#[cfg(any(feature = "std", feature = "alloc"))] +impl<'de, E> VariantAccess<'de> for FlatVariantDeserializer<'de, E> +where + E: Error, +{ + type Error = E; + + fn unit_variant(self) -> Result<(), E> { + Deserialize::deserialize(self.de) + } + + fn newtype_variant_seed(self, seed: T) -> Result + where + T: DeserializeSeed<'de>, + { + // Covered by tests/test_annotations.rs + // flatten::enum_::externally_tagged::newtype + seed.deserialize(self.de) + } + + fn tuple_variant(self, _len: usize, visitor: V) -> Result + where + V: Visitor<'de>, + { + // Covered by tests/test_annotations.rs + // flatten::enum_::externally_tagged::tuple + self.de.deserialize_any(visitor) + } + + fn struct_variant( + self, + _fields: &'static [&'static str], + visitor: V, + ) -> Result + where + V: Visitor<'de>, + { + // Map() covered by tests/test_annotations.rs + // flatten::enum_::externally_tagged::struct_from_map + // Seq() covered by tests/test_annotations.rs + // flatten::enum_::externally_tagged::struct_from_seq + self.de.deserialize_any(visitor) + } +} + pub struct AdjacentlyTaggedEnumVariantSeed { pub enum_name: &'static str, pub variants: &'static [&'static str], diff --git a/serde_derive/src/de.rs b/serde_derive/src/de.rs index 518f84320..2f3bf7289 100644 --- a/serde_derive/src/de.rs +++ b/serde_derive/src/de.rs @@ -1067,10 +1067,10 @@ fn deserialize_struct( _serde::de::VariantAccess::struct_variant(__variant, FIELDS, #visitor_expr) }, StructForm::InternallyTagged(_, deserializer) => quote! { - _serde::Deserializer::deserialize_any(#deserializer, #visitor_expr) + _serde::Deserializer::deserialize_map(#deserializer, #visitor_expr) }, StructForm::Untagged(_, deserializer) => quote! { - _serde::Deserializer::deserialize_any(#deserializer, #visitor_expr) + _serde::Deserializer::deserialize_map(#deserializer, #visitor_expr) }, }; @@ -1402,7 +1402,7 @@ fn deserialize_internally_tagged_enum( #variants_stmt - let (__tag, __content) = _serde::Deserializer::deserialize_any( + let (__tag, __content) = _serde::Deserializer::deserialize_map( __deserializer, _serde::__private::de::TaggedContentVisitor::<__Field>::new(#tag, #expecting))?; let __deserializer = _serde::__private::de::ContentDeserializer::<__D::Error>::new(__content); @@ -1846,8 +1846,11 @@ fn deserialize_internally_tagged_variant( cattrs: &attr::Container, deserializer: TokenStream, ) -> Fragment { - if variant.attrs.deserialize_with().is_some() { - return deserialize_untagged_variant(params, variant, cattrs, deserializer); + if let Some(path) = variant.attrs.deserialize_with() { + let unwrap_fn = unwrap_to_variant_closure(params, variant, false); + return quote_block! { + _serde::__private::Result::map(#path(#deserializer), #unwrap_fn) + }; } let variant_ident = &variant.ident; @@ -1862,7 +1865,7 @@ fn deserialize_internally_tagged_variant( quote!((#default)) }); quote_block! { - _serde::Deserializer::deserialize_any(#deserializer, _serde::__private::de::InternallyTaggedUnitVisitor::new(#type_name, #variant_name))?; + _serde::Deserializer::deserialize_map(#deserializer, _serde::__private::de::InternallyTaggedUnitVisitor::new(#type_name, #variant_name))?; _serde::__private::Ok(#this_value::#variant_ident #default) } } @@ -1907,7 +1910,7 @@ fn deserialize_untagged_variant( quote!((#default)) }); quote_expr! { - match _serde::Deserializer::deserialize_any( + match _serde::Deserializer::deserialize_unit( #deserializer, _serde::__private::de::UntaggedUnitVisitor::new(#type_name, #variant_name) ) { diff --git a/test_suite/tests/test_annotations.rs b/test_suite/tests/test_annotations.rs index 685bf329d..3b64c8a15 100644 --- a/test_suite/tests/test_annotations.rs +++ b/test_suite/tests/test_annotations.rs @@ -1763,36 +1763,6 @@ fn test_partially_untagged_enum_desugared() { ); } -#[test] -fn test_partially_untagged_internally_tagged_enum() { - #[derive(Serialize, Deserialize, PartialEq, Debug)] - #[serde(tag = "t")] - enum Data { - A, - B, - #[serde(untagged)] - Var(u32), - } - - let data = Data::A; - - assert_de_tokens( - &data, - &[ - Token::Map { len: None }, - Token::Str("t"), - Token::Str("A"), - Token::MapEnd, - ], - ); - - let data = Data::Var(42); - - assert_de_tokens(&data, &[Token::U32(42)]); - - // TODO test error output -} - #[test] fn test_transparent_struct() { #[derive(Serialize, Deserialize, PartialEq, Debug)] @@ -2974,4 +2944,81 @@ mod flatten { } } } + + // Reaches crate::private::de::content::ContentDeserializer::deserialize_option + mod with_optional_field { + use super::*; + + #[derive(Debug, PartialEq, Serialize, Deserialize)] + struct Outer { + #[serde(flatten)] + inner: Inner, + } + + #[derive(Debug, PartialEq, Serialize, Deserialize)] + struct Inner { + optional: Option, + } + + #[test] + fn some() { + assert_tokens( + &Outer { + inner: Inner { optional: Some(42) }, + }, + &[ + Token::Map { len: None }, + Token::Str("optional"), + Token::Some, + Token::U32(42), + Token::MapEnd, + ], + ); + } + + #[test] + fn some_without_marker() { + assert_de_tokens( + &Outer { + inner: Inner { optional: Some(42) }, + }, + &[ + Token::Map { len: None }, + Token::Str("optional"), + Token::U32(42), + Token::MapEnd, + ], + ); + } + + #[test] + fn none() { + assert_tokens( + &Outer { + inner: Inner { optional: None }, + }, + &[ + Token::Map { len: None }, + Token::Str("optional"), + Token::None, + Token::MapEnd, + ], + ); + } + + #[test] + fn unit() { + assert_de_tokens( + &Outer { + inner: Inner { optional: None }, + }, + &[ + Token::Map { len: None }, + Token::Str("optional"), + Token::Unit, + Token::MapEnd, + ], + ); + } + } } diff --git a/test_suite/tests/test_enum_adjacently_tagged.rs b/test_suite/tests/test_enum_adjacently_tagged.rs index 0bcdbc39e..beddff45f 100644 --- a/test_suite/tests/test_enum_adjacently_tagged.rs +++ b/test_suite/tests/test_enum_adjacently_tagged.rs @@ -410,6 +410,8 @@ mod newtype { } } +// Reaches crate::private::de::content::ContentDeserializer::deserialize_newtype_struct +// in Content::Newtype case #[test] fn newtype_with_newtype() { #[derive(Debug, PartialEq, Serialize, Deserialize)] diff --git a/test_suite/tests/test_enum_internally_tagged.rs b/test_suite/tests/test_enum_internally_tagged.rs index b4d428c4d..ed666ed98 100644 --- a/test_suite/tests/test_enum_internally_tagged.rs +++ b/test_suite/tests/test_enum_internally_tagged.rs @@ -157,6 +157,23 @@ fn newtype_unit() { Token::StructEnd, ], ); + + assert_de_tokens( + &value, + &[ + Token::Seq { len: Some(1) }, + Token::Str("NewtypeUnit"), // tag + Token::SeqEnd, + ], + ); + assert_de_tokens( + &value, + &[ + Token::Seq { len: Some(1) }, + Token::BorrowedStr("NewtypeUnit"), // tag + Token::SeqEnd, + ], + ); } #[test] @@ -225,6 +242,8 @@ fn newtype_unit_struct() { ); } +// Reaches crate::private::de::content::ContentDeserializer::deserialize_newtype_struct +// in _ case #[test] fn newtype_newtype() { assert_tokens( @@ -1064,6 +1083,36 @@ fn wrong_tag() { ); } +#[test] +fn partially_untagged() { + #[derive(Debug, PartialEq, Serialize, Deserialize)] + #[serde(tag = "tag")] + enum Data { + A, + B, + #[serde(untagged)] + Var(u32), + } + + let data = Data::A; + + assert_de_tokens( + &data, + &[ + Token::Map { len: None }, + Token::Str("tag"), + Token::Str("A"), + Token::MapEnd, + ], + ); + + let data = Data::Var(42); + + assert_de_tokens(&data, &[Token::U32(42)]); + + // TODO test error output +} + #[test] fn untagged_variant() { #[derive(Debug, PartialEq, Serialize, Deserialize)]