From 722cdf5eadd8beb7330ad2ea0e8e99df26e2ea11 Mon Sep 17 00:00:00 2001 From: Juliette Cordor Date: Wed, 11 Dec 2024 11:02:31 +1100 Subject: [PATCH] update error handling in enum strip --- quork-proc/src/strip_enum.rs | 25 ++++++++++++++++++++----- quork-proc/tests/strip_enum.rs | 12 ++++++++++-- 2 files changed, 30 insertions(+), 7 deletions(-) diff --git a/quork-proc/src/strip_enum.rs b/quork-proc/src/strip_enum.rs index e5a4d3c..eaff25c 100644 --- a/quork-proc/src/strip_enum.rs +++ b/quork-proc/src/strip_enum.rs @@ -13,14 +13,14 @@ fn ignore_variant(variant: &Variant) -> bool { ignored = true; Ok(()) } else { - Err(meta.error("unsupported stripped property")) + Err(meta.error("unsupported property")) } }); if let Err(err) = list.parse_args_with(list_parser) { abort! { err.span(), - "Failed to parse stripped attribute: {}", err; + "Failed to parse attribute: {}", err; help = "Only supported properties on enum variants are `ignore`" } } @@ -88,11 +88,17 @@ pub fn strip_enum(ast: &mut DeriveInput) -> TokenStream { new_ident = Some(meta.value()?.parse()?); Ok(()) } else { - Err(meta.error("unsupported stripped property")) + Err(meta.error("unsupported property")) } }); - info_attr.parse_args_with(ident_parser).unwrap(); + if let Err(err) = info_attr.parse_args_with(ident_parser) { + abort! { + err.span(), + "Failed to parse attribute: {}", err; + help = "Only supported properties on enum definitions are `ident`" + } + } new_ident.unwrap_or_else(default_ident) } else { @@ -103,7 +109,16 @@ pub fn strip_enum(ast: &mut DeriveInput) -> TokenStream { .iter() .filter(|attr| attr.path().is_ident("stripped_meta")) .flat_map(|meta_attr| match &meta_attr.meta { - Meta::List(meta_data) => vec![meta_data.parse_args::().unwrap()], + Meta::List(meta_data) => match meta_data.parse_args::() { + Ok(meta) => vec![meta], + Err(err) => { + abort! { + err.span(), + "Failed to parse specified metadata: {}", err; + help = "Make sure the provided arguments are in the form of Rust metadata. (i.e the tokens contained within `#[...]`)" + } + } + }, // Meta::NameValue(MetaNameValue { // value: // syn::Expr::Lit(syn::ExprLit { diff --git a/quork-proc/tests/strip_enum.rs b/quork-proc/tests/strip_enum.rs index fa0d9ad..be4f4ee 100644 --- a/quork-proc/tests/strip_enum.rs +++ b/quork-proc/tests/strip_enum.rs @@ -32,18 +32,26 @@ fn has_all_variants() { #[stripped_meta(strum(serialize_all = "kebab-case"))] enum EnumExclude { Test1(DummyStruct), - #[stripped(notignore)] + #[stripped(ignore)] Test2(DummyStruct), Test3(DummyStruct), } -#[derive(Strip, Display)] +#[derive(Strip)] #[stripped_meta(derive(EnumIter))] #[stripped_meta(strum(serialize_all = "kebab-case"))] enum EnumWithInherit { Test1(DummyStruct), } +#[derive(Strip)] +#[stripped_meta(derive(EnumIter))] +#[stripped_meta(strum(serialize_all = "kebab-case"))] +#[stripped(ident = IChoseThisIdent)] +enum EnumWithCustomIdent { + Test1(DummyStruct), +} + #[test] fn excludes_no_hook_variant() { let variants = enum_to_string::();