From 4dbfa9c3b2db0a4f1ac7d84ea2cebe4c11759c79 Mon Sep 17 00:00:00 2001 From: 魏曹先生 <1992414357@qq.com> Date: Mon, 13 Apr 2026 21:49:13 +0800 Subject: Simplify attribute parsing for enum_desc and enum_rename --- mingling_macros/src/enum_tag.rs | 47 ++++++----------------------------------- 1 file changed, 7 insertions(+), 40 deletions(-) (limited to 'mingling_macros') 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 { for attr in attrs { if attr.path().is_ident("enum_desc") { - return match attr.parse_args::() { - 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::() { + 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 Result> { for attr in attrs { if attr.path().is_ident("enum_rename") { - return match attr.parse_args::() { - 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::() { + 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\")]`", )), }; } -- cgit