zyn
A template engine for Rust procedural macros. Write quote!-like code with control flow, interpolation pipes, and composable elements.
Install
[]
= "0.0.0"
Quick Start
use *;
// Generate a greeting function with a configurable name
let name = format_ident!;
let is_pub = true;
let output: TokenStream = zyn! ;
// output: pub fn hello_world() { println!("hello!"); }
Template Syntax
Interpolation
Insert any expression that implements ToTokens:
zyn!
Supports field access, method calls, and any Rust expression:
zyn!
Pipes
Transform interpolated values with pipes. Reference them in snake_case — they resolve to PascalCase structs automatically:
zyn!
Chain pipes:
zyn! // HelloWorld -> HELLO_WORLD
Format pipes
Pipes can take arguments via : syntax. The ident and fmt pipes use a {} placeholder:
zyn!
Control Flow
Conditionals
zyn!
Conditions support field access and method calls:
zyn!
Loops
zyn!
// output: pub x: f64, pub y: f64, pub z: f64,
Pattern Matching
zyn!
Compile Errors
zyn!
Elements
Reusable template components. Define with #[element], invoke with @:
// Generates struct FieldDecl, referenced as @field_decl in templates:
zyn!
// output: pub age: u32,
Elements can have zero parameters. Parentheses are optional when there are no props:
// All equivalent:
zyn!
zyn!
Elements support children via a children parameter:
zyn!
Children-only elements can omit parens entirely:
zyn!
Custom Pipes
Define with #[pipe]:
// Generates struct Prefix, used as {{ name | prefix }}:
zyn!
Custom Names
Override the template name for elements and pipes:
...
// Referenced as @my_component in templates (resolves to MyComponent struct)
Case Conversion Utilities
The case module and macros are available for use outside templates:
use case;
to_snake // "hello_world"
to_pascal // "HelloWorld"
to_camel // "helloWorld"
to_screaming // "HELLO_WORLD"
to_kebab // "hello-world"
// As macros (also work on syn::Ident):
pascal! // "HelloWorld"
pascal! // syn::Ident in PascalCase
snake! // syn::Ident in snake_case
License
MIT