Skip to content

Commit

Permalink
Merge pull request #2377 from mfro/master
Browse files Browse the repository at this point in the history
Allow bytes for adjacently tagged enums
  • Loading branch information
dtolnay committed May 5, 2023
2 parents 624879c + a803ec1 commit b5d68ae
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
13 changes: 13 additions & 0 deletions serde/src/private/de.rs
Expand Up @@ -990,6 +990,19 @@ mod content {
Ok(TagContentOtherField::Other)
}
}

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

/// Not public API
Expand Down
53 changes: 53 additions & 0 deletions test_suite/tests/test_annotations.rs
Expand Up @@ -2317,6 +2317,59 @@ fn test_internally_tagged_enum_new_type_with_unit() {
);
}

#[test]
fn test_adjacently_tagged_enum_bytes() {
#[derive(Serialize, Deserialize, PartialEq, Debug)]
#[serde(tag = "t", content = "c")]
enum Data {
A { a: i32 },
}

let data = Data::A { a: 0 };

assert_tokens(
&data,
&[
Token::Struct {
name: "Data",
len: 2,
},
Token::Str("t"),
Token::Str("A"),
Token::Str("c"),
Token::Struct {
name: "A",
len: 1,
},
Token::Str("a"),
Token::I32(0),
Token::StructEnd,
Token::StructEnd,
],
);

assert_de_tokens(
&data,
&[
Token::Struct {
name: "Data",
len: 2,
},
Token::Bytes(b"t"),
Token::Str("A"),
Token::Bytes(b"c"),
Token::Struct {
name: "A",
len: 1,
},
Token::Str("a"),
Token::I32(0),
Token::StructEnd,
Token::StructEnd,
],
);
}

#[test]
fn test_adjacently_tagged_enum_containing_flatten() {
#[derive(Serialize, Deserialize, PartialEq, Debug)]
Expand Down

0 comments on commit b5d68ae

Please sign in to comment.