Skip to content

Commit

Permalink
Merge pull request 2475 from Baptistemontan/master
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay committed Jul 9, 2023
2 parents 09b78b2 + c604bdb commit 7aa0453
Show file tree
Hide file tree
Showing 2 changed files with 600 additions and 2 deletions.
42 changes: 40 additions & 2 deletions serde/src/private/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -894,7 +894,7 @@ mod content {
where
D: Deserializer<'de>,
{
deserializer.deserialize_str(self)
deserializer.deserialize_identifier(self)
}
}

Expand All @@ -905,6 +905,20 @@ mod content {
write!(formatter, "{:?} or {:?}", 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>
where
E: de::Error,
Expand All @@ -917,6 +931,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 @@ -942,7 +969,7 @@ mod content {
where
D: Deserializer<'de>,
{
deserializer.deserialize_str(self)
deserializer.deserialize_identifier(self)
}
}

Expand All @@ -957,6 +984,17 @@ mod 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
Loading

0 comments on commit 7aa0453

Please sign in to comment.