mirror of
https://github.com/pezkuwichain/serde.git
synced 2026-06-13 11:21:01 +00:00
Implemented skip_deserializing for enum
This commit is contained in:
@@ -496,6 +496,7 @@ fn deserialize_item_enum(
|
|||||||
|
|
||||||
let variant_visitor = deserialize_field_visitor(
|
let variant_visitor = deserialize_field_visitor(
|
||||||
variants.iter()
|
variants.iter()
|
||||||
|
.filter(|variant| !variant.attrs.skip_deserializing())
|
||||||
.map(|variant| variant.attrs.name().deserialize_name())
|
.map(|variant| variant.attrs.name().deserialize_name())
|
||||||
.collect(),
|
.collect(),
|
||||||
item_attrs,
|
item_attrs,
|
||||||
@@ -518,7 +519,7 @@ fn deserialize_item_enum(
|
|||||||
|
|
||||||
// Match arms to extract a variant from a string
|
// Match arms to extract a variant from a string
|
||||||
let mut variant_arms = vec![];
|
let mut variant_arms = vec![];
|
||||||
for (i, variant) in variants.iter().enumerate() {
|
for (i, variant) in variants.iter().filter(|variant| !variant.attrs.skip_deserializing()).enumerate() {
|
||||||
let variant_name = aster::id(format!("__field{}", i));
|
let variant_name = aster::id(format!("__field{}", i));
|
||||||
let variant_name = quote!(__Field::#variant_name);
|
let variant_name = quote!(__Field::#variant_name);
|
||||||
|
|
||||||
|
|||||||
@@ -187,12 +187,14 @@ impl Item {
|
|||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Variant {
|
pub struct Variant {
|
||||||
name: Name,
|
name: Name,
|
||||||
|
skip_deserializing: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Variant {
|
impl Variant {
|
||||||
pub fn from_ast(cx: &Ctxt, variant: &syn::Variant) -> Self {
|
pub fn from_ast(cx: &Ctxt, variant: &syn::Variant) -> Self {
|
||||||
let mut ser_name = Attr::none(cx, "rename");
|
let mut ser_name = Attr::none(cx, "rename");
|
||||||
let mut de_name = Attr::none(cx, "rename");
|
let mut de_name = Attr::none(cx, "rename");
|
||||||
|
let mut skip_deserializing = BoolAttr::none(cx, "skip_deserializing");
|
||||||
|
|
||||||
for meta_items in variant.attrs.iter().filter_map(get_serde_meta_items) {
|
for meta_items in variant.attrs.iter().filter_map(get_serde_meta_items) {
|
||||||
for meta_item in meta_items {
|
for meta_item in meta_items {
|
||||||
@@ -212,6 +214,10 @@ impl Variant {
|
|||||||
de_name.set_opt(de);
|
de_name.set_opt(de);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// Parse `#[serde(skip_deserializing)]`
|
||||||
|
MetaItem(Word(ref name)) if name == "skip_deserializing" => {
|
||||||
|
skip_deserializing.set_true();
|
||||||
|
}
|
||||||
|
|
||||||
MetaItem(ref meta_item) => {
|
MetaItem(ref meta_item) => {
|
||||||
cx.error(format!("unknown serde variant attribute `{}`",
|
cx.error(format!("unknown serde variant attribute `{}`",
|
||||||
@@ -230,12 +236,17 @@ impl Variant {
|
|||||||
serialize: ser_name.get().unwrap_or_else(|| variant.ident.to_string()),
|
serialize: ser_name.get().unwrap_or_else(|| variant.ident.to_string()),
|
||||||
deserialize: de_name.get().unwrap_or_else(|| variant.ident.to_string()),
|
deserialize: de_name.get().unwrap_or_else(|| variant.ident.to_string()),
|
||||||
},
|
},
|
||||||
|
skip_deserializing: skip_deserializing.get(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn name(&self) -> &Name {
|
pub fn name(&self) -> &Name {
|
||||||
&self.name
|
&self.name
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn skip_deserializing(&self) -> bool {
|
||||||
|
self.skip_deserializing
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Represents field attribute information
|
/// Represents field attribute information
|
||||||
|
|||||||
@@ -37,7 +37,9 @@ enum Enum {
|
|||||||
Unit,
|
Unit,
|
||||||
Simple(i32),
|
Simple(i32),
|
||||||
Seq(i32, i32, i32),
|
Seq(i32, i32, i32),
|
||||||
Map { a: i32, b: i32, c: i32 }
|
Map { a: i32, b: i32, c: i32 },
|
||||||
|
#[serde(skip_deserializing)]
|
||||||
|
Skipped,
|
||||||
}
|
}
|
||||||
|
|
||||||
//////////////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////////////
|
||||||
@@ -805,6 +807,12 @@ declare_error_tests! {
|
|||||||
],
|
],
|
||||||
Error::UnknownVariant("Foo".to_owned()),
|
Error::UnknownVariant("Foo".to_owned()),
|
||||||
}
|
}
|
||||||
|
test_enum_skipped_variant<Enum> {
|
||||||
|
&[
|
||||||
|
Token::EnumUnit("Enum", "Skipped"),
|
||||||
|
],
|
||||||
|
Error::UnknownVariant("Skipped".to_owned()),
|
||||||
|
}
|
||||||
test_struct_seq_too_long<Struct> {
|
test_struct_seq_too_long<Struct> {
|
||||||
&[
|
&[
|
||||||
Token::SeqStart(Some(4)),
|
Token::SeqStart(Some(4)),
|
||||||
|
|||||||
Reference in New Issue
Block a user