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), <variant content>]
- [Enum { .. }, Str(variant), <variant content>]
- [Enum { .. }, BorrowedStr(variant), <variant content>]
This commit is contained in:
Mingun 2023-08-07 02:53:24 +05:00 committed by Mingun
parent 05a5b7e3c6
commit 84d1c5385d
2 changed files with 13 additions and 0 deletions

View File

@ -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<Content<'de>>,
}
@ -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<TagOrContent<'de>>,
@ -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<T> {
tag_name: &'static str,

View File

@ -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,
],
);