From 7ce68cd11516bd7cf037ecea99a92aee7c31b2c3 Mon Sep 17 00:00:00 2001 From: 魏曹先生 <1992414357@qq.com> Date: Sat, 28 Mar 2026 00:47:46 +0800 Subject: Add initial Mingling framework codebase --- mingling_macros/src/node.rs | 60 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 mingling_macros/src/node.rs (limited to 'mingling_macros/src/node.rs') diff --git a/mingling_macros/src/node.rs b/mingling_macros/src/node.rs new file mode 100644 index 0000000..3d9473f --- /dev/null +++ b/mingling_macros/src/node.rs @@ -0,0 +1,60 @@ +//! Command Node Macro Implementation +//! +//! This module provides the `node` procedural macro for creating +//! command nodes from dot-separated path strings. + +use just_fmt::kebab_case; +use proc_macro::TokenStream; +use proc_macro2::TokenStream as TokenStream2; +use quote::quote; +use syn::parse::{Parse, ParseStream}; +use syn::{LitStr, Result as SynResult}; + +/// Parses a string literal input for the node macro +struct NodeInput { + path: LitStr, +} + +impl Parse for NodeInput { + fn parse(input: ParseStream) -> SynResult { + Ok(NodeInput { + path: input.parse()?, + }) + } +} + +/// Implementation of the `node` procedural macro +/// +/// # Examples +/// +/// ```ignore +/// use mingling_macros::node; +/// +/// // Creates: Node::default().join("root").join("subcommand").join("action") +/// let node = node!("root.subcommand.action"); +/// ``` +pub fn node(input: TokenStream) -> TokenStream { + // Parse the input as a string literal + let input_parsed = syn::parse_macro_input!(input as NodeInput); + let path_str = input_parsed.path.value(); + + // Split the path by dots + let parts: Vec = path_str + .split('.') + .map(|s| kebab_case!(s).to_string()) + .collect(); + + // Build the expression starting from Node::default() + let mut expr: TokenStream2 = quote! { + mingling::Node::default() + }; + + // Add .join() calls for each part of the path + for part in parts { + expr = quote! { + #expr.join(#part) + }; + } + + expr.into() +} -- cgit