1 stable release
new 1.0.0 | Jan 16, 2025 |
---|
#91 in Template engine
14KB
205 lines
🔷 Tron
A powerful, composable template system for Rust
Tron is a modern template engine that brings composability and type safety to code generation. Build and compose templates with confidence, execute them with rust-script, and generate Rust code dynamically.
✨ Features
- 🧩 Composable Templates - Nest and combine templates seamlessly
- 🎯 Type-Safe - Catch template errors at compile time
- 🔌 rust-script Integration - Execute generated code directly
- 📦 Dependency Management - Handle external crate dependencies gracefully
- 🛠 Rich Tooling - Comprehensive error handling and debugging
🚀 Quick Start
Add Tron to your project:
[dependencies]
tron = "0.1.0"
[features]
default = []
execute = ["tempfile", "which"] # Optional: For rust-script support
Create your first template:
use tron::{TronTemplate, TronRef};
// Create a template
let mut template = TronTemplate::new(r#"
fn @[name]@() {
println!("@[message]@");
}
"#)?;
let mut template_ref = TronRef::new(template);
// Set values
template_ref.set("name", "greet")?;
template_ref.set("message", "Hello from Tron!")?;
// Render
let code = template_ref.render()?;
🎭 Template Composition
Tron shines when composing templates:
// Create a module template
let mut module = TronTemplate::new(r#"
mod @[name]@ {
@[content]@
}
"#)?;
// Create a function template
let mut function = TronTemplate::new(r#"
fn @[func_name]@() {
@[body]@
}
"#)?;
// Compose them
let mut module_ref = TronRef::new(module);
let mut function_ref = TronRef::new(function);
function_ref.set("func_name", "example")?;
function_ref.set("body", "println!(\"Composed!\");")?;
module_ref.set("name", "generated")?;
module_ref.set_ref("content", function_ref)?;
// Renders:
// mod generated {
// fn example() {
// println!("Composed!");
// }
// }
🎯 Key Concepts
Templates
Templates are the building blocks of Tron. They use a simple @[placeholder]@
syntax:
let template = TronTemplate::new("fn @[name]@() -> @[return_type]@ { @[body]@ }")?;
Template References
TronRef
wraps templates with additional capabilities:
let template_ref = TronRef::new(template)
.with_dependency("serde = \"1.0\"");
Template Assembly
Combine multiple templates with TronAssembler
:
let mut assembler = TronAssembler::new();
assembler.add_template(header_ref);
assembler.add_template(body_ref);
assembler.add_template(footer_ref);
let combined = assembler.render_all()?;
📚 Documentation
Visit our documentation for:
- Detailed API reference
- Advanced usage examples
- Best practices
- Tutorial guides
🤝 Contributing
We welcome contributions! Check out our Contributing Guide to get started.
- Fork the repository
- Create a feature branch
- Submit a pull request
📝 License
Tron is MIT licensed. See LICENSE for details.
Dependencies
~2–13MB
~163K SLoC