diff options
| author | 魏曹先生 <1992414357@qq.com> | 2026-04-13 21:49:13 +0800 |
|---|---|---|
| committer | 魏曹先生 <1992414357@qq.com> | 2026-04-13 21:49:13 +0800 |
| commit | 4dbfa9c3b2db0a4f1ac7d84ea2cebe4c11759c79 (patch) | |
| tree | d37ca996f9c39348278399a3a60ea28f1b59a613 | |
| parent | 69794f0e88457e59b37bd208e32a1b58cbe457f8 (diff) | |
Simplify attribute parsing for enum_desc and enum_rename
| -rw-r--r-- | mingling_macros/src/enum_tag.rs | 47 |
1 files changed, 7 insertions, 40 deletions
diff --git a/mingling_macros/src/enum_tag.rs b/mingling_macros/src/enum_tag.rs index 6a3abf8..0160964 100644 --- a/mingling_macros/src/enum_tag.rs +++ b/mingling_macros/src/enum_tag.rs @@ -10,8 +10,7 @@ use proc_macro::TokenStream; use proc_macro2::Span; use quote::quote; use syn::{ - Attribute, Data, DeriveInput, Error, Expr, Fields, Ident, Lit, LitStr, Meta, MetaNameValue, - Result, Variant, parse_macro_input, + Attribute, Data, DeriveInput, Error, Fields, Ident, LitStr, Result, Variant, parse_macro_input, }; pub fn derive_enum_tag(input: TokenStream) -> TokenStream { @@ -146,22 +145,11 @@ fn process_variant( fn extract_description(attrs: &[Attribute], variant_name: &Ident) -> Result<LitStr> { for attr in attrs { if attr.path().is_ident("enum_desc") { - return match attr.parse_args::<Meta>() { - Ok(Meta::NameValue(MetaNameValue { - value: - Expr::Lit(syn::PatLit { - lit: Lit::Str(lit_str), - .. - }), - .. - })) => Ok(lit_str), - Ok(_) => Err(Error::new_spanned( - attr, - "#[enum_desc] attribute must be in the form `#[enum_desc(\"description\")]`", - )), + return match attr.parse_args::<LitStr>() { + Ok(lit_str) => Ok(lit_str), Err(_) => Err(Error::new_spanned( attr, - "Failed to parse #[enum_desc] attribute", + "#[enum_desc] attribute must be in the form `#[enum_desc(\"description\")]`", )), }; } @@ -175,32 +163,11 @@ fn extract_description(attrs: &[Attribute], variant_name: &Ident) -> Result<LitS fn extract_rename(attrs: &[Attribute]) -> Result<Option<String>> { for attr in attrs { if attr.path().is_ident("enum_rename") { - return match attr.parse_args::<Meta>() { - Ok(Meta::NameValue(MetaNameValue { - path, - value: - Expr::Lit(syn::PatLit { - lit: Lit::Str(lit_str), - .. - }), - .. - })) => { - if path.is_ident("name") { - Ok(Some(lit_str.value())) - } else { - Err(Error::new_spanned( - attr, - "#[enum_rename] attribute must be in the form `#[enum_rename(name = \"new_name\")]`", - )) - } - } - Ok(_) => Err(Error::new_spanned( - attr, - "#[enum_rename] attribute must be in the form `#[enum_rename(name = \"new_name\")]`", - )), + return match attr.parse_args::<LitStr>() { + Ok(lit_str) => Ok(Some(lit_str.value())), Err(_) => Err(Error::new_spanned( attr, - "Failed to parse #[enum_rename] attribute", + "#[enum_rename] attribute must be in the form `#[enum_rename(\"new_name\")]`", )), }; } |
