From 96393bfcc7324eca877002157ca6f44d066013b6 Mon Sep 17 00:00:00 2001 From: Armin Ronacher Date: Tue, 20 Mar 2018 21:48:25 +0100 Subject: [PATCH] Added checks for flatten attribute --- serde_derive_internals/src/check.rs | 35 +++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/serde_derive_internals/src/check.rs b/serde_derive_internals/src/check.rs index c3df861b..7afe42ca 100644 --- a/serde_derive_internals/src/check.rs +++ b/serde_derive_internals/src/check.rs @@ -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. ///