diff options
| author | 魏曹先生 <1992414357@qq.com> | 2026-05-16 22:08:39 +0800 |
|---|---|---|
| committer | 魏曹先生 <1992414357@qq.com> | 2026-05-16 22:08:39 +0800 |
| commit | c48149a86eafcca967ef9aa947864fb4d0c727ea (patch) | |
| tree | c794eaef4435cb0f4ec8524861ebf3165f660097 /mingling_macros/src | |
| parent | 5fe400c006d9d143aa7b977cb13f95e7380754a5 (diff) | |
Replace hardcoded `ThisProgram` ident with shared constant
Diffstat (limited to 'mingling_macros/src')
| -rw-r--r-- | mingling_macros/src/chain.rs | 10 | ||||
| -rw-r--r-- | mingling_macros/src/dispatcher.rs | 5 | ||||
| -rw-r--r-- | mingling_macros/src/dispatcher_clap.rs | 2 | ||||
| -rw-r--r-- | mingling_macros/src/groupped.rs | 6 | ||||
| -rw-r--r-- | mingling_macros/src/lib.rs | 8 | ||||
| -rw-r--r-- | mingling_macros/src/pack.rs | 22 | ||||
| -rw-r--r-- | mingling_macros/src/program_setup.rs | 10 |
7 files changed, 42 insertions, 21 deletions
diff --git a/mingling_macros/src/chain.rs b/mingling_macros/src/chain.rs index 134b3c2..47f32e3 100644 --- a/mingling_macros/src/chain.rs +++ b/mingling_macros/src/chain.rs @@ -5,6 +5,8 @@ use syn::{ FnArg, Ident, ItemFn, Pat, PatType, ReturnType, Signature, Type, TypePath, parse_macro_input, }; +use crate::DEFAULT_PROGRAM_NAME; + /// Extracted information about a resource injection parameter struct ResourceInjection { var_name: Ident, @@ -132,11 +134,13 @@ fn extract_args_info(sig: &Signature) -> syn::Result<(Pat, TypePath, Vec<Resourc } pub fn chain_attr(attr: TokenStream, item: TokenStream) -> TokenStream { + let default_program_ident = crate::default_program_ident(); + // Parse the attribute arguments (e.g., MyProgram from #[chain(MyProgram)]) // If no argument is provided, use ThisProgram let (group_name, use_crate_prefix) = if attr.is_empty() { ( - Ident::new("ThisProgram", proc_macro2::Span::call_site()), + Ident::new(DEFAULT_PROGRAM_NAME, proc_macro2::Span::call_site()), true, ) } else { @@ -231,7 +235,7 @@ pub fn chain_attr(attr: TokenStream, item: TokenStream) -> TokenStream { // Determine the program type for the return type let program_type = if use_crate_prefix { - quote! { ThisProgram } + quote! { #default_program_ident } } else { quote! { #group_name } }; @@ -386,7 +390,7 @@ pub fn chain_attr(attr: TokenStream, item: TokenStream) -> TokenStream { ::mingling::macros::register_chain!(#previous_type, #struct_name); - impl ::mingling::Chain<ThisProgram> for #struct_name { + impl ::mingling::Chain<#default_program_ident> for #struct_name { type Previous = #previous_type; #proc_fn diff --git a/mingling_macros/src/dispatcher.rs b/mingling_macros/src/dispatcher.rs index 915f5db..64f0339 100644 --- a/mingling_macros/src/dispatcher.rs +++ b/mingling_macros/src/dispatcher.rs @@ -11,6 +11,7 @@ use syn::{Ident, Result as SynResult, Token}; #[cfg(feature = "dispatch_tree")] use crate::COMPILE_TIME_DISPATCHERS; +use crate::DEFAULT_PROGRAM_NAME; enum DispatcherChainInput { Explicit { @@ -88,7 +89,7 @@ pub fn dispatcher(input: TokenStream) -> TokenStream { command_struct, pack, } => ( - Ident::new("ThisProgram", proc_macro2::Span::call_site()), + Ident::new(DEFAULT_PROGRAM_NAME, proc_macro2::Span::call_site()), command_name, command_struct, pack, @@ -104,7 +105,7 @@ pub fn dispatcher(input: TokenStream) -> TokenStream { let expanded = { let program_ident = if use_default { - Ident::new("ThisProgram", proc_macro2::Span::call_site()) + Ident::new(DEFAULT_PROGRAM_NAME, proc_macro2::Span::call_site()) } else { group_name.clone() }; diff --git a/mingling_macros/src/dispatcher_clap.rs b/mingling_macros/src/dispatcher_clap.rs index 37c662f..2a1ac44 100644 --- a/mingling_macros/src/dispatcher_clap.rs +++ b/mingling_macros/src/dispatcher_clap.rs @@ -144,7 +144,7 @@ pub fn dispatcher_clap_attr(attr: TokenStream, item: TokenStream) -> TokenStream error_struct: options.error_struct.clone(), help_enabled: options.help_enabled, }, - Ident::new("ThisProgram", proc_macro2::Span::call_site()), + Ident::new(DEFAULT_PROGRAM_NAME, proc_macro2::Span::call_site()), ), DispatcherClapInput::Explicit { group_name, diff --git a/mingling_macros/src/groupped.rs b/mingling_macros/src/groupped.rs index 3c30827..4fd0665 100644 --- a/mingling_macros/src/groupped.rs +++ b/mingling_macros/src/groupped.rs @@ -3,6 +3,8 @@ use proc_macro2::Span; use quote::quote; use syn::{Attribute, DeriveInput, Ident, parse_macro_input}; +use crate::DEFAULT_PROGRAM_NAME; + /// Parses the `#[group(...)]` attribute to extract the group type fn parse_group_attribute(attrs: &[Attribute]) -> Option<Ident> { for attr in attrs { @@ -24,7 +26,7 @@ pub fn derive_groupped(input: TokenStream) -> TokenStream { // Parse attributes to find #[group(...)] let group_ident = parse_group_attribute(&input.attrs) - .unwrap_or_else(|| Ident::new("ThisProgram", Span::call_site())); + .unwrap_or_else(|| Ident::new(DEFAULT_PROGRAM_NAME, Span::call_site())); let any_output_convert_impls = proc_macro2::TokenStream::from(build_any_output_convert_impls( struct_name.clone(), @@ -55,7 +57,7 @@ pub fn derive_groupped_serialize(input: TokenStream) -> TokenStream { // Parse attributes to find #[group(...)] let group_ident = parse_group_attribute(&input_parsed.attrs) - .unwrap_or_else(|| Ident::new("ThisProgram", Span::call_site())); + .unwrap_or_else(|| Ident::new(DEFAULT_PROGRAM_NAME, Span::call_site())); let any_output_convert_impls = proc_macro2::TokenStream::from(build_any_output_convert_impls( struct_name.clone(), diff --git a/mingling_macros/src/lib.rs b/mingling_macros/src/lib.rs index 175c7e5..7dd2fe2 100644 --- a/mingling_macros/src/lib.rs +++ b/mingling_macros/src/lib.rs @@ -44,6 +44,12 @@ mod renderer; #[cfg(feature = "comp")] mod suggest; +pub(crate) const DEFAULT_PROGRAM_NAME: &str = "ThisProgram"; + +pub(crate) fn default_program_ident() -> Ident { + Ident::new(DEFAULT_PROGRAM_NAME, proc_macro2::Span::call_site()) +} + // Global variables #[cfg(feature = "general_renderer")] pub(crate) static GENERAL_RENDERERS: Lazy<Mutex<BTreeSet<String>>> = @@ -1549,7 +1555,7 @@ pub fn suggest_enum(input: TokenStream) -> TokenStream { fn read_name(input: &TokenStream) -> Ident { if input.is_empty() { - Ident::new("ThisProgram", proc_macro2::Span::call_site()) + Ident::new(DEFAULT_PROGRAM_NAME, proc_macro2::Span::call_site()) } else { syn::parse(input.clone()).unwrap() } diff --git a/mingling_macros/src/pack.rs b/mingling_macros/src/pack.rs index 10a34e4..2242082 100644 --- a/mingling_macros/src/pack.rs +++ b/mingling_macros/src/pack.rs @@ -3,6 +3,8 @@ use quote::quote; use syn::parse::{Parse, ParseStream}; use syn::{Ident, Result as SynResult, Token, Type}; +use crate::DEFAULT_PROGRAM_NAME; + enum PackInput { Explicit { group_name: Ident, @@ -50,6 +52,8 @@ impl Parse for PackInput { } pub fn pack(input: TokenStream) -> TokenStream { + let default_program_ident = crate::default_program_ident(); + // Parse the input let pack_input = syn::parse_macro_input!(input as PackInput); @@ -64,7 +68,7 @@ pub fn pack(input: TokenStream) -> TokenStream { type_name, inner_type, } => ( - Ident::new("ThisProgram", proc_macro2::Span::call_site()), + Ident::new(DEFAULT_PROGRAM_NAME, proc_macro2::Span::call_site()), type_name, inner_type, true, @@ -207,13 +211,13 @@ pub fn pack(input: TokenStream) -> TokenStream { #default_impl #register_impl - impl Into<mingling::AnyOutput<ThisProgram>> for #type_name { - fn into(self) -> mingling::AnyOutput<ThisProgram> { + impl Into<mingling::AnyOutput<#default_program_ident>> for #type_name { + fn into(self) -> mingling::AnyOutput<#default_program_ident> { mingling::AnyOutput::new(self) } } - impl From<#type_name> for mingling::ChainProcess<ThisProgram> { + impl From<#type_name> for mingling::ChainProcess<#default_program_ident> { fn from(value: #type_name) -> Self { mingling::AnyOutput::new(value).route_chain() } @@ -221,19 +225,19 @@ pub fn pack(input: TokenStream) -> TokenStream { impl #type_name { /// Converts the wrapper type into a `ChainProcess` for chaining operations. - pub fn to_chain(self) -> mingling::ChainProcess<ThisProgram> { + pub fn to_chain(self) -> mingling::ChainProcess<#default_program_ident> { mingling::AnyOutput::new(self).route_chain() } /// Converts the wrapper type into a `ChainProcess` for rendering operations. - pub fn to_render(self) -> mingling::ChainProcess<ThisProgram> { + pub fn to_render(self) -> mingling::ChainProcess<#default_program_ident> { mingling::AnyOutput::new(self).route_renderer() } } - impl ::mingling::Groupped<ThisProgram> for #type_name { - fn member_id() -> ThisProgram { - ThisProgram::#type_name + impl ::mingling::Groupped<#default_program_ident> for #type_name { + fn member_id() -> #default_program_ident { + #default_program_ident::#type_name } } } diff --git a/mingling_macros/src/program_setup.rs b/mingling_macros/src/program_setup.rs index 60a11a9..57b7e6c 100644 --- a/mingling_macros/src/program_setup.rs +++ b/mingling_macros/src/program_setup.rs @@ -3,6 +3,8 @@ use quote::quote; use syn::spanned::Spanned; use syn::{FnArg, Ident, ItemFn, Pat, PatType, ReturnType, Signature, Type, parse_macro_input}; +use crate::DEFAULT_PROGRAM_NAME; + /// Extracts the program parameter from function arguments fn extract_program_param(sig: &Signature) -> syn::Result<(Pat, Type)> { // The function should have exactly one parameter @@ -48,11 +50,13 @@ fn extract_return_type(sig: &Signature) -> syn::Result<()> { } pub fn setup_attr(attr: TokenStream, item: TokenStream) -> TokenStream { + let default_program_ident = crate::default_program_ident(); + // Parse the attribute arguments (e.g., MyProgram from #[program_setup(MyProgram)]) // If no argument is provided, use ThisProgram let (program_name, use_crate_prefix) = if attr.is_empty() { ( - Ident::new("ThisProgram", proc_macro2::Span::call_site()), + Ident::new(DEFAULT_PROGRAM_NAME, proc_macro2::Span::call_site()), true, ) } else { @@ -106,8 +110,8 @@ pub fn setup_attr(attr: TokenStream, item: TokenStream) -> TokenStream { #[doc(hidden)] #vis struct #struct_name; - impl ::mingling::setup::ProgramSetup<ThisProgram> for #struct_name { - fn setup(&mut self, program: &mut ::mingling::Program<ThisProgram>) { + impl ::mingling::setup::ProgramSetup<#default_program_ident> for #struct_name { + fn setup(&mut self, program: &mut ::mingling::Program<#default_program_ident>) { // Call the original function with the actual Program type #fn_name(program); } |
