Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Deserializer::deserialize_identifier support for adjacently tagged enums #2475

Merged
merged 3 commits into from
Jul 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 46 additions & 4 deletions serde/src/private/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -915,15 +915,33 @@ mod content {
where
D: Deserializer<'de>,
{
deserializer.deserialize_str(self)
deserializer.deserialize_identifier(self)
}
}

impl<'de> Visitor<'de> for TagOrContentFieldVisitor {
type Value = TagOrContentField;

fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
write!(formatter, "{:?} or {:?}", self.tag, self.content)
write!(
formatter,
"{:?}, {:?}, b{0:?}, b{1:?}, 0 or 1",
self.tag, self.content
)
}

fn visit_u64<E>(self, field_index: u64) -> Result<Self::Value, E>
where
E: de::Error,
{
match field_index {
0 => Ok(TagOrContentField::Tag),
1 => Ok(TagOrContentField::Content),
_ => Err(de::Error::invalid_value(
Unexpected::Unsigned(field_index),
&self,
)),
}
}

fn visit_str<E>(self, field: &str) -> Result<Self::Value, E>
Expand All @@ -938,6 +956,19 @@ mod content {
Err(de::Error::invalid_value(Unexpected::Str(field), &self))
}
}

fn visit_bytes<E>(self, field: &[u8]) -> Result<Self::Value, E>
where
E: de::Error,
{
if field == self.tag.as_bytes() {
Ok(TagOrContentField::Tag)
} else if field == self.content.as_bytes() {
Ok(TagOrContentField::Content)
} else {
Err(de::Error::invalid_value(Unexpected::Bytes(field), &self))
}
}
}

/// Used by generated code to deserialize an adjacently tagged enum when
Expand All @@ -963,7 +994,7 @@ mod content {
where
D: Deserializer<'de>,
{
deserializer.deserialize_str(self)
deserializer.deserialize_identifier(self)
}
}

Expand All @@ -973,11 +1004,22 @@ mod content {
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
write!(
formatter,
"{:?}, {:?}, or other ignored fields",
"{:?}, {:?}, b{0:?}, b{1:?}, 0, 1, or other ignored field identifiers",
self.tag, self.content
)
}

fn visit_u64<E>(self, field_index: u64) -> Result<Self::Value, E>
where
E: de::Error,
{
match field_index {
0 => Ok(TagContentOtherField::Tag),
1 => Ok(TagContentOtherField::Content),
_ => Ok(TagContentOtherField::Other),
}
}

fn visit_str<E>(self, field: &str) -> Result<Self::Value, E>
where
E: de::Error,
Expand Down
2 changes: 1 addition & 1 deletion test_suite/tests/test_annotations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3022,7 +3022,7 @@ fn test_expecting_message_adjacently_tagged_enum() {

assert_de_tokens_error::<Enum>(
&[Token::Map { len: None }, Token::Unit],
r#"invalid type: unit value, expected "tag", "content", or other ignored fields"#,
r#"invalid type: unit value, expected "tag", "content", b"tag", b"content", 0, 1, or other ignored field identifiers"#,
);

// Check that #[serde(expecting = "...")] doesn't affect variant identifier error message
Expand Down
Loading