From 397e8716d4b53a67063621920a414b0992bfee28 Mon Sep 17 00:00:00 2001 From: Mingun Date: Sat, 17 Aug 2024 10:46:04 +0500 Subject: [PATCH 1/8] Explain code in ContentDeserializer::deserialize_newtype_struct and add comments about coverage --- serde/src/private/de.rs | 13 +++++++++++++ test_suite/tests/test_enum_adjacently_tagged.rs | 2 ++ test_suite/tests/test_enum_internally_tagged.rs | 2 ++ 3 files changed, 17 insertions(+) diff --git a/serde/src/private/de.rs b/serde/src/private/de.rs index 50ae6ed15..5a129b0a5 100644 --- a/serde/src/private/de.rs +++ b/serde/src/private/de.rs @@ -1373,7 +1373,20 @@ mod content { V: Visitor<'de>, { match self.content { + // 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), } } 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..12a0e48b1 100644 --- a/test_suite/tests/test_enum_internally_tagged.rs +++ b/test_suite/tests/test_enum_internally_tagged.rs @@ -225,6 +225,8 @@ fn newtype_unit_struct() { ); } +// Reaches crate::private::de::content::ContentDeserializer::deserialize_newtype_struct +// in _ case #[test] fn newtype_newtype() { assert_tokens( From 90564e60092926a15542cdd552343833280b1f79 Mon Sep 17 00:00:00 2001 From: Mingun Date: Sat, 17 Aug 2024 10:18:24 +0500 Subject: [PATCH 2/8] Cover all cases in ContentDeserializer::deserialize_option --- serde/src/private/de.rs | 9 ++++ test_suite/tests/test_annotations.rs | 77 ++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+) diff --git a/serde/src/private/de.rs b/serde/src/private/de.rs index 5a129b0a5..2f4687c96 100644 --- a/serde/src/private/de.rs +++ b/serde/src/private/de.rs @@ -1306,10 +1306,19 @@ mod content { 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), } } diff --git a/test_suite/tests/test_annotations.rs b/test_suite/tests/test_annotations.rs index 685bf329d..5a93c9ac8 100644 --- a/test_suite/tests/test_annotations.rs +++ b/test_suite/tests/test_annotations.rs @@ -2974,4 +2974,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, + ], + ); + } + } } From a1e4ce79b34f308014d29f8edffdf61496f63874 Mon Sep 17 00:00:00 2001 From: Mingun Date: Sat, 17 Aug 2024 12:25:10 +0500 Subject: [PATCH 3/8] Move test forgotten in 2cbfd37072a5af4774a77c8d3f7661dd3c1af194 due to resolving conflicts --- test_suite/tests/test_annotations.rs | 30 ------------------- .../tests/test_enum_internally_tagged.rs | 30 +++++++++++++++++++ 2 files changed, 30 insertions(+), 30 deletions(-) diff --git a/test_suite/tests/test_annotations.rs b/test_suite/tests/test_annotations.rs index 5a93c9ac8..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)] diff --git a/test_suite/tests/test_enum_internally_tagged.rs b/test_suite/tests/test_enum_internally_tagged.rs index 12a0e48b1..7214af450 100644 --- a/test_suite/tests/test_enum_internally_tagged.rs +++ b/test_suite/tests/test_enum_internally_tagged.rs @@ -1066,6 +1066,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)] From 6929c5dfd10b9a568f83c478d1cae8679d9bd64e Mon Sep 17 00:00:00 2001 From: Mingun Date: Sun, 11 Aug 2024 02:06:52 +0500 Subject: [PATCH 4/8] Remove confusing call to deserialize_untagged_variant in deserialize_internally_tagged_variant deserialize_untagged_variant in that place is called when deserialzie_with attribute is set. In that case it performs special actions that is better to use explicitly in deserialize_untagged_variant for readability --- serde_derive/src/de.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/serde_derive/src/de.rs b/serde_derive/src/de.rs index 518f84320..5f90dff3d 100644 --- a/serde_derive/src/de.rs +++ b/serde_derive/src/de.rs @@ -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; From ec5f5908f9be45de9cf9c1ee155d321668bd0214 Mon Sep 17 00:00:00 2001 From: Mingun Date: Mon, 7 Aug 2023 21:48:28 +0500 Subject: [PATCH 5/8] Add test for sequence in newtype_unit failures(1): newtype_unit --- test_suite/tests/test_enum_internally_tagged.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/test_suite/tests/test_enum_internally_tagged.rs b/test_suite/tests/test_enum_internally_tagged.rs index 7214af450..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] From bcffa60daff9971f20f4bf992d9ddea2af8bf9f6 Mon Sep 17 00:00:00 2001 From: Mingun Date: Mon, 7 Aug 2023 23:20:53 +0500 Subject: [PATCH 6/8] Unify handling of unit and unit structs in ContentDeserializer --- serde/src/private/de.rs | 44 ++++++++++++++++++++--------------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/serde/src/private/de.rs b/serde/src/private/de.rs index 2f4687c96..563575d5c 100644 --- a/serde/src/private/de.rs +++ b/serde/src/private/de.rs @@ -1327,9 +1327,24 @@ mod content { where V: Visitor<'de>, { + // Covered by tests/test_enum_internally_tagged.rs + // newtype_unit match self.content { - Content::Unit => visitor.visit_unit(), - + // As a special case, allow deserializing untagged newtype + // variant containing unit struct. + // + // #[derive(Deserialize)] + // struct Info; + // + // #[derive(Deserialize)] + // #[serde(tag = "topic")] + // enum Message { + // Info(Info), + // } + // + // 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)] @@ -1340,7 +1355,8 @@ mod content { // // We want {"result":"Success"} to deserialize into Response<()>. Content::Map(ref v) if v.is_empty() => visitor.visit_unit(), - _ => Err(self.invalid_type(&visitor)), + Content::Seq(ref v) if v.is_empty() => visitor.visit_unit(), + _ => self.deserialize_any(visitor), } } @@ -1352,25 +1368,9 @@ mod content { where V: Visitor<'de>, { - match self.content { - // As a special case, allow deserializing untagged newtype - // variant containing unit struct. - // - // #[derive(Deserialize)] - // struct Info; - // - // #[derive(Deserialize)] - // #[serde(tag = "topic")] - // enum Message { - // Info(Info), - // } - // - // We want {"topic":"Info"} to deserialize even though - // ordinarily unit structs do not deserialize from empty map/seq. - 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), - } + // Covered by tests/test_enum_internally_tagged.rs + // newtype_unit_struct + self.deserialize_unit(visitor) } fn deserialize_newtype_struct( From 5292da8b953b1acfeacd3c52f2d1ae4e23fd63c0 Mon Sep 17 00:00:00 2001 From: Mingun Date: Mon, 12 Aug 2024 01:41:12 +0500 Subject: [PATCH 7/8] Consistently use visit_content_seq and visit_content_map when deserialize enums Helper methods visit_content_[seq|map] does the same as [Seq|Map]Deserializer::deserialize_any and used everywhere except here. Reuse them for consistency --- serde/src/private/de.rs | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/serde/src/private/de.rs b/serde/src/private/de.rs index 563575d5c..e4f0b3984 100644 --- a/serde/src/private/de.rs +++ b/serde/src/private/de.rs @@ -1624,9 +1624,7 @@ mod content { V: de::Visitor<'de>, { match self.value { - Some(Content::Seq(v)) => { - de::Deserializer::deserialize_any(SeqDeserializer::new(v.into_iter()), visitor) - } + Some(Content::Seq(v)) => visit_content_seq(v, visitor), Some(other) => Err(de::Error::invalid_type( other.unexpected(), &"tuple variant", @@ -1647,12 +1645,8 @@ mod content { 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(Content::Map(v)) => visit_content_map(v, visitor), + Some(Content::Seq(v)) => visit_content_seq(v, visitor), Some(other) => Err(de::Error::invalid_type( other.unexpected(), &"struct variant", From 9009f631eddfb15b9dba2726c08d79a4516791e6 Mon Sep 17 00:00:00 2001 From: Mingun Date: Sat, 12 Aug 2023 15:17:53 +0500 Subject: [PATCH 8/8] Deserializer should not check correctness of data, this is the responsibility of the Visitor Examples of errors produced during deserialization of internally tagged enums in tests if instead of a Seq/Map a Str("unexpected string") will be provided: In tests/test_annotations.rs flatten::enum_::internally_tagged::tuple: before: `invalid type: string "unexpected string", expected tuple variant` after : `invalid type: string "unexpected string", expected tuple variant Enum::Tuple` flatten::enum_::internally_tagged::struct_from_map: before: `invalid type: string "unexpected string", expected struct variant` after : `invalid type: string "unexpected string", expected struct variant Enum::Struct` --- serde/src/private/de.rs | 683 ++++------------------------------------ 1 file changed, 70 insertions(+), 613 deletions(-) diff --git a/serde/src/private/de.rs b/serde/src/private/de.rs index e4f0b3984..316157aa3 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,139 +1102,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(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>, @@ -1400,61 +1200,6 @@ mod content { } } - 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( - 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)), - } - } - - fn deserialize_struct( - self, - _name: &'static str, - _fields: &'static [&'static 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)), - } - } - fn deserialize_enum( self, _name: &str, @@ -1465,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() { @@ -1485,33 +1235,20 @@ 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 - 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>, @@ -1528,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) } @@ -1612,10 +1351,7 @@ mod content { { match self.value { Some(value) => seed.deserialize(ContentDeserializer::new(value)), - None => Err(de::Error::invalid_type( - de::Unexpected::UnitVariant, - &"newtype variant", - )), + None => seed.deserialize(UnitDeserializer::new()), } } @@ -1624,15 +1360,8 @@ mod content { V: de::Visitor<'de>, { match self.value { - Some(Content::Seq(v)) => visit_content_seq(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(value) => ContentDeserializer::new(value).deserialize_any(visitor), + None => visitor.visit_unit(), } } @@ -1645,16 +1374,8 @@ mod content { V: de::Visitor<'de>, { match self.value { - Some(Content::Map(v)) => visit_content_map(v, visitor), - Some(Content::Seq(v)) => visit_content_seq(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(value) => ContentDeserializer::new(value).deserialize_any(visitor), + None => visitor.visit_unit(), } } } @@ -1665,52 +1386,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, @@ -1754,6 +1429,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>, @@ -1786,139 +1467,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>, @@ -1940,27 +1488,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>, @@ -1984,61 +1511,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, @@ -2049,6 +1521,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() { @@ -2069,13 +1544,18 @@ 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 { @@ -2085,21 +1565,6 @@ mod content { }) } - 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>, @@ -2115,6 +1580,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()) } @@ -2184,8 +1659,8 @@ mod content { 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 + // Covered by tests/test_enum_adjacently_tagged.rs + // partially_untagged // Covered by tests/test_enum_untagged.rs // newtype_enum::unit None => Ok(()), @@ -2203,10 +1678,7 @@ mod content { // 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", - )), + None => seed.deserialize(UnitDeserializer::new()), } } @@ -2215,21 +1687,14 @@ mod content { V: de::Visitor<'de>, { match self.value { - // Covered by tests/test_annotations.rs + // 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(value) => ContentRefDeserializer::new(value).deserialize_any(visitor), + None => visitor.visit_unit(), } } @@ -2242,21 +1707,13 @@ mod content { V: de::Visitor<'de>, { match self.value { - // Covered by tests/test_enum_untagged.rs + // 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(value) => ContentRefDeserializer::new(value).deserialize_any(visitor), + None => visitor.visit_unit(), } } }