From 019b8def49d814bca44047d85c9ff27bbda36a66 Mon Sep 17 00:00:00 2001 From: 魏曹先生 <1992414357@qq.com> Date: Sat, 25 Apr 2026 22:05:08 +0800 Subject: Add clap_parser feature and refactor dispatcher macro internals --- mingling_macros/src/dispatcher.rs | 39 +++++++++++---------------------------- 1 file changed, 11 insertions(+), 28 deletions(-) (limited to 'mingling_macros/src/dispatcher.rs') diff --git a/mingling_macros/src/dispatcher.rs b/mingling_macros/src/dispatcher.rs index 6223a81..3ca5d25 100644 --- a/mingling_macros/src/dispatcher.rs +++ b/mingling_macros/src/dispatcher.rs @@ -67,7 +67,7 @@ impl Parse for DispatcherChainInput { // Additionally, the token stream generation patterns are nearly identical between // the two main functions and could benefit from refactoring. -pub fn dispatcher_chain(input: TokenStream) -> TokenStream { +pub fn dispatcher(input: TokenStream) -> TokenStream { // Parse the input let dispatcher_input = syn::parse_macro_input!(input as DispatcherChainInput); @@ -96,46 +96,29 @@ pub fn dispatcher_chain(input: TokenStream) -> TokenStream { let comp_entry = get_comp_entry(&pack); - let expanded = if use_default { - // For default case, use ThisProgram - quote! { - #[derive(Debug, Default)] - pub struct #command_struct; - - ::mingling::macros::pack!(ThisProgram, #pack = Vec); - - #comp_entry + let expanded = { + let program_ident = if use_default { + Ident::new("ThisProgram", proc_macro2::Span::call_site()) + } else { + group_name.clone() + }; - impl ::mingling::Dispatcher for #command_struct { - fn node(&self) -> ::mingling::Node { - ::mingling::macros::node!(#command_name_str) - } - fn begin(&self, args: Vec) -> ::mingling::ChainProcess { - #pack::new(args).to_chain() - } - fn clone_dispatcher(&self) -> Box> { - Box::new(#command_struct) - } - } - } - } else { - // For explicit case, use the provided group_name quote! { #[derive(Debug, Default)] pub struct #command_struct; - ::mingling::macros::pack!(#group_name, #pack = Vec); + ::mingling::macros::pack!(#program_ident, #pack = Vec); #comp_entry - impl ::mingling::Dispatcher<#group_name> for #command_struct { + impl ::mingling::Dispatcher<#program_ident> for #command_struct { fn node(&self) -> ::mingling::Node { ::mingling::macros::node!(#command_name_str) } - fn begin(&self, args: Vec) -> ::mingling::ChainProcess<#group_name> { + fn begin(&self, args: Vec) -> ::mingling::ChainProcess<#program_ident> { #pack::new(args).to_chain() } - fn clone_dispatcher(&self) -> Box> { + fn clone_dispatcher(&self) -> Box> { Box::new(#command_struct) } } -- cgit