Added checks for flatten attribute

This commit is contained in:
Armin Ronacher 2018-03-20 21:48:25 +01:00
parent 1d92569abc
commit 96393bfcc7

View File

@ -14,6 +14,7 @@ use Ctxt;
/// object. Simpler checks should happen when parsing and building the attrs.
pub fn check(cx: &Ctxt, cont: &Container) {
check_getter(cx, cont);
check_flatten(cx, cont);
check_identifier(cx, cont);
check_variant_skip_attrs(cx, cont);
check_internal_tag_field_name_conflict(cx, cont);
@ -40,6 +41,40 @@ fn check_getter(cx: &Ctxt, cont: &Container) {
}
}
/// Flattening has some restrictions we can test.
fn check_flatten(cx: &Ctxt, cont: &Container) {
match cont.data {
Data::Enum(_) => {
if cont.attrs.has_flatten() {
cx.error("#[serde(flatten)] is not allowed in an enum");
}
}
Data::Struct(_, _) => {
for field in cont.data.all_fields() {
if !field.attrs.flatten() {
continue;
}
if field.attrs.skip_serializing() {
cx.error(
"#[serde(flatten] can not be combined with \
#[serde(skip_serializing)]"
);
} else if field.attrs.skip_serializing_if().is_some() {
cx.error(
"#[serde(flatten] can not be combined with \
#[serde(skip_serializing_if = \"...\")]"
);
} else if field.attrs.skip_deserializing() {
cx.error(
"#[serde(flatten] can not be combined with \
#[serde(skip_deserializing)]"
);
}
}
}
}
}
/// The `other` attribute must be used at most once and it must be the last
/// variant of an enum that has the `field_identifier` attribute.
///