### Quick start: template, parse, and format Source: https://github.com/aacebo/moxy/blob/master/README.md Demonstrates the core workflow: creating a token stream with template!, parsing it into an AST item, and formatting it back to a string. Requires the template, ast, and fmt features. ```rust use moxy::ast::Item; let name = "Widget"; let tokens = moxy::template! { pub struct {{ name }}; }; let item: Item = moxy::parse!(tokens).unwrap(); assert_eq!(item.as_struct().unwrap().ident.text(), "Widget"); assert_eq!(moxy::fmt!(&item).unwrap(), "pub struct Widget;"); ``` -------------------------------- ### Add moxy with template and fmt features Source: https://github.com/aacebo/moxy/blob/master/README.md Adds moxy with the template and fmt features enabled. This is the recommended way to get started with the library. ```console cargo add moxy --features template,fmt ``` -------------------------------- ### Build script with moxy::build::rustc::Config Source: https://github.com/aacebo/moxy/blob/master/README.md Example build.rs script that reads rustc configuration, sets a minimum version, checks a cfg, reruns on changes, and conditionally enables the nightly cfg. Requires the build feature. ```rust // build.rs fn main() -> Result<(), Box> { let mut config = moxy::build::rustc::Config::read()?; config .min_version("1.85.0") .check_cfg("cfg(nightly)") .rerun_if_changed("build.rs"); if config.version().channel.is_nightly() { config.cfg("nightly"); } config.emit(); Ok(()) } ``` -------------------------------- ### Add moxy with minimal features Source: https://github.com/aacebo/moxy/blob/master/README.md Adds moxy with only the token and ast features, disabling default features. Use this to minimize dependencies when you only need token streams and AST parsing. ```console cargo add moxy --no-default-features --features token,ast ``` -------------------------------- ### Format AST with FmtConfig and fmt! Source: https://github.com/aacebo/moxy/blob/master/README.md Formats a parsed AST with configurable indentation using the fmt feature. The FmtConfig is set to use two spaces for indentation. ```rust use moxy::ast::Item; use moxy::fmt::{FmtConfig, Indent}; let item: Item = moxy::parse!("struct User { id: u64, name: String }").unwrap(); let config = FmtConfig::default().with_indent(Indent::space(2)); let output = moxy::fmt!(&item, config).unwrap(); assert_eq!(output, "struct User {\n id: u64,\n name: String,\n}"); ``` -------------------------------- ### Construct tokens with ident! and Token Source: https://github.com/aacebo/moxy/blob/master/README.md Shows how to construct tokens using the ident! macro and the Token type. The token feature is the foundation, similar to proc-macro2. ```rust use moxy::Token; use moxy::token::ident; let name = ident!(Generated, "_", Item); let comma: Token![,] = Default::default(); assert_eq!(name.to_string(), "Generated_Item"); assert_eq!(comma.as_str(), ","); ``` -------------------------------- ### Create diagnostics with error! and help! Source: https://github.com/aacebo/moxy/blob/master/README.md Builds a span-aware diagnostic with a help message using the error! and help! macros. The emit() method produces a token stream that contains compile_error. ```rust let tokens = moxy::error!( "missing template", [moxy::help!("add #[template { ... }]")], ) .emit(); assert!(tokens.to_string().contains("compile_error")); ``` -------------------------------- ### Add moxy with derive feature Source: https://github.com/aacebo/moxy/blob/master/README.md Adds moxy with the derive feature enabled, which provides the #[derive(ToTokens)] macro. ```console cargo add moxy --features derive ``` -------------------------------- ### Generate token streams with template! and @for Source: https://github.com/aacebo/moxy/blob/master/README.md Builds a token stream with interpolation and control flow using the template! macro. The @for loop iterates over the fields array and generates struct fields. ```rust let fields = ["id", "name"]; let tokens = moxy::template! { struct User { @for (field in fields) { {{ field }}: String, } } }; assert!(tokens.to_string().contains("struct User")); ``` -------------------------------- ### Add moxy as build dependency Source: https://github.com/aacebo/moxy/blob/master/README.md Adds moxy as a build dependency with only the build feature enabled. This is required for using the build helpers in a build script. ```console cargo add moxy --build --no-default-features --features build ``` -------------------------------- ### Parse AST nodes with moxy::parse! Source: https://github.com/aacebo/moxy/blob/master/README.md Parses Rust syntax into typed AST nodes (Item, Type, Expr) using the ast feature. Follows the same parse-at-the-level-you-need style as syn. ```rust use moxy::ast::{Expr, Item, Type}; let item: Item = moxy::parse!("pub struct User { id: u64 }").unwrap(); let ty: Type = moxy::parse!("Option>").unwrap(); let expr: Expr = moxy::parse!("items.next()?").unwrap(); assert!(item.is_struct()); assert!(ty.is_path()); assert!(expr.is_unary()); ``` -------------------------------- ### Create identifiers with paste! Source: https://github.com/aacebo/moxy/blob/master/README.md Creates identifiers at expansion time using the paste! macro. The identifier is formed by concatenating the parts read_ and value. ```rust moxy::paste! { fn {{ read_ value }}() -> u32 { 7 } } assert_eq!(read_value(), 7); ``` -------------------------------- ### Derive ToTokens with inline template Source: https://github.com/aacebo/moxy/blob/master/README.md Implements ToTokens for a struct using an inline template with the derive feature. The template interpolates self.value into the generated token stream. ```rust use moxy::token::ToTokenStream; #[derive(moxy::ToTokens)] #[moxy(template { const VALUE: &str = {{ self.value }}; })] struct Generated { value: String, } let tokens = Generated { value: "seven".into() }.to_token_stream(); assert!(tokens.to_string().contains("VALUE")); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.