From 3572008321bff319d5ab226d0b9979fa89af498e Mon Sep 17 00:00:00 2001 From: 魏曹先生 <1992414357@qq.com> Date: Tue, 31 Mar 2026 16:57:58 +0800 Subject: Rename chain_struct macro to pack --- mingling/src/lib.rs | 2 +- mingling_macros/src/chain_struct.rs | 162 ------------------------------------ mingling_macros/src/lib.rs | 18 ++-- mingling_macros/src/pack.rs | 162 ++++++++++++++++++++++++++++++++++++ 4 files changed, 172 insertions(+), 172 deletions(-) delete mode 100644 mingling_macros/src/chain_struct.rs create mode 100644 mingling_macros/src/pack.rs diff --git a/mingling/src/lib.rs b/mingling/src/lib.rs index a0efc19..5c5ae7d 100644 --- a/mingling/src/lib.rs +++ b/mingling/src/lib.rs @@ -8,10 +8,10 @@ pub mod parser; #[allow(unused_imports)] pub mod macros { pub use mingling_macros::chain; - pub use mingling_macros::chain_struct; pub use mingling_macros::dispatcher; pub use mingling_macros::dispatcher_render; pub use mingling_macros::node; + pub use mingling_macros::pack; pub use mingling_macros::program; pub use mingling_macros::r_print; pub use mingling_macros::r_println; diff --git a/mingling_macros/src/chain_struct.rs b/mingling_macros/src/chain_struct.rs deleted file mode 100644 index 7305a67..0000000 --- a/mingling_macros/src/chain_struct.rs +++ /dev/null @@ -1,162 +0,0 @@ -//! Chain Struct Macro Implementation -//! -//! This module provides the `chain_struct!` macro for creating wrapper types -//! with automatic implementations of common traits. - -use proc_macro::TokenStream; -use quote::quote; -use syn::parse::{Parse, ParseStream}; -use syn::{Ident, Result as SynResult, Token, Type}; - -/// Parses input in the format: `TypeName = InnerType` -struct ChainStructInput { - type_name: Ident, - inner_type: Type, -} - -impl Parse for ChainStructInput { - fn parse(input: ParseStream) -> SynResult { - let type_name = input.parse()?; - input.parse::()?; - let inner_type = input.parse()?; - - Ok(ChainStructInput { - type_name, - inner_type, - }) - } -} - -pub fn chain_struct(input: TokenStream) -> TokenStream { - let ChainStructInput { - type_name, - inner_type, - } = syn::parse_macro_input!(input as ChainStructInput); - - // Generate the struct definition - #[cfg(not(feature = "serde"))] - let struct_def = quote! { - #[derive(Debug)] - pub struct #type_name { - inner: #inner_type, - } - }; - - #[cfg(feature = "serde")] - let struct_def = quote! { - #[derive(Debug, serde::Serialize)] - pub struct #type_name { - inner: #inner_type, - } - }; - - // Generate the new() method - let new_impl = quote! { - impl #type_name { - /// Creates a new instance of the wrapper type - pub fn new(inner: #inner_type) -> Self { - Self { inner } - } - } - }; - - // Generate From and Into implementations - let from_into_impl = quote! { - impl From<#inner_type> for #type_name { - fn from(inner: #inner_type) -> Self { - Self::new(inner) - } - } - - impl From<#type_name> for #inner_type { - fn from(wrapper: #type_name) -> #inner_type { - wrapper.inner - } - } - }; - - // Generate AsRef and AsMut implementations - let as_ref_impl = quote! { - impl ::std::convert::AsRef<#inner_type> for #type_name { - fn as_ref(&self) -> &#inner_type { - &self.inner - } - } - - impl ::std::convert::AsMut<#inner_type> for #type_name { - fn as_mut(&mut self) -> &mut #inner_type { - &mut self.inner - } - } - }; - - // Generate Deref and DerefMut implementations - let deref_impl = quote! { - impl ::std::ops::Deref for #type_name { - type Target = #inner_type; - - fn deref(&self) -> &Self::Target { - &self.inner - } - } - - impl ::std::ops::DerefMut for #type_name { - fn deref_mut(&mut self) -> &mut Self::Target { - &mut self.inner - } - } - }; - - // Check if the inner type implements Default by generating conditional code - let default_impl = quote! { - impl ::std::default::Default for #type_name - where - #inner_type: ::std::default::Default, - { - fn default() -> Self { - Self::new(::std::default::Default::default()) - } - } - }; - - let any_out_impl = quote! { - impl Into for #type_name { - fn into(self) -> mingling::AnyOutput { - mingling::AnyOutput::new(self) - } - } - - impl Into for #type_name { - fn into(self) -> mingling::ChainProcess { - mingling::AnyOutput::new(self).route_chain() - } - } - - impl #type_name { - /// Converts the wrapper type into a `ChainProcess` for chaining operations. - pub fn to_chain(self) -> mingling::ChainProcess { - mingling::AnyOutput::new(self).route_chain() - } - - /// Converts the wrapper type into a `ChainProcess` for rendering operations. - pub fn to_render(self) -> mingling::ChainProcess { - mingling::AnyOutput::new(self).route_renderer() - } - } - }; - - // Combine all implementations - let expanded = quote! { - #struct_def - - #new_impl - #from_into_impl - #as_ref_impl - #deref_impl - #default_impl - - #any_out_impl - }; - - expanded.into() -} diff --git a/mingling_macros/src/lib.rs b/mingling_macros/src/lib.rs index 13d5ddc..b0ed658 100644 --- a/mingling_macros/src/lib.rs +++ b/mingling_macros/src/lib.rs @@ -9,9 +9,9 @@ use quote::quote; use syn::parse_macro_input; mod chain; -mod chain_struct; mod dispatcher_chain; mod node; +mod pack; mod render; mod renderer; @@ -58,10 +58,10 @@ pub fn node(input: TokenStream) -> TokenStream { /// # Examples /// /// ```ignore -/// use mingling_macros::chain_struct; +/// use mingling_macros::pack; /// /// // Creates a wrapper type around String -/// chain_struct!(NameString = String); +/// pack!(NameString = String); /// /// // Usage: /// let name = NameString::new("Hello".to_string()); @@ -71,8 +71,8 @@ pub fn node(input: TokenStream) -> TokenStream { /// let chain_process = name2.to_chain(); // Convert to ChainProcess /// ``` #[proc_macro] -pub fn chain_struct(input: TokenStream) -> TokenStream { - chain_struct::chain_struct(input) +pub fn pack(input: TokenStream) -> TokenStream { + pack::pack(input) } /// Creates a dispatcher chain for command execution. @@ -83,11 +83,11 @@ pub fn chain_struct(input: TokenStream) -> TokenStream { /// /// # Syntax /// -/// `dispatcher!("command_name", CommandStruct => ChainStruct)` +/// `dispatcher!("command_name", CommandStruct => Packed)` /// /// - `command_name`: A string literal representing the command name /// - `CommandStruct`: The name of the dispatcher struct to generate -/// - `ChainStruct`: The name of the chain wrapper struct to generate +/// - `Packed`: The name of the chain wrapper struct to generate /// /// # Examples /// @@ -115,11 +115,11 @@ pub fn dispatcher(input: TokenStream) -> TokenStream { /// /// # Syntax /// -/// `dispatcher_render!("command_name", CommandStruct => ChainStruct)` +/// `dispatcher_render!("command_name", CommandStruct => Packed)` /// /// - `command_name`: A string literal representing the command name /// - `CommandStruct`: The name of the dispatcher struct to generate -/// - `ChainStruct`: The name of the chain wrapper struct to generate +/// - `Packed`: The name of the chain wrapper struct to generate /// /// # Examples /// diff --git a/mingling_macros/src/pack.rs b/mingling_macros/src/pack.rs new file mode 100644 index 0000000..bff67d4 --- /dev/null +++ b/mingling_macros/src/pack.rs @@ -0,0 +1,162 @@ +//! Chain Struct Macro Implementation +//! +//! This module provides the `pack!` macro for creating wrapper types +//! with automatic implementations of common traits. + +use proc_macro::TokenStream; +use quote::quote; +use syn::parse::{Parse, ParseStream}; +use syn::{Ident, Result as SynResult, Token, Type}; + +/// Parses input in the format: `TypeName = InnerType` +struct PackInput { + type_name: Ident, + inner_type: Type, +} + +impl Parse for PackInput { + fn parse(input: ParseStream) -> SynResult { + let type_name = input.parse()?; + input.parse::()?; + let inner_type = input.parse()?; + + Ok(PackInput { + type_name, + inner_type, + }) + } +} + +pub fn pack(input: TokenStream) -> TokenStream { + let PackInput { + type_name, + inner_type, + } = syn::parse_macro_input!(input as PackInput); + + // Generate the struct definition + #[cfg(not(feature = "serde"))] + let struct_def = quote! { + #[derive(Debug)] + pub struct #type_name { + inner: #inner_type, + } + }; + + #[cfg(feature = "serde")] + let struct_def = quote! { + #[derive(Debug, serde::Serialize)] + pub struct #type_name { + inner: #inner_type, + } + }; + + // Generate the new() method + let new_impl = quote! { + impl #type_name { + /// Creates a new instance of the wrapper type + pub fn new(inner: #inner_type) -> Self { + Self { inner } + } + } + }; + + // Generate From and Into implementations + let from_into_impl = quote! { + impl From<#inner_type> for #type_name { + fn from(inner: #inner_type) -> Self { + Self::new(inner) + } + } + + impl From<#type_name> for #inner_type { + fn from(wrapper: #type_name) -> #inner_type { + wrapper.inner + } + } + }; + + // Generate AsRef and AsMut implementations + let as_ref_impl = quote! { + impl ::std::convert::AsRef<#inner_type> for #type_name { + fn as_ref(&self) -> &#inner_type { + &self.inner + } + } + + impl ::std::convert::AsMut<#inner_type> for #type_name { + fn as_mut(&mut self) -> &mut #inner_type { + &mut self.inner + } + } + }; + + // Generate Deref and DerefMut implementations + let deref_impl = quote! { + impl ::std::ops::Deref for #type_name { + type Target = #inner_type; + + fn deref(&self) -> &Self::Target { + &self.inner + } + } + + impl ::std::ops::DerefMut for #type_name { + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.inner + } + } + }; + + // Check if the inner type implements Default by generating conditional code + let default_impl = quote! { + impl ::std::default::Default for #type_name + where + #inner_type: ::std::default::Default, + { + fn default() -> Self { + Self::new(::std::default::Default::default()) + } + } + }; + + let any_out_impl = quote! { + impl Into for #type_name { + fn into(self) -> mingling::AnyOutput { + mingling::AnyOutput::new(self) + } + } + + impl Into for #type_name { + fn into(self) -> mingling::ChainProcess { + mingling::AnyOutput::new(self).route_chain() + } + } + + impl #type_name { + /// Converts the wrapper type into a `ChainProcess` for chaining operations. + pub fn to_chain(self) -> mingling::ChainProcess { + mingling::AnyOutput::new(self).route_chain() + } + + /// Converts the wrapper type into a `ChainProcess` for rendering operations. + pub fn to_render(self) -> mingling::ChainProcess { + mingling::AnyOutput::new(self).route_renderer() + } + } + }; + + // Combine all implementations + let expanded = quote! { + #struct_def + + #new_impl + #from_into_impl + #as_ref_impl + #deref_impl + #default_impl + + #any_out_impl + }; + + expanded.into() +} -- cgit