diff options
| author | 魏曹先生 <1992414357@qq.com> | 2026-04-25 22:05:08 +0800 |
|---|---|---|
| committer | 魏曹先生 <1992414357@qq.com> | 2026-04-25 22:05:08 +0800 |
| commit | 019b8def49d814bca44047d85c9ff27bbda36a66 (patch) | |
| tree | f86292d3cd0911ca9d436325d8f9b1d18284ffa6 /mingling_macros | |
| parent | 87bbc88548a4dadbc04998732de4cabfc93f3524 (diff) | |
Add clap_parser feature and refactor dispatcher macro internals
Diffstat (limited to 'mingling_macros')
| -rw-r--r-- | mingling_macros/Cargo.toml | 3 | ||||
| -rw-r--r-- | mingling_macros/src/dispatcher.rs | 39 | ||||
| -rw-r--r-- | mingling_macros/src/lib.rs | 2 |
3 files changed, 15 insertions, 29 deletions
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<String>); - - #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<ThisProgram> for #command_struct { - fn node(&self) -> ::mingling::Node { - ::mingling::macros::node!(#command_name_str) - } - fn begin(&self, args: Vec<String>) -> ::mingling::ChainProcess<ThisProgram> { - #pack::new(args).to_chain() - } - fn clone_dispatcher(&self) -> Box<dyn ::mingling::Dispatcher<ThisProgram>> { - 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<String>); + ::mingling::macros::pack!(#program_ident, #pack = Vec<String>); #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<String>) -> ::mingling::ChainProcess<#group_name> { + fn begin(&self, args: Vec<String>) -> ::mingling::ChainProcess<#program_ident> { #pack::new(args).to_chain() } - fn clone_dispatcher(&self) -> Box<dyn ::mingling::Dispatcher<#group_name>> { + fn clone_dispatcher(&self) -> Box<dyn ::mingling::Dispatcher<#program_ident>> { 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] |
