md-tmpl
Strongly-typed prompt templates for LLMs — markdown files with YAML frontmatter, validated at build time via proc macros, with a full runtime API for dynamic loading.
use include_template;
// Parses and validates the template at build time, generates typed structs + enums.
include_template!;
// Generated types:
// task_report::Params — typed struct
// task_report::ParamsPriority — enum(Critical, High, Medium, Low)
// task_report::ParamsTasksItem — struct { name, urgency }
// task_report::ParamsTasksItemUrgency — enum(Critical, High, Medium, Low)
let params = builder
.title
.priority
.tasks
.build;
let output = params.render.unwrap;
assert!;
assert!;
assert!;
The template behind it — a plain .tmpl.md markdown file:
name: task_report
description: A task report template with types
types:
-
params:
- - -
Priority: {{ kind(priority) }}
-
Rename a variant, add a field, remove a param — the compiler catches it immediately. No runtime surprises.
Why?
- Build-time validation — proc macros parse and validate syntax, types, and variable references at
cargo build. Typos, missing fields, and type mismatches are build errors. Templates can also be loaded and validated at runtime. - Markdown-native — prompts live in
.tmpl.mdfiles, readable in any editor or on GitHub. Compound types use()(never<>), control-flow tags use> {% %}blockquote prefixes. - Agent-safe — when an LLM edits prompts, the compiler catches drift immediately.
validate_template()enables hot-reload with contract enforcement.
Installation
Available on crates.io: https://crates.io/crates/md-tmpl (and https://crates.io/crates/md-tmpl-macros)
# build-time validation + code generation:
MSRV: 1.85 (Rust 2024 edition) · no_std compatible (disable default std feature)
Build-Time Typed Structs
include_template!
Reads a .tmpl.md file at build time, validates it, and generates a typed
module:
use include_template;
// Generates: pub mod simple_greeting { pub struct Params { pub name: String } }
include_template!;
let output = Params .render.unwrap;
assert_eq!;
template!
Inline template strings — same validation, no file needed:
template!;
let output = Params
.render
.unwrap;
assert_eq!;
TypedBuilder Integration
Enable typed-builder for ergonomic builder patterns:
# include_template!;
let params = builder
.name // setter(into): accepts &str or String
.count
.build; // `items` defaults to vec![]
let output = params.render.unwrap;
| Field type | Builder behaviour |
|---|---|
String |
setter(into) — accepts &str, String, or anything Into<String> |
Vec<…> |
default — omit the field to get an empty Vec |
Scalars (i64, f64, bool) |
Required |
Sub-structs also derive TypedBuilder:
# include_template!;
let item = builder
.label
.build;
let params = builder
.name
.count
.items
.build;
serde Integration
Render directly from any Serialize struct:
use Template;
use Serialize;
let tmpl = from_source.unwrap;
let output = tmpl.render.unwrap;
Runtime API
For dynamic or scripting use cases, parse templates at runtime.
ctx! Macro
Ergonomic context construction with nested structs and lists:
use ;
let tmpl = from_source.unwrap;
let output = tmpl.render_ctx.unwrap;
assert_eq!;
Runtime Loading
use load_template;
let tmpl = load_template.unwrap;
let mut ctx = new;
ctx.set;
let output = tmpl.render_ctx.unwrap;
assert!;
Hot-Reload
Load templates from disk at runtime while keeping type safety — iterate on prompt wording without recompiling:
# include_template!;
let tmpl = from_file.unwrap;
validate_template.unwrap;
let output = Params .render_reloaded.unwrap;
Caching
TemplateCache hashes file contents — unchanged files return cached
compilations. render_cached() extends this to included templates:
use TemplateCache;
let dir = tempdir.unwrap;
let path = dir.path.join;
write.unwrap;
let cache = new;
let tmpl = cache.load.unwrap;
let mut ctx = new;
ctx.set;
let output = tmpl.render_ctx_cached.unwrap;
assert_eq!;
Defaults & Extra Params
render_allowing_extra()
Extra context keys not declared in frontmatter are silently ignored:
use ;
let tmpl = from_source.unwrap;
let ctx = ctx! ;
assert_eq!;
defaults_context()
Returns a Context pre-filled with default values:
use Template;
let tmpl = from_source.unwrap;
let mut ctx = tmpl.defaults_context;
ctx.set; // count already has default 5
assert_eq!;
Performance
Internal Benchmarks (Criterion)
| Operation | Small | Medium | Large |
|---|---|---|---|
| render | 196 ns | 1.11 µs | 22.1 µs |
| parse | 3.65 µs | 15.3 µs | 30.8 µs |
vs Competitors
Criterion benchmarks, render only (pre-parsed template + data → output). (source)
| Scenario | md-tmpl | Tera | MiniJinja |
Handlebars |
|---|---|---|---|---|
| simple | 130 ns 🏆 | 213 ns | 558 ns | 632 ns |
| loop | 445 ns 🏆 | 618 ns | 2.00 µs | 2.85 µs |
| conditional | 173 ns 🏆 | 348 ns | 625 ns | 1.16 µs |
| hero | 2.07 µs 🏆 | 2.09 µs | 7.62 µs | 21.4 µs |
| mega | 10.1 µs 🏆 | 11.1 µs | 30.1 µs | 84.7 µs |
Intel Xeon @ 2.60 GHz, 3 runs × 100 Criterion samples. Hero/mega margins are small — treat as comparable to Tera.
Full Reference
See SPEC.md for the complete syntax — control-flow tags, filters, built-in functions, whitespace control, and error diagnostics.
License
Apache-2.0 OR MIT