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/Cargo.toml | 3 +++ mingling_core/Cargo.toml | 2 ++ mingling_macros/Cargo.toml | 3 +++ mingling_macros/src/dispatcher.rs | 39 +++++++++++---------------------------- mingling_macros/src/lib.rs | 2 +- 5 files changed, 20 insertions(+), 29 deletions(-) diff --git a/mingling/Cargo.toml b/mingling/Cargo.toml index e52ffe1..7c5d4c5 100644 --- a/mingling/Cargo.toml +++ b/mingling/Cargo.toml @@ -18,7 +18,10 @@ mingling = { path = ".", features = ["full"] } [features] debug = ["mingling_core/debug"] async = ["mingling_core/async", "mingling_macros/async"] + default = ["mingling_core/default"] + +clap_parser = ["mingling_macros/clap_parser"] full = ["mingling_core/full", "mingling_macros/full", "comp", "parser"] general_renderer = [ "mingling_core/general_renderer", diff --git a/mingling_core/Cargo.toml b/mingling_core/Cargo.toml index 61bee49..b6846e7 100644 --- a/mingling_core/Cargo.toml +++ b/mingling_core/Cargo.toml @@ -9,7 +9,9 @@ repository = "https://github.com/catilgrass/mingling" [features] default = [] async = [] + full = ["comp", "general_renderer"] + comp = ["dep:just_template"] debug = ["dep:log", "dep:env_logger"] general_renderer = [ diff --git a/mingling_macros/Cargo.toml b/mingling_macros/Cargo.toml index 6def757..1a578a5 100644 --- a/mingling_macros/Cargo.toml +++ b/mingling_macros/Cargo.toml @@ -12,7 +12,10 @@ proc-macro = true [features] default = [] async = [] + full = ["comp", "general_renderer"] + +clap_parser = [] comp = [] general_renderer = [] 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) } } diff --git a/mingling_macros/src/lib.rs b/mingling_macros/src/lib.rs index 5ee53a1..5736578 100644 --- a/mingling_macros/src/lib.rs +++ b/mingling_macros/src/lib.rs @@ -55,7 +55,7 @@ pub fn pack(input: TokenStream) -> TokenStream { #[proc_macro] pub fn dispatcher(input: TokenStream) -> TokenStream { - dispatcher::dispatcher_chain(input) + dispatcher::dispatcher(input) } #[proc_macro] -- cgit