aboutsummaryrefslogtreecommitdiff
path: root/mingling_macros/src/derive/grouped.rs
diff options
context:
space:
mode:
author魏曹先生 <1992414357@qq.com>2026-07-20 00:59:17 +0800
committer魏曹先生 <1992414357@qq.com>2026-07-20 01:01:27 +0800
commit1f583f625a7d541dc7d47a8136b31d29a5ed5441 (patch)
tree795006c8c2a9f10c866951de5aba318a64007219 /mingling_macros/src/derive/grouped.rs
parente8ae63988d562969df47ae6d92129974e7fb6980 (diff)
chore(macros): reorganize module hierarchy in mingling_macros
Restructure flat module layout into logical directories (attr/, derive/, func/, systems/, extensions/, utils.rs). Tighten internal function visibility to pub(crate). All public API signatures remain unchanged.
Diffstat (limited to 'mingling_macros/src/derive/grouped.rs')
-rw-r--r--mingling_macros/src/derive/grouped.rs79
1 files changed, 79 insertions, 0 deletions
diff --git a/mingling_macros/src/derive/grouped.rs b/mingling_macros/src/derive/grouped.rs
new file mode 100644
index 0000000..307aab6
--- /dev/null
+++ b/mingling_macros/src/derive/grouped.rs
@@ -0,0 +1,79 @@
+use proc_macro::TokenStream;
+use quote::quote;
+use syn::{DeriveInput, Ident, parse_macro_input};
+
+pub(crate) fn derive_grouped(input: TokenStream) -> TokenStream {
+ // Parse the input struct/enum
+ let input = parse_macro_input!(input as DeriveInput);
+ let struct_name = input.ident;
+
+ let group_ident: proc_macro2::TokenStream = crate::default_program_path();
+
+ let any_output_convert_impls =
+ proc_macro2::TokenStream::from(build_any_output_convert_impls(&struct_name, &group_ident));
+
+ // Generate the Grouped trait implementation
+ let expanded = quote! {
+ ::mingling::macros::register_type!(#struct_name);
+
+ impl ::mingling::Grouped<#group_ident> for #struct_name {
+ fn member_id() -> #group_ident {
+ #group_ident::#struct_name
+ }
+ }
+
+ #any_output_convert_impls
+ };
+
+ expanded.into()
+}
+
+#[cfg(feature = "structural_renderer")]
+pub fn derive_grouped_serialize(input: TokenStream) -> TokenStream {
+ // Parse the input struct/enum
+ let input_parsed = parse_macro_input!(input as DeriveInput);
+ let struct_name = input_parsed.ident.clone();
+
+ let group_ident: proc_macro2::TokenStream = crate::default_program_path();
+
+ let any_output_convert_impls =
+ proc_macro2::TokenStream::from(build_any_output_convert_impls(&struct_name, &group_ident));
+
+ // Generate both Serialize and Grouped implementations
+ let expanded = quote! {
+ #[derive(serde::Serialize)]
+ #input_parsed
+
+ ::mingling::macros::register_type!(#struct_name);
+
+ impl ::mingling::Grouped<#group_ident> for #struct_name {
+ fn member_id() -> #group_ident {
+ #group_ident::#struct_name
+ }
+ }
+
+ #any_output_convert_impls
+ };
+
+ expanded.into()
+}
+
+fn build_any_output_convert_impls(
+ struct_name: &Ident,
+ group_ident: &proc_macro2::TokenStream,
+) -> TokenStream {
+ quote! {
+ impl ::std::convert::Into<::mingling::AnyOutput<#group_ident>> for #struct_name {
+ fn into(self) -> ::mingling::AnyOutput<#group_ident> {
+ ::mingling::AnyOutput::new(self)
+ }
+ }
+
+ impl ::std::convert::Into<::mingling::ChainProcess<#group_ident>> for #struct_name {
+ fn into(self) -> ::mingling::ChainProcess<#group_ident> {
+ ::mingling::AnyOutput::new(self).route_chain()
+ }
+ }
+ }
+ .into()
+}