mirror of
https://github.com/pezkuwichain/serde.git
synced 2026-06-13 10:11:01 +00:00
Allow flatten attribute in enums
This commit is contained in:
@@ -6,7 +6,7 @@
|
|||||||
// option. This file may not be copied, modified, or distributed
|
// option. This file may not be copied, modified, or distributed
|
||||||
// except according to those terms.
|
// except according to those terms.
|
||||||
|
|
||||||
use internals::ast::{Container, Data, Style};
|
use internals::ast::{Container, Data, Field, Style};
|
||||||
use internals::attr::{EnumTag, Identifier};
|
use internals::attr::{EnumTag, Identifier};
|
||||||
use internals::Ctxt;
|
use internals::Ctxt;
|
||||||
|
|
||||||
@@ -44,15 +44,24 @@ fn check_getter(cx: &Ctxt, cont: &Container) {
|
|||||||
/// Flattening has some restrictions we can test.
|
/// Flattening has some restrictions we can test.
|
||||||
fn check_flatten(cx: &Ctxt, cont: &Container) {
|
fn check_flatten(cx: &Ctxt, cont: &Container) {
|
||||||
match cont.data {
|
match cont.data {
|
||||||
Data::Enum(_) => {
|
Data::Enum(ref variants) => {
|
||||||
if cont.attrs.has_flatten() {
|
for variant in variants {
|
||||||
cx.error("#[serde(flatten)] cannot be used within enums");
|
for field in &variant.fields {
|
||||||
|
check_flatten_field(cx, variant.style, field);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Data::Struct(style, _) => {
|
}
|
||||||
for field in cont.data.all_fields() {
|
Data::Struct(style, ref fields) => {
|
||||||
|
for field in fields {
|
||||||
|
check_flatten_field(cx, style, field);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn check_flatten_field(cx: &Ctxt, style: Style, field: &Field) {
|
||||||
if !field.attrs.flatten() {
|
if !field.attrs.flatten() {
|
||||||
continue;
|
return;
|
||||||
}
|
}
|
||||||
match style {
|
match style {
|
||||||
Style::Tuple => {
|
Style::Tuple => {
|
||||||
@@ -79,9 +88,6 @@ fn check_flatten(cx: &Ctxt, cont: &Container) {
|
|||||||
#[serde(skip_deserializing)]",
|
#[serde(skip_deserializing)]",
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The `other` attribute must be used at most once and it must be the last
|
/// The `other` attribute must be used at most once and it must be the last
|
||||||
|
|||||||
Reference in New Issue
Block a user