tron 2.1.0

A rust based template system built for speed and simplicity.
Documentation
//! Basic usage example demonstrating core Tron functionality.
//!
//! This example shows how to create, configure, and render templates
//! using the basic Tron API.

use tron::{TronTemplate, TronRef, TronError, Result};

fn main() -> Result<()> {
    println!("=== Tron Basic Usage Example ===\n");

    // Example 1: Simple template creation and rendering
    example_simple_template()?;
    
    // Example 2: Template with multiple placeholders
    example_multiple_placeholders()?;
    
    // Example 3: Using TronRef for dependency management
    example_template_ref()?;
    
    // Example 4: Error handling
    example_error_handling();

    Ok(())
}

fn example_simple_template() -> Result<()> {
    println!("1. Simple Template:");
    
    let mut template = TronTemplate::new("Hello @[name]@!")?;
    template.set("name", "World")?;
    
    let result = template.render()?;
    println!("   Input: \"Hello @[name]@!\"");
    println!("   Output: \"{}\"", result);
    println!();
    
    Ok(())
}

fn example_multiple_placeholders() -> Result<()> {
    println!("2. Multiple Placeholders:");
    
    let mut template = TronTemplate::new(
        "fn @[function_name]@(@[params]@) -> @[return_type]@ {\n    @[body]@\n}"
    )?;
    
    template.set("function_name", "calculate_sum")?;
    template.set("params", "a: i32, b: i32")?;
    template.set("return_type", "i32")?;
    template.set("body", "a + b")?;
    
    let result = template.render()?;
    println!("   Generated Rust function:");
    println!("{}", result);
    println!();
    
    Ok(())
}

fn example_template_ref() -> Result<()> {
    println!("3. Template Reference with Dependencies:");
    
    let template = TronTemplate::new(
        "use serde::{{Serialize, Deserialize}};\n\n#[derive(Serialize, Deserialize)]\npub struct @[struct_name]@ {\n    @[fields]@\n}"
    )?;
    
    let template_ref = TronRef::new(template)
        .with_dependency("serde = { version = \"1.0\", features = [\"derive\"] }");
    
    let mut template_ref = template_ref;
    template_ref.set("struct_name", "User")?;
    template_ref.set("fields", "pub name: String,\n    pub email: String,\n    pub age: u32,")?;
    
    let result = template_ref.render()?;
    println!("   Generated Rust struct with dependencies:");
    println!("{}", result);
    println!("   Dependencies: {:?}", template_ref.dependencies());
    println!();
    
    Ok(())
}

fn example_error_handling() {
    println!("4. Error Handling:");
    
    // Example of missing placeholder error
    let mut template = TronTemplate::new("Hello @[name]@!").unwrap();
    match template.render() {
        Ok(_) => println!("   Unexpected success"),
        Err(TronError::MissingPlaceholder(placeholder)) => {
            println!("   ✓ Caught missing placeholder error for: '{}'", placeholder);
        }
        Err(e) => println!("   Unexpected error: {}", e),
    }
    
    // Example of invalid placeholder name error
    match template.set("nonexistent", "value") {
        Ok(_) => println!("   Unexpected success"),
        Err(TronError::MissingPlaceholder(placeholder)) => {
            println!("   ✓ Caught invalid placeholder error for: '{}'", placeholder);
        }
        Err(e) => println!("   Unexpected error: {}", e),
    }
    
    // Example of invalid template syntax
    match TronTemplate::new("@[invalid name with spaces]@") {
        Ok(_) => println!("   Unexpected success"),
        Err(TronError::InvalidSyntax(msg)) => {
            println!("   ✓ Caught invalid syntax error: {}", msg);
        }
        Err(e) => println!("   Unexpected error: {}", e),
    }
    
    println!();
}