mirror of
https://github.com/serde-rs/serde.git
synced 2025-10-02 07:21:12 +00:00
Store flatten flag in container attributes
This commit is contained in:
parent
571bb8caed
commit
5ae06bba49
@ -286,7 +286,7 @@ fn serialize_struct_as_struct(params: &Parameters, fields: &[Field], cattrs: &at
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_struct_as_map(params: &Parameters, fields: &[Field], _cattrs: &attr::Container) -> Fragment {
|
fn serialize_struct_as_map(params: &Parameters, fields: &[Field], cattrs: &attr::Container) -> Fragment {
|
||||||
let serialize_fields = serialize_struct_visitor(
|
let serialize_fields = serialize_struct_visitor(
|
||||||
fields,
|
fields,
|
||||||
params,
|
params,
|
||||||
@ -301,11 +301,7 @@ fn serialize_struct_as_map(params: &Parameters, fields: &[Field], _cattrs: &attr
|
|||||||
|
|
||||||
let let_mut = mut_if(serialized_fields.peek().is_some());
|
let let_mut = mut_if(serialized_fields.peek().is_some());
|
||||||
|
|
||||||
let has_flatten = fields
|
let len = if cattrs.has_flatten() {
|
||||||
.iter()
|
|
||||||
.any(|field| field.attrs.flatten());
|
|
||||||
|
|
||||||
let len = if has_flatten {
|
|
||||||
quote!(None)
|
quote!(None)
|
||||||
} else {
|
} else {
|
||||||
let len = serialized_fields
|
let len = serialized_fields
|
||||||
|
@ -48,7 +48,7 @@ pub enum Style {
|
|||||||
|
|
||||||
impl<'a> Container<'a> {
|
impl<'a> Container<'a> {
|
||||||
pub fn from_ast(cx: &Ctxt, item: &'a syn::DeriveInput) -> Container<'a> {
|
pub fn from_ast(cx: &Ctxt, item: &'a syn::DeriveInput) -> Container<'a> {
|
||||||
let attrs = attr::Container::from_ast(cx, item);
|
let mut attrs = attr::Container::from_ast(cx, item);
|
||||||
|
|
||||||
let mut data = match item.data {
|
let mut data = match item.data {
|
||||||
syn::Data::Enum(ref data) => {
|
syn::Data::Enum(ref data) => {
|
||||||
@ -85,6 +85,10 @@ impl<'a> Container<'a> {
|
|||||||
found #[serde(repr = \"{}\")]", attrs.repr()));
|
found #[serde(repr = \"{}\")]", attrs.repr()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if has_flatten {
|
||||||
|
attrs.mark_has_flatten();
|
||||||
|
}
|
||||||
|
|
||||||
let item = Container {
|
let item = Container {
|
||||||
ident: item.ident,
|
ident: item.ident,
|
||||||
attrs: attrs,
|
attrs: attrs,
|
||||||
|
@ -146,6 +146,7 @@ pub struct Container {
|
|||||||
remote: Option<syn::Path>,
|
remote: Option<syn::Path>,
|
||||||
identifier: Identifier,
|
identifier: Identifier,
|
||||||
repr: ContainerRepr,
|
repr: ContainerRepr,
|
||||||
|
has_flatten: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Styles of representing an enum.
|
/// Styles of representing an enum.
|
||||||
@ -417,6 +418,7 @@ impl Container {
|
|||||||
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),
|
repr: repr.get().unwrap_or(ContainerRepr::Auto),
|
||||||
|
has_flatten: false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -467,6 +469,14 @@ impl Container {
|
|||||||
pub fn repr(&self) -> ContainerRepr {
|
pub fn repr(&self) -> ContainerRepr {
|
||||||
self.repr
|
self.repr
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn has_flatten(&self) -> bool {
|
||||||
|
self.has_flatten
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn mark_has_flatten(&mut self) {
|
||||||
|
self.has_flatten = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn decide_tag(
|
fn decide_tag(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user