Fix deserialize_with on a struct variant with one field

This commit is contained in:
David Tolnay 2018-04-12 23:44:53 -07:00
parent 9f38ca032e
commit 202c10147e
No known key found for this signature in database
GPG Key ID: F9BA143B95FF6D82
2 changed files with 28 additions and 12 deletions

View File

@ -2598,6 +2598,14 @@ fn wrap_deserialize_variant_with(
})
});
let unwrap_fn = match variant.style {
Style::Struct if variant.fields.len() == 1 => {
let field_ident = variant.fields[0].ident.unwrap();
quote!({
|__wrap| {
#this::#variant_ident { #field_ident: __wrap.value }
}
})
}
Style::Struct => {
let field_idents = variant
.fields

View File

@ -391,9 +391,6 @@ fn test_gen() {
#[derive(Serialize, Deserialize)]
enum ExternallyTaggedVariantWith {
#[allow(dead_code)]
Normal { f1: String },
#[serde(serialize_with = "ser_x")]
#[serde(deserialize_with = "de_x")]
#[allow(dead_code)]
@ -404,6 +401,11 @@ fn test_gen() {
#[allow(dead_code)]
Tuple(String, u8),
#[serde(serialize_with = "ser_x")]
#[serde(deserialize_with = "de_x")]
#[allow(dead_code)]
Struct1 { x: X },
#[serde(serialize_with = "serialize_some_other_variant")]
#[serde(deserialize_with = "deserialize_some_other_variant")]
#[allow(dead_code)]
@ -419,14 +421,16 @@ fn test_gen() {
#[derive(Serialize, Deserialize)]
#[serde(tag = "t")]
enum InternallyTaggedVariantWith {
#[allow(dead_code)]
Normal { f1: String },
#[serde(serialize_with = "ser_x")]
#[serde(deserialize_with = "de_x")]
#[allow(dead_code)]
Newtype(X),
#[serde(serialize_with = "ser_x")]
#[serde(deserialize_with = "de_x")]
#[allow(dead_code)]
Struct1 { x: X },
#[serde(serialize_with = "serialize_some_other_variant")]
#[serde(deserialize_with = "deserialize_some_other_variant")]
#[allow(dead_code)]
@ -442,9 +446,6 @@ fn test_gen() {
#[derive(Serialize, Deserialize)]
#[serde(tag = "t", content = "c")]
enum AdjacentlyTaggedVariantWith {
#[allow(dead_code)]
Normal { f1: String },
#[serde(serialize_with = "ser_x")]
#[serde(deserialize_with = "de_x")]
#[allow(dead_code)]
@ -455,6 +456,11 @@ fn test_gen() {
#[allow(dead_code)]
Tuple(String, u8),
#[serde(serialize_with = "ser_x")]
#[serde(deserialize_with = "de_x")]
#[allow(dead_code)]
Struct1 { x: X },
#[serde(serialize_with = "serialize_some_other_variant")]
#[serde(deserialize_with = "deserialize_some_other_variant")]
#[allow(dead_code)]
@ -470,9 +476,6 @@ fn test_gen() {
#[derive(Serialize, Deserialize)]
#[serde(untagged)]
enum UntaggedVariantWith {
#[allow(dead_code)]
Normal { f1: String },
#[serde(serialize_with = "ser_x")]
#[serde(deserialize_with = "de_x")]
#[allow(dead_code)]
@ -483,6 +486,11 @@ fn test_gen() {
#[allow(dead_code)]
Tuple(String, u8),
#[serde(serialize_with = "ser_x")]
#[serde(deserialize_with = "de_x")]
#[allow(dead_code)]
Struct1 { x: X },
#[serde(serialize_with = "serialize_some_other_variant")]
#[serde(deserialize_with = "deserialize_some_other_variant")]
#[allow(dead_code)]