From 84d1c5385ddefb1bb89feff596f5810d9293168b Mon Sep 17 00:00:00 2001 From: Mingun Date: Mon, 7 Aug 2023 02:53:24 +0500 Subject: [PATCH] Use correct tokens in test to represent an enum variant Unit variant of externally tagged enum cannot be deserialized from the string token by itself. It is ContentDeserializer + serde_test::Deserializer that makes this possible, because serde_test::Deserializer produces Content::Str() from Token::BorrowedStr() and ContentDeserializer produces unit variant from Content::Str(). The following tokens all produces Content::String(variant): - Token::String(variant) - Token::Str(variant) - Token::UnitVariant { variant, .. } Token::BorrowedStr(variant) produces Content::Str(variant) that was the real purpose to use it in test in #933. This actually makes this test testing `Content` rather than type itself. Correct way to represent enum one of: - [xxxVariant { .. }] - [Enum { .. }, xxxVariant { variant, .. }] - [Enum { .. }, String(variant), ] - [Enum { .. }, Str(variant), ] - [Enum { .. }, BorrowedStr(variant), ] --- serde/src/private/de.rs | 7 +++++++ test_suite/tests/test_macros.rs | 6 ++++++ 2 files changed, 13 insertions(+) diff --git a/serde/src/private/de.rs b/serde/src/private/de.rs index 883e6909..c402d2c6 100644 --- a/serde/src/private/de.rs +++ b/serde/src/private/de.rs @@ -313,6 +313,8 @@ mod content { } } + /// Used to capture data in [`Content`] from other deserializers. + /// Cannot capture externally tagged enums, `i128` and `u128`. struct ContentVisitor<'de> { value: PhantomData>, } @@ -528,6 +530,8 @@ mod content { Content(Content<'de>), } + /// Serves as a seed for deserializing a key of internally tagged enum. + /// Cannot capture externally tagged enums, `i128` and `u128`. struct TagOrContentVisitor<'de> { name: &'static str, value: PhantomData>, @@ -814,6 +818,9 @@ mod content { /// Used by generated code to deserialize an internally tagged enum. /// + /// Captures map or sequence from the original deserializer and searches + /// a tag in it (in case of sequence, tag is the first element of sequence). + /// /// Not public API. pub struct TaggedContentVisitor { tag_name: &'static str, diff --git a/test_suite/tests/test_macros.rs b/test_suite/tests/test_macros.rs index 2b7d189f..b9e54301 100644 --- a/test_suite/tests/test_macros.rs +++ b/test_suite/tests/test_macros.rs @@ -997,7 +997,9 @@ fn test_internally_tagged_struct_variant_containing_unit_variant() { Token::Str("action"), Token::Str("Log"), Token::Str("level"), + Token::Enum { name: "Level" }, Token::BorrowedStr("Info"), + Token::Unit, Token::StructEnd, ], ); @@ -1009,7 +1011,9 @@ fn test_internally_tagged_struct_variant_containing_unit_variant() { Token::Str("action"), Token::Str("Log"), Token::Str("level"), + Token::Enum { name: "Level" }, Token::BorrowedStr("Info"), + Token::Unit, Token::MapEnd, ], ); @@ -1019,7 +1023,9 @@ fn test_internally_tagged_struct_variant_containing_unit_variant() { &[ Token::Seq { len: Some(2) }, Token::Str("Log"), + Token::Enum { name: "Level" }, Token::BorrowedStr("Info"), + Token::Unit, Token::SeqEnd, ], );