Crate tomplate_build

Crate tomplate_build 

Source
Expand description

§Build-time utilities for Tomplate

This crate provides build-time template discovery and amalgamation for the Tomplate template engine. It’s designed to be used in build.rs scripts to discover template files in your project and prepare them for compile-time processing.

§Overview

The build process:

  1. Discovers .tomplate.toml files using glob patterns
  2. Parses and validates template definitions
  3. Amalgamates all templates into a single TOML file
  4. Places the amalgamated file in OUT_DIR for the macro to read

§Quick Start

In your build.rs:

fn main() {
    tomplate_build::Builder::new()
        .add_patterns([
            "**/*.tomplate.toml",
            "templates/*.toml"
        ])
        .build()
        .expect("Failed to build templates");
}

§Template File Format

Templates are defined in TOML files with the following structure:

[template_name]
template = "The template string with {placeholders}"
engine = "simple"  # Optional: "simple", "handlebars", "tera", or "minijinja"

[another_template]
template = """
Multi-line templates
are also supported
"""

§Advanced Configuration

use tomplate_build::{Builder, Engine};

fn main() {
    Builder::new()
        // Add patterns one by one
        .add_pattern("templates/*.toml")
        .add_pattern("sql/*.toml")
        // Or add multiple at once
        .add_patterns(vec!["config/*.toml", "queries/*.toml"])
        // Set a default engine for templates without explicit engine
        .default_engine(Engine::Handlebars)
        // Build and generate the amalgamated file
        .build()
        .expect("Failed to build templates");
}

§Error Handling

The builder will fail if:

  • Template files have invalid TOML syntax
  • Duplicate template names are found across files
  • File I/O errors occur

§Integration with Cargo

The builder automatically:

  • Sets up cargo:rerun-if-changed for template files
  • Outputs to OUT_DIR (respecting Cargo’s build system)
  • Provides clear error messages for debugging

Re-exports§

pub use types::Engine;
pub use types::Error;
pub use types::Result;
pub use types::Template;

Modules§

types
Types used throughout the build system.

Structs§

Builder
The main builder for discovering and processing templates.

Enums§

BuildMode
Build mode for template amalgamation.