diff options
author | Marcel Müller <m.mueller@ifm.com> | 2022-05-11 09:23:03 +0200 |
---|---|---|
committer | Marcel Müller <m.mueller@ifm.com> | 2022-05-12 08:51:46 +0200 |
commit | 4c2f168e8831be7334bf4c46db725df644ed6490 (patch) | |
tree | cdf5773764652107ff4cfbe4ea52f35a61497179 /crates | |
parent | 68b61bdf0eed3c2ed3373e285899128d3f62e1eb (diff) |
Emit better errors when parsing invalid enums
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 | 22 |
1 files changed, 14 insertions, 8 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 698147dd..3799e38c 100644 --- a/crates/core/tedge_api/tedge_config_derive/src/lib.rs +++ b/crates/core/tedge_api/tedge_config_derive/src/lib.rs @@ -1,6 +1,6 @@ use proc_macro::TokenStream as TS; use proc_macro2::TokenStream; -use proc_macro_error::{abort, proc_macro_error, ResultExt}; +use proc_macro_error::{abort, emit_error, proc_macro_error, OptionExt, ResultExt}; use quote::{quote, ToTokens, TokenStreamExt}; use syn::{ parse_macro_input, Attribute, DeriveInput, Ident, Lit, LitStr, Meta, MetaNameValue, NestedMeta, @@ -325,17 +325,23 @@ pub fn derive_config(input: TS) -> TS { }, ) } - syn::Fields::Unit => abort!( - ident, - "Unit structs are not supported as they cannot be represented" - ), + syn::Fields::Unit => { + emit_error!( + var, + "Unit structs are not supported as they cannot be represented" + ); + return None; + } }; let docs = extract_docs_from_attributes(var.attrs.iter()); - ConfigVariant { kind, docs } + Some(ConfigVariant { kind, docs }) }) - .collect(); + .collect::<Option<_>>(); - ConfigQuoteKind::Enum(enum_kind, variants) + ConfigQuoteKind::Enum( + enum_kind, + variants.expect_or_abort("Enum contains invalid variants"), + ) } syn::Data::Union(_) => { abort!( |