Skip to content

Commit

Permalink
update error handling in enum strip
Browse files Browse the repository at this point in the history
  • Loading branch information
jewlexx committed Dec 11, 2024
1 parent 6f634db commit 722cdf5
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
25 changes: 20 additions & 5 deletions quork-proc/src/strip_enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`"
}
}
Expand Down Expand Up @@ -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 {
Expand All @@ -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::<syn::Meta>().unwrap()],
Meta::List(meta_data) => match meta_data.parse_args::<syn::Meta>() {
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 {
Expand Down
12 changes: 10 additions & 2 deletions quork-proc/tests/strip_enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<EnumExcludeStripped>();
Expand Down

0 comments on commit 722cdf5

Please sign in to comment.