Format variant-skip message only if variant is skipped

This commit is contained in:
David Tolnay
2016-12-24 13:10:06 -05:00
parent 2401ae61a8
commit 28d67f4172
+7 -17
View File
@@ -256,29 +256,19 @@ fn serialize_variant(
let variant_ident = variant.ident.clone(); let variant_ident = variant.ident.clone();
let variant_name = variant.attrs.name().serialize_name(); let variant_name = variant.attrs.name().serialize_name();
if variant.attrs.skip_serializing() {
let skipped_msg = format!("The enum variant {}::{} cannot be serialized", let skipped_msg = format!("The enum variant {}::{} cannot be serialized",
type_ident, variant_ident); type_ident, variant_ident);
let skipped_err = quote! { let skipped_err = quote! {
Err(_serde::ser::Error::invalid_value(#skipped_msg)) Err(_serde::ser::Error::invalid_value(#skipped_msg))
}; };
let fields_pat = match variant.style {
if variant.attrs.skip_serializing() { Style::Unit => quote!(),
match variant.style { Style::Newtype | Style::Tuple => quote!( (..) ),
Style::Unit => { Style::Struct => quote!( {..} ),
};
quote! { quote! {
#type_ident::#variant_ident => #skipped_err, #type_ident::#variant_ident #fields_pat => #skipped_err,
}
},
Style::Newtype | Style::Tuple => {
quote! {
#type_ident::#variant_ident(..) => #skipped_err,
}
},
Style::Struct => {
quote! {
#type_ident::#variant_ident{..} => #skipped_err,
}
}
} }
} else { // variant wasn't skipped } else { // variant wasn't skipped
match variant.style { match variant.style {