Remove #[serde(repr = "map")]
This commit is contained in:
parent
d44f12907b
commit
58d52e784b
@ -770,10 +770,7 @@ fn deserialize_struct(
|
|||||||
untagged: &Untagged,
|
untagged: &Untagged,
|
||||||
) -> Fragment {
|
) -> Fragment {
|
||||||
let is_enum = variant_ident.is_some();
|
let is_enum = variant_ident.is_some();
|
||||||
let as_map = deserializer.is_none() && !is_enum && match cattrs.repr() {
|
let as_map = deserializer.is_none() && !is_enum && cattrs.has_flatten();
|
||||||
attr::ContainerRepr::Struct | attr::ContainerRepr::Auto => false,
|
|
||||||
attr::ContainerRepr::Map => true,
|
|
||||||
};
|
|
||||||
|
|
||||||
let this = ¶ms.this;
|
let this = ¶ms.this;
|
||||||
let (de_impl_generics, de_ty_generics, ty_generics, where_clause) =
|
let (de_impl_generics, de_ty_generics, ty_generics, where_clause) =
|
||||||
@ -896,10 +893,7 @@ fn deserialize_struct_in_place(
|
|||||||
deserializer: Option<Tokens>,
|
deserializer: Option<Tokens>,
|
||||||
) -> Option<Fragment> {
|
) -> Option<Fragment> {
|
||||||
let is_enum = variant_ident.is_some();
|
let is_enum = variant_ident.is_some();
|
||||||
let as_map = deserializer.is_none() && !is_enum && match cattrs.repr() {
|
let as_map = deserializer.is_none() && !is_enum && cattrs.has_flatten();
|
||||||
attr::ContainerRepr::Struct | attr::ContainerRepr::Auto => false,
|
|
||||||
attr::ContainerRepr::Map => true,
|
|
||||||
};
|
|
||||||
|
|
||||||
// for now we do not support in_place deserialization for structs that
|
// for now we do not support in_place deserialization for structs that
|
||||||
// are represented as map.
|
// are represented as map.
|
||||||
|
@ -245,9 +245,10 @@ fn serialize_tuple_struct(
|
|||||||
fn serialize_struct(params: &Parameters, fields: &[Field], cattrs: &attr::Container) -> Fragment {
|
fn serialize_struct(params: &Parameters, fields: &[Field], cattrs: &attr::Container) -> Fragment {
|
||||||
assert!(fields.len() as u64 <= u64::from(u32::MAX));
|
assert!(fields.len() as u64 <= u64::from(u32::MAX));
|
||||||
|
|
||||||
match cattrs.repr() {
|
if cattrs.has_flatten() {
|
||||||
attr::ContainerRepr::Struct | attr::ContainerRepr::Auto => serialize_struct_as_struct(params, fields, cattrs),
|
serialize_struct_as_map(params, fields, cattrs)
|
||||||
attr::ContainerRepr::Map => serialize_struct_as_map(params, fields, cattrs),
|
} else {
|
||||||
|
serialize_struct_as_struct(params, fields, cattrs)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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 {
|
if has_flatten {
|
||||||
attrs.mark_has_flatten();
|
attrs.mark_has_flatten();
|
||||||
}
|
}
|
||||||
|
@ -15,7 +15,6 @@ use syn::punctuated::Punctuated;
|
|||||||
use syn::synom::{Synom, ParseError};
|
use syn::synom::{Synom, ParseError};
|
||||||
use std::collections::BTreeSet;
|
use std::collections::BTreeSet;
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
use std::fmt;
|
|
||||||
use proc_macro2::{Span, TokenStream, TokenNode, TokenTree};
|
use proc_macro2::{Span, TokenStream, TokenNode, TokenTree};
|
||||||
|
|
||||||
// This module handles parsing of `#[serde(...)]` attributes. The entrypoints
|
// 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
|
/// Represents container (e.g. struct) attribute information
|
||||||
pub struct Container {
|
pub struct Container {
|
||||||
name: Name,
|
name: Name,
|
||||||
@ -145,7 +115,6 @@ pub struct Container {
|
|||||||
type_into: Option<syn::Type>,
|
type_into: Option<syn::Type>,
|
||||||
remote: Option<syn::Path>,
|
remote: Option<syn::Path>,
|
||||||
identifier: Identifier,
|
identifier: Identifier,
|
||||||
repr: ContainerRepr,
|
|
||||||
has_flatten: bool,
|
has_flatten: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -224,7 +193,6 @@ impl Container {
|
|||||||
let mut remote = Attr::none(cx, "remote");
|
let mut remote = Attr::none(cx, "remote");
|
||||||
let mut field_identifier = BoolAttr::none(cx, "field_identifier");
|
let mut field_identifier = BoolAttr::none(cx, "field_identifier");
|
||||||
let mut variant_identifier = BoolAttr::none(cx, "variant_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_items in item.attrs.iter().filter_map(get_serde_meta_items) {
|
||||||
for meta_item in meta_items {
|
for meta_item in meta_items {
|
||||||
@ -264,16 +232,6 @@ impl Container {
|
|||||||
deny_unknown_fields.set_true();
|
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)]`
|
// Parse `#[serde(default)]`
|
||||||
Meta(Word(word)) if word == "default" => match item.data {
|
Meta(Word(word)) if word == "default" => match item.data {
|
||||||
syn::Data::Struct(syn::DataStruct { fields: syn::Fields::Named(_), .. }) => {
|
syn::Data::Struct(syn::DataStruct { fields: syn::Fields::Named(_), .. }) => {
|
||||||
@ -417,7 +375,6 @@ impl Container {
|
|||||||
type_into: type_into.get(),
|
type_into: type_into.get(),
|
||||||
remote: remote.get(),
|
remote: remote.get(),
|
||||||
identifier: decide_identifier(cx, item, &field_identifier, &variant_identifier),
|
identifier: decide_identifier(cx, item, &field_identifier, &variant_identifier),
|
||||||
repr: repr.get().unwrap_or(ContainerRepr::Auto),
|
|
||||||
has_flatten: false,
|
has_flatten: false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -466,10 +423,6 @@ impl Container {
|
|||||||
self.identifier
|
self.identifier
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn repr(&self) -> ContainerRepr {
|
|
||||||
self.repr
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn has_flatten(&self) -> bool {
|
pub fn has_flatten(&self) -> bool {
|
||||||
self.has_flatten
|
self.has_flatten
|
||||||
}
|
}
|
||||||
|
@ -96,7 +96,6 @@ where
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Serialize, Deserialize)]
|
#[derive(Debug, PartialEq, Serialize, Deserialize)]
|
||||||
#[serde(repr = "map")]
|
|
||||||
struct CollectOther {
|
struct CollectOther {
|
||||||
a: u32,
|
a: u32,
|
||||||
b: u32,
|
b: u32,
|
||||||
@ -105,7 +104,6 @@ struct CollectOther {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Serialize, Deserialize)]
|
#[derive(Debug, PartialEq, Serialize, Deserialize)]
|
||||||
#[serde(repr = "map")]
|
|
||||||
struct FlattenStructEnumWrapper {
|
struct FlattenStructEnumWrapper {
|
||||||
#[serde(flatten)]
|
#[serde(flatten)]
|
||||||
data: FlattenStructEnum,
|
data: FlattenStructEnum,
|
||||||
@ -123,7 +121,6 @@ enum FlattenStructEnum {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Serialize, Deserialize)]
|
#[derive(Debug, PartialEq, Serialize, Deserialize)]
|
||||||
#[serde(repr = "map")]
|
|
||||||
struct FlattenStructTagContentEnumWrapper {
|
struct FlattenStructTagContentEnumWrapper {
|
||||||
outer: u32,
|
outer: u32,
|
||||||
#[serde(flatten)]
|
#[serde(flatten)]
|
||||||
@ -1497,7 +1494,7 @@ fn test_flatten_struct_tag_content_enum_newtype() {
|
|||||||
#[test]
|
#[test]
|
||||||
fn test_unknown_field_in_flatten() {
|
fn test_unknown_field_in_flatten() {
|
||||||
#[derive(Debug, PartialEq, Serialize, Deserialize)]
|
#[derive(Debug, PartialEq, Serialize, Deserialize)]
|
||||||
#[serde(repr = "map", deny_unknown_fields)]
|
#[serde(deny_unknown_fields)]
|
||||||
struct Outer {
|
struct Outer {
|
||||||
dummy: String,
|
dummy: String,
|
||||||
#[serde(flatten)]
|
#[serde(flatten)]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user