Crate tron

Crate tron 

Source
Expand description

§Tron - A Powerful Rust Template Engine

Tron is a modern, composable template engine designed for generating Rust code and other text-based content. It provides a simple yet powerful syntax for creating templates with placeholders that can be filled dynamically.

§Features

  • Simple Syntax: Uses @[placeholder]@ delimiters for easy template creation
  • Composable: Nest templates within other templates seamlessly
  • Type-Safe: Comprehensive error handling with descriptive error types
  • Execution: Optional rust-script integration for running generated code
  • Dependency Management: Handle external crate dependencies in templates
  • File Loading: Load templates from files with automatic path tracking
  • Assembly: Combine multiple templates into complex structures

§Quick Start

use tron::{TronTemplate, TronRef};

// Create a simple template
let mut template = TronTemplate::new("fn @[name]@() {\n    @[body]@\n}").unwrap();
template.set("name", "greet").unwrap();
template.set("body", "println!(\"Hello, World!\");").unwrap();

let result = template.render().unwrap();
println!("{}", result);

// Use TronRef for more advanced features
let template_ref = TronRef::new(template)
    .with_dependency("serde = \"1.0\"");

§Template Composition

Templates can be composed together for complex code generation:

use tron::{TronTemplate, TronRef};

// Create outer template
let outer = TronTemplate::new("mod @[module_name]@ {\n    @[content]@\n}").unwrap();
let mut outer_ref = TronRef::new(outer);

// Create inner template
let inner = TronTemplate::new("pub fn @[func_name]@() -> &'static str {\n    @[body]@\n}").unwrap();
let mut inner_ref = TronRef::new(inner);

// Fill inner template
inner_ref.set("func_name", "get_message").unwrap();
inner_ref.set("body", "\"Hello from composed template!\"").unwrap();

// Compose templates
outer_ref.set("module_name", "generated").unwrap();
outer_ref.set_ref("content", inner_ref).unwrap();

let result = outer_ref.render().unwrap();

§Assembly

For complex multi-part generation, use TronAssembler:

use tron::{TronTemplate, TronRef, TronAssembler};

let mut assembler = TronAssembler::new();

// Add header
let header = TronTemplate::new("// Generated code\nuse std::collections::HashMap;").unwrap();
assembler.add_template(TronRef::new(header));

// Add function
let func = TronTemplate::new("fn @[name]@() {\n    @[body]@\n}").unwrap();
let mut func_ref = TronRef::new(func);
func_ref.set("name", "example").unwrap();
func_ref.set("body", "println!(\"Generated function!\");").unwrap();
assembler.add_template(func_ref);

let result = assembler.render_all().unwrap();

§Compile-time Templates

Use macros for compile-time template generation:

use tron::{template, template_ref, assemble_templates, generate_code};

// Create template from literal
let tmpl = template!("fn @[name]@() { @[body]@ }");

// Create template reference with dependencies
let tmpl_ref = template_ref!(
    "use serde::Serialize;\n#[derive(Serialize)]\nstruct @[name]@ {}",
    dependencies = ["serde = \"1.0\""]
);

// Assemble multiple templates
let assembler = assemble_templates![
    "// Generated code",
    "mod @[module_name]@ {",
    "    @[module_content]@",
    "}"
];

// Generate code at compile time
let code = generate_code!(
    template = "const @[name]@: @[type]@ = @[value]@;",
    placeholders = {
        "name" => "PI",
        "type" => "f64",
        "value" => "3.14159"
    }
);

§Builder Patterns

For more ergonomic template construction, use the builder patterns:

use tron::{TronTemplateBuilder, TronRefBuilder};
use std::collections::HashMap;

// Build a template with the fluent API
let template = TronTemplateBuilder::new()
    .content("fn @[name]@(@[params]@) -> @[return_type]@ {\n    @[body]@\n}")
    .set("name", "add")
    .set("params", "a: i32, b: i32")
    .set("return_type", "i32")
    .set("body", "a + b")
    .build()
    .unwrap();

// Build a template reference with dependencies
let template_ref = TronRefBuilder::new()
    .content("fn main() {\n    @[body]@\n}")
    .dependencies(&["serde = \"1.0\"", "tokio = \"1.0\""])
    .set("body", "println!(\"Hello from builder!\");")
    .build()
    .unwrap();

Re-exports§

pub use error::TronError;
pub use error::Result;
pub use template::TronTemplate;
pub use template_ref::TronRef;
pub use assembler::TronAssembler;
pub use builder::TronTemplateBuilder;
pub use builder::TronRefBuilder;
pub use loader::TemplateLoader;
pub use loader::LoaderConfig;
pub use loader::TemplateMetadata;
pub use cache::TemplateCache;
pub use cache::CacheConfig;
pub use cache::CacheStats;
pub use cache::CacheKey;
pub use cache::CachedTemplate;

Modules§

assembler
Template assembly functionality for composing multiple templates.
builder
Builder patterns for fluent template construction.
cache
Template caching and performance optimization utilities.
error
Error handling for the Tron template engine.
loader
Template loading utilities for the Tron template engine.
macros
Compile-time template generation macros.
template
Core template functionality for the Tron template engine.
template_ref
Template reference functionality with execution capabilities.
validation
Template validation and linting functionality.

Macros§

assemble_templates
Create a template assembler with multiple templates at compile time.
generate_code
Generate code using a template at compile time.
include_template
Include a Tron template file at compile time and create a template instance.
template
Create a template from a string literal at compile time.
template_builder
Create a template builder at compile time with predefined values.
template_ref
Create a template reference with dependencies at compile time.