2 stable releases
| 2.1.0 | Sep 11, 2025 |
|---|---|
| 1.0.0 | Jan 16, 2025 |
#130 in Template engine
199 downloads per month
190KB
2.5K
SLoC
🔷 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
🔧 VSCode Extension
Tron includes a VSCode extension for enhanced template editing:
Features
- Syntax Highlighting - Full support for
.tronand.tplfiles - Smart Autocompletion - Placeholder suggestions and snippets
- Template Validation - Real-time error detection and linting
- Live Preview - See rendered templates instantly
Installation
The extension is located in the tron_vscode/ directory. To install:
cd tron_vscode
npm install
npm run compile
code --install-extension ./tron-template-engine-1.0.0.vsix
Configuration
The extension provides several configuration options:
{
"tron.validation.enabled": true,
"tron.validation.minPlaceholderLength": 2,
"tron.validation.checkTrailingWhitespace": false,
"tron.preview.autoRefresh": true,
"tron.snippets.enabled": true
}
Note: Trailing whitespace checking is disabled by default to prevent false positives. Enable it only if needed for your specific use case.
🤝 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.2–8.5MB
~182K SLoC