From 4be889ac2dc5263ce03bb014de24916bee2e9aa8 Mon Sep 17 00:00:00 2001 From: 魏曹先生 <1992414357@qq.com> Date: Fri, 19 Jun 2026 01:28:37 +0800 Subject: Add dispatch_tree integration to dispatcher_clap --- mingling_macros/src/dispatcher_clap.rs | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) (limited to 'mingling_macros/src') diff --git a/mingling_macros/src/dispatcher_clap.rs b/mingling_macros/src/dispatcher_clap.rs index 5a6cf4c..34d146c 100644 --- a/mingling_macros/src/dispatcher_clap.rs +++ b/mingling_macros/src/dispatcher_clap.rs @@ -232,6 +232,9 @@ pub fn dispatcher_clap_attr(attr: TokenStream, item: TokenStream) -> TokenStream None }; + let dispatch_tree_entry = + get_dispatch_tree_entry(&command_name_str, &dispatcher_struct, &struct_name); + let expanded = quote! { // Keep the original struct definition #input_struct @@ -242,9 +245,13 @@ pub fn dispatcher_clap_attr(attr: TokenStream, item: TokenStream) -> TokenStream // Generate the help block if enabled #help_gen + // Dispatch tree registration (if feature enabled) + #dispatch_tree_entry + // Generate the dispatcher struct #[doc(hidden)] - struct #dispatcher_struct; + #[derive(Default)] + pub struct #dispatcher_struct; impl ::mingling::Dispatcher<#program_path> for #dispatcher_struct { fn node(&self) -> ::mingling::Node { @@ -273,3 +280,24 @@ pub fn dispatcher_clap_attr(attr: TokenStream, item: TokenStream) -> TokenStream expanded.into() } + +#[cfg(feature = "dispatch_tree")] +fn get_dispatch_tree_entry( + command_name_str: &str, + dispatcher_struct: &Ident, + entry_name: &Ident, +) -> proc_macro2::TokenStream { + let node_name_lit = syn::LitStr::new(command_name_str, proc_macro2::Span::call_site()); + quote! { + ::mingling::macros::register_dispatcher!(#node_name_lit, #dispatcher_struct, #entry_name); + } +} + +#[cfg(not(feature = "dispatch_tree"))] +fn get_dispatch_tree_entry( + _command_name_str: &str, + _dispatcher_struct: &Ident, + _entry_name: &Ident, +) -> proc_macro2::TokenStream { + quote! {} +} -- cgit