diff options
author | Marcel Müller <m.mueller@ifm.com> | 2022-05-11 09:12:14 +0200 |
---|---|---|
committer | Marcel Müller <m.mueller@ifm.com> | 2022-05-12 08:51:42 +0200 |
commit | 68b61bdf0eed3c2ed3373e285899128d3f62e1eb (patch) | |
tree | f9dd4e202d0563a40dba35899b541cc26566a223 /crates | |
parent | 296e0bc82c532acd77a4ceeb83490bc93628442d (diff) |
Allow untagged enums in Config derive
Signed-off-by: Marcel Müller <m.mueller@ifm.com>
Diffstat (limited to 'crates')
-rw-r--r-- | crates/core/tedge_api/tedge_config_derive/src/lib.rs | 40 | ||||
-rw-r--r-- | crates/core/tedge_api/tests/derive_config.rs | 12 |
2 files changed, 31 insertions, 21 deletions
diff --git a/crates/core/tedge_api/tedge_config_derive/src/lib.rs b/crates/core/tedge_api/tedge_config_derive/src/lib.rs index a7e87321..698147dd 100644 --- a/crates/core/tedge_api/tedge_config_derive/src/lib.rs +++ b/crates/core/tedge_api/tedge_config_derive/src/lib.rs @@ -243,7 +243,7 @@ pub fn derive_config(input: TS) -> TS { macro_rules! abort_parse_enum_kind { ($kind:expr) => { abort!($kind, "Could not parse enum tag kind."; - help = "Accepted kinds are 'untagged' and 'tag = \"type\'") + help = "Accepted kinds are #[config(untagged)] and #[config(tag = \"type\')].") } } @@ -252,30 +252,36 @@ pub fn derive_config(input: TS) -> TS { .expect_or_abort("Could not parse #[config] meta attribute.") { syn::Meta::Path(kind) => { - if kind.is_ident("untagged") { - ConfigEnumKind::Untagged - } else { - abort_parse_enum_kind!(kind) - } + abort_parse_enum_kind!(kind) } syn::Meta::List(kind) => { if kind.nested.len() != 1 { abort_parse_enum_kind!(kind) } - if let Some(NestedMeta::Meta(Meta::NameValue(MetaNameValue { - path, - lit: Lit::Str(lit_str), - .. - }))) = kind.nested.first() - { - if path.is_ident("tag") { - ConfigEnumKind::Tagged(lit_str.clone()) - } else { + match kind.nested.first() { + Some(NestedMeta::Meta(Meta::NameValue(MetaNameValue { + path, + lit: Lit::Str(lit_str), + .. + }))) => { + if path.is_ident("tag") { + ConfigEnumKind::Tagged(lit_str.clone()) + } else { + abort_parse_enum_kind!(kind) + } + } + Some(NestedMeta::Meta(Meta::Path(path))) => { + if path.is_ident("untagged") { + ConfigEnumKind::Untagged + } else { + abort_parse_enum_kind!(path) + } + } + _ => { + println!("Oh no!"); abort_parse_enum_kind!(kind) } - } else { - abort_parse_enum_kind!(kind) } } syn::Meta::NameValue(kind) => abort!( diff --git a/crates/core/tedge_api/tests/derive_config.rs b/crates/core/tedge_api/tests/derive_config.rs index dfe57ef3..7e633031 100644 --- a/crates/core/tedge_api/tests/derive_config.rs +++ b/crates/core/tedge_api/tests/derive_config.rs @@ -40,6 +40,13 @@ enum EnumConfig { }, } +#[derive(Debug, Config)] +#[config(untagged)] +enum UntaggedEnumConfig { + One, + Two, +} + #[test] fn check_derive_config() { let conf = SimpleConfig::as_config(); @@ -53,10 +60,7 @@ fn check_derive_config() { if let ConfigKind::Enum(_, variants) = EnumConfig::as_config().kind() { if let ConfigKind::Struct(fields) = variants[2].2.kind() { - assert_eq!( - fields[0].1, - Some("The port of the inner complex type") - ) + assert_eq!(fields[0].1, Some("The port of the inner complex type")) } else { panic!() } |