diff options
| author | 魏曹先生 <1992414357@qq.com> | 2026-06-19 01:28:37 +0800 |
|---|---|---|
| committer | 魏曹先生 <1992414357@qq.com> | 2026-06-19 01:28:37 +0800 |
| commit | 4be889ac2dc5263ce03bb014de24916bee2e9aa8 (patch) | |
| tree | a2b6782801cb53af670d1b96a6c85f0e50f520f4 /mingling_macros | |
| parent | 66aab5323663a2d5e3e2db562cf6ce60d9e2c5be (diff) | |
Add dispatch_tree integration to dispatcher_clap
Diffstat (limited to 'mingling_macros')
| -rw-r--r-- | mingling_macros/src/dispatcher_clap.rs | 30 |
1 files changed, 29 insertions, 1 deletions
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! {} +} |
