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:
- Discovers
.tomplate.toml
files using glob patterns - Parses and validates template definitions
- Amalgamates all templates into a single TOML file
- 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§
Modules§
- types
- Types used throughout the build system.
Structs§
- Builder
- The main builder for discovering and processing templates.
Enums§
- Build
Mode - Build mode for template amalgamation.