Remove #[serde(repr = "map")]

This commit is contained in:
Armin Ronacher 2018-03-18 18:30:46 +01:00
parent d44f12907b
commit 58d52e784b
5 changed files with 7 additions and 68 deletions

View File

@ -770,10 +770,7 @@ fn deserialize_struct(
untagged: &Untagged,
) -> Fragment {
let is_enum = variant_ident.is_some();
let as_map = deserializer.is_none() && !is_enum && match cattrs.repr() {
attr::ContainerRepr::Struct | attr::ContainerRepr::Auto => false,
attr::ContainerRepr::Map => true,
};
let as_map = deserializer.is_none() && !is_enum && cattrs.has_flatten();
let this = &params.this;
let (de_impl_generics, de_ty_generics, ty_generics, where_clause) =
@ -896,10 +893,7 @@ fn deserialize_struct_in_place(
deserializer: Option<Tokens>,
) -> Option<Fragment> {
let is_enum = variant_ident.is_some();
let as_map = deserializer.is_none() && !is_enum && match cattrs.repr() {
attr::ContainerRepr::Struct | attr::ContainerRepr::Auto => false,
attr::ContainerRepr::Map => true,
};
let as_map = deserializer.is_none() && !is_enum && cattrs.has_flatten();
// for now we do not support in_place deserialization for structs that
// are represented as map.

View File

@ -245,9 +245,10 @@ fn serialize_tuple_struct(
fn serialize_struct(params: &Parameters, fields: &[Field], cattrs: &attr::Container) -> Fragment {
assert!(fields.len() as u64 <= u64::from(u32::MAX));
match cattrs.repr() {
attr::ContainerRepr::Struct | attr::ContainerRepr::Auto => serialize_struct_as_struct(params, fields, cattrs),
attr::ContainerRepr::Map => serialize_struct_as_map(params, fields, cattrs),
if cattrs.has_flatten() {
serialize_struct_as_map(params, fields, cattrs)
} else {
serialize_struct_as_struct(params, fields, cattrs)
}
}

View File

@ -79,12 +79,6 @@ impl<'a> Container<'a> {
},
}
if has_flatten && attrs.repr() != attr::ContainerRepr::Map {
cx.error(format!("#[serde(flatten)] requires \
#[serde(repr = \"map\")] on the container, but \
found #[serde(repr = \"{}\")]", attrs.repr()));
}
if has_flatten {
attrs.mark_has_flatten();
}

View File

@ -15,7 +15,6 @@ use syn::punctuated::Punctuated;
use syn::synom::{Synom, ParseError};
use std::collections::BTreeSet;
use std::str::FromStr;
use std::fmt;
use proc_macro2::{Span, TokenStream, TokenNode, TokenTree};
// This module handles parsing of `#[serde(...)]` attributes. The entrypoints
@ -103,35 +102,6 @@ impl Name {
}
}
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
pub enum ContainerRepr {
Auto,
Struct,
Map,
}
impl fmt::Display for ContainerRepr {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", match *self {
ContainerRepr::Auto => "auto",
ContainerRepr::Struct => "struct",
ContainerRepr::Map => "map",
})
}
}
impl FromStr for ContainerRepr {
type Err = ();
fn from_str(s: &str) -> Result<ContainerRepr, ()> {
match s {
"auto" => Ok(ContainerRepr::Auto),
"struct" => Ok(ContainerRepr::Struct),
"map" => Ok(ContainerRepr::Map),
_ => Err(()),
}
}
}
/// Represents container (e.g. struct) attribute information
pub struct Container {
name: Name,
@ -145,7 +115,6 @@ pub struct Container {
type_into: Option<syn::Type>,
remote: Option<syn::Path>,
identifier: Identifier,
repr: ContainerRepr,
has_flatten: bool,
}
@ -224,7 +193,6 @@ impl Container {
let mut remote = Attr::none(cx, "remote");
let mut field_identifier = BoolAttr::none(cx, "field_identifier");
let mut variant_identifier = BoolAttr::none(cx, "variant_identifier");
let mut repr = Attr::none(cx, "repr");
for meta_items in item.attrs.iter().filter_map(get_serde_meta_items) {
for meta_item in meta_items {
@ -264,16 +232,6 @@ impl Container {
deny_unknown_fields.set_true();
}
// Parse `#[serde(repr = "foo")]`
Meta(NameValue(ref m)) if m.ident == "repr" => {
if let Ok(s) = get_lit_str(cx, m.ident.as_ref(), m.ident.as_ref(), &m.lit) {
match ContainerRepr::from_str(&s.value()) {
Ok(value) => repr.set(value),
Err(()) => cx.error(format!("unknown value for #[serde(repr = {})]", s.value()))
}
}
}
// Parse `#[serde(default)]`
Meta(Word(word)) if word == "default" => match item.data {
syn::Data::Struct(syn::DataStruct { fields: syn::Fields::Named(_), .. }) => {
@ -417,7 +375,6 @@ impl Container {
type_into: type_into.get(),
remote: remote.get(),
identifier: decide_identifier(cx, item, &field_identifier, &variant_identifier),
repr: repr.get().unwrap_or(ContainerRepr::Auto),
has_flatten: false,
}
}
@ -466,10 +423,6 @@ impl Container {
self.identifier
}
pub fn repr(&self) -> ContainerRepr {
self.repr
}
pub fn has_flatten(&self) -> bool {
self.has_flatten
}

View File

@ -96,7 +96,6 @@ where
}
#[derive(Debug, PartialEq, Serialize, Deserialize)]
#[serde(repr = "map")]
struct CollectOther {
a: u32,
b: u32,
@ -105,7 +104,6 @@ struct CollectOther {
}
#[derive(Debug, PartialEq, Serialize, Deserialize)]
#[serde(repr = "map")]
struct FlattenStructEnumWrapper {
#[serde(flatten)]
data: FlattenStructEnum,
@ -123,7 +121,6 @@ enum FlattenStructEnum {
}
#[derive(Debug, PartialEq, Serialize, Deserialize)]
#[serde(repr = "map")]
struct FlattenStructTagContentEnumWrapper {
outer: u32,
#[serde(flatten)]
@ -1497,7 +1494,7 @@ fn test_flatten_struct_tag_content_enum_newtype() {
#[test]
fn test_unknown_field_in_flatten() {
#[derive(Debug, PartialEq, Serialize, Deserialize)]
#[serde(repr = "map", deny_unknown_fields)]
#[serde(deny_unknown_fields)]
struct Outer {
dummy: String,
#[serde(flatten)]