From fcbb3d37832002b6c1de31e43707ed921ae80e08 Mon Sep 17 00:00:00 2001 From: Jake Kiesel Date: Sat, 7 Sep 2019 11:43:53 -0600 Subject: [PATCH] Add support for other enum representations --- serde_derive/src/de.rs | 17 ++++++++++++++++- serde_derive/src/ser.rs | 34 +++++++++++++++++++++++----------- 2 files changed, 39 insertions(+), 12 deletions(-) diff --git a/serde_derive/src/de.rs b/serde_derive/src/de.rs index 9f3b0319..d54309c8 100644 --- a/serde_derive/src/de.rs +++ b/serde_derive/src/de.rs @@ -1698,7 +1698,7 @@ fn deserialize_externally_tagged_variant( attr::Default::Path(ref path) => quote!( #path() ), - _ => unimplemented!(), + attr::Default::None => unimplemented!(), }; @@ -1857,6 +1857,21 @@ fn deserialize_untagged_newtype_variant( let field_ty = field.ty; match field.attrs.deserialize_with() { None => { + if field.attrs.skip_deserializing() { + let let_default = match field.attrs.default() { + attr::Default::Default => quote!( + _serde::export::Default::default() + ), + attr::Default::Path(ref path) => quote!( + #path() + ), + attr::Default::None => unimplemented!(), + }; + + return quote_expr! { + _serde::export::Ok(#this::#variant_ident(#let_default)) + }; + } let span = field.original.span(); let func = quote_spanned!(span=> <#field_ty as _serde::Deserialize>::deserialize); quote_expr! { diff --git a/serde_derive/src/ser.rs b/serde_derive/src/ser.rs index 9b532beb..e79b8e7e 100644 --- a/serde_derive/src/ser.rs +++ b/serde_derive/src/ser.rs @@ -608,6 +608,15 @@ fn serialize_internally_tagged_variant( } Style::Newtype => { let field = &variant.fields[0]; + if field.attrs.skip_serializing() { + return quote_block! { + let mut __struct = try!(_serde::Serializer::serialize_struct( + __serializer, #type_name, 1)); + try!(_serde::ser::SerializeStruct::serialize_field( + &mut __struct, #tag, #variant_name)); + _serde::ser::SerializeStruct::end(__struct) + }; + } let mut field_expr = quote!(__field0); if let Some(path) = field.attrs.serialize_with() { field_expr = wrap_serialize_field_with(params, field.ty, path, &field_expr); @@ -668,6 +677,15 @@ fn serialize_adjacently_tagged_variant( } Style::Newtype => { let field = &variant.fields[0]; + if field.attrs.skip_serializing() { + return quote_block! { + let mut __struct = try!(_serde::Serializer::serialize_struct( + __serializer, #type_name, 1)); + try!(_serde::ser::SerializeStruct::serialize_field( + &mut __struct, #tag, #variant_name)); + _serde::ser::SerializeStruct::end(__struct) + }; + } let mut field_expr = quote!(__field0); if let Some(path) = field.attrs.serialize_with() { field_expr = wrap_serialize_field_with(params, field.ty, path, &field_expr); @@ -771,6 +789,11 @@ fn serialize_untagged_variant( } Style::Newtype => { let field = &variant.fields[0]; + if field.attrs.skip_serializing() { + return quote_expr! { + _serde::Serializer::serialize_unit(__serializer) + }; + } let mut field_expr = quote!(__field0); if let Some(path) = field.attrs.serialize_with() { field_expr = wrap_serialize_field_with(params, field.ty, path, &field_expr); @@ -835,12 +858,6 @@ fn serialize_tuple_variant( variant_index, variant_name, } => { - if len.to_string() == "0" { - return quote_expr! { - _serde::Serializer::serialize_unit_variant(__serializer, #type_name, #variant_index, #variant_name) - } - } - quote_block! { let #let_mut __serde_state = try!(_serde::Serializer::serialize_tuple_variant( __serializer, @@ -853,11 +870,6 @@ fn serialize_tuple_variant( } } TupleVariant::Untagged => { - if len.to_string() == "0" { - return quote_expr! { - _serde::Serializer::serialize_unit(__serializer) - } - } quote_block! { let #let_mut __serde_state = try!(_serde::Serializer::serialize_tuple( __serializer,