diff options
| author | 魏曹先生 <1992414357@qq.com> | 2026-03-29 01:18:25 +0800 |
|---|---|---|
| committer | 魏曹先生 <1992414357@qq.com> | 2026-03-29 01:18:25 +0800 |
| commit | 7809a8cbfbaf41fcc81de980c903e11f08bd1b12 (patch) | |
| tree | 1ae5d274530fa73af768146b955d183b94eb2a0a /mingling_macros/src/chain.rs | |
| parent | db9afa0b06355028eafe3bc29fe0b2429ba8fd0a (diff) | |
Generate struct names from function names in chain and renderer macros
Diffstat (limited to 'mingling_macros/src/chain.rs')
| -rw-r--r-- | mingling_macros/src/chain.rs | 23 |
1 files changed, 5 insertions, 18 deletions
diff --git a/mingling_macros/src/chain.rs b/mingling_macros/src/chain.rs index ddedc05..a7be233 100644 --- a/mingling_macros/src/chain.rs +++ b/mingling_macros/src/chain.rs @@ -5,24 +5,11 @@ use proc_macro::TokenStream; use quote::quote; -use syn::parse::{Parse, ParseStream}; use syn::spanned::Spanned; use syn::{ FnArg, Ident, ItemFn, Pat, PatType, ReturnType, Signature, Type, TypePath, parse_macro_input, }; -/// Parses the chain attribute arguments -struct ChainAttribute { - struct_name: Ident, -} - -impl Parse for ChainAttribute { - fn parse(input: ParseStream) -> syn::Result<Self> { - let struct_name = input.parse()?; - Ok(ChainAttribute { struct_name }) - } -} - /// Extracts the previous type and parameter name from function arguments fn extract_previous_info(sig: &Signature) -> syn::Result<(Pat, TypePath)> { // The function should have exactly one parameter @@ -72,11 +59,7 @@ fn extract_return_type(sig: &Signature) -> syn::Result<TypePath> { } } -pub fn chain_attr(attr: TokenStream, item: TokenStream) -> TokenStream { - // Parse the attribute arguments - let chain_attr = parse_macro_input!(attr as ChainAttribute); - let struct_name = chain_attr.struct_name; - +pub fn chain_attr(item: TokenStream) -> TokenStream { // Parse the function item let input_fn = parse_macro_input!(item as ItemFn); @@ -113,6 +96,10 @@ pub fn chain_attr(attr: TokenStream, item: TokenStream) -> TokenStream { // Get function name let fn_name = &input_fn.sig.ident; + // Generate struct name from function name using pascal_case + let pascal_case_name = just_fmt::pascal_case!(fn_name.to_string()); + let struct_name = Ident::new(&pascal_case_name, fn_name.span()); + // Generate the struct and implementation let expanded = quote! { #(#fn_attrs)* |
