diff options
Diffstat (limited to 'mingling_macros/src/dispatcher_chain.rs')
| -rw-r--r-- | mingling_macros/src/dispatcher_chain.rs | 38 |
1 files changed, 37 insertions, 1 deletions
diff --git a/mingling_macros/src/dispatcher_chain.rs b/mingling_macros/src/dispatcher_chain.rs index f531424..4038600 100644 --- a/mingling_macros/src/dispatcher_chain.rs +++ b/mingling_macros/src/dispatcher_chain.rs @@ -1,7 +1,7 @@ //! Dispatcher Chain and Dispatcher Render Macros //! //! This module provides macros for creating dispatcher chain and dispatcher render structs -//! with automatic implementations of the `DispatcherChain` trait. +//! with automatic implementations of the `Dispatcher` trait. use proc_macro::TokenStream; use quote::quote; @@ -60,6 +60,13 @@ impl Parse for DispatcherChainInput { } } +// NOTICE: This implementation contains significant code duplication between the explicit +// and default cases in both `dispatcher_chain` and `dispatcher_render` functions. +// The logic for handling default vs explicit group names and generating the appropriate +// code should be extracted into common helper functions to reduce redundancy. +// 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 { // Parse the input let dispatcher_input = syn::parse_macro_input!(input as DispatcherChainInput); @@ -87,6 +94,8 @@ pub fn dispatcher_chain(input: TokenStream) -> TokenStream { let command_name_str = command_name.value(); + let comp_entry = get_comp_entry(&pack); + let expanded = if use_default { // For default case, use ThisProgram quote! { @@ -95,6 +104,8 @@ pub fn dispatcher_chain(input: TokenStream) -> TokenStream { ::mingling::macros::pack!(ThisProgram, #pack = Vec<String>); + #comp_entry + impl ::mingling::Dispatcher<ThisProgram> for #command_struct { fn node(&self) -> ::mingling::Node { ::mingling::macros::node!(#command_name_str) @@ -115,6 +126,8 @@ pub fn dispatcher_chain(input: TokenStream) -> TokenStream { ::mingling::macros::pack!(#group_name, #pack = Vec<String>); + #comp_entry + impl ::mingling::Dispatcher<#group_name> for #command_struct { fn node(&self) -> ::mingling::Node { ::mingling::macros::node!(#command_name_str) @@ -159,6 +172,8 @@ pub fn dispatcher_render(input: TokenStream) -> TokenStream { let command_name_str = command_name.value(); + let comp_entry = get_comp_entry(&pack); + let expanded = if use_default { // For default case, use ThisProgram quote! { @@ -167,6 +182,8 @@ pub fn dispatcher_render(input: TokenStream) -> TokenStream { ::mingling::macros::pack!(ThisProgram, #pack = Vec<String>); + #comp_entry + impl ::mingling::Dispatcher for #command_struct { fn node(&self) -> ::mingling::Node { ::mingling::macros::node!(#command_name_str) @@ -187,6 +204,8 @@ pub fn dispatcher_render(input: TokenStream) -> TokenStream { ::mingling::macros::pack!(#group_name, #pack = Vec<String>); + #comp_entry + impl ::mingling::Dispatcher for #command_struct { fn node(&self) -> ::mingling::Node { ::mingling::macros::node!(#command_name_str) @@ -203,3 +222,20 @@ pub fn dispatcher_render(input: TokenStream) -> TokenStream { expanded.into() } + +#[cfg(feature = "comp")] +fn get_comp_entry(entry_name: &Ident) -> proc_macro2::TokenStream { + let comp_entry = quote! { + impl ::mingling::CompletionEntry for #entry_name { + fn get_input(self) -> Vec<String> { + self.inner.clone() + } + } + }; + comp_entry +} + +#[cfg(not(feature = "comp"))] +fn get_comp_entry(_entry_name: &Ident) -> proc_macro2::TokenStream { + quote! {} +} |
