Skip to main content

Crate temple_dsl

Crate temple_dsl 

Source
Expand description

Temple is a small language for shaping data: an input value goes in, a value of the shape you asked for comes out.

let total = input.items.map(i -> i.price * i.qty).sum()

{
  "customer": {{ input.customer.name }},
  "total": {{ total }},
  "vip": {{ total > 1000 }}
}

§Rendering a template

Template::compile parses and checks the source once; render it as many times as you like, into a Value or straight into your own type.

use serde::Deserialize;
use temple_dsl::{Template, Value};

#[derive(Deserialize)]
struct Greeting {
    text: String,
}

let template = Template::compile(r#"{ "text": {{ concat("hi ", input.name) }} }"#).unwrap();
let greeting: Greeting = template.render(Value::obj([("name", "Ada")])).unwrap();
assert_eq!(greeting.text, "hi Ada");

§Embedding temple in another language

A host parses one expression or value at a time inside its own syntax (ExprUnit::parse_at, TemplateUnit::parse_at), checks it against the Names it will bind, and evaluates it with an Env under a Budget.

use temple_dsl::{Budget, Env, ExprUnit, Names, Value};

let names = Names::new().var("limit");
let unit = ExprUnit::compile("input.age >= limit", &names).unwrap();
let env = Env::new().set("limit", 18);
let out = unit
    .eval(&Value::obj([("age", Value::Int(21))]), &env, &Budget::new(1_000))
    .unwrap();
assert_eq!(out, Value::Bool(true));

Structs§

Budget
A work limit shared by every unit evaluated with it: one unit per expression and per element a method or built-in touches; copies cost one per element and one per 64 bytes of text.
Env
The values, and optionally the functions, a unit is evaluated with.
ExprUnit
A checked expression, ready to evaluate with an Env.
Functions
Host functions written in temple, callable from units evaluated with an Env that carries them.
InputRead
A read of an input path: input.data.order is ["data", "order"], stopping at the first index or method call.
Names
The variables and functions a unit may use besides input and this.
ParsedExpr
An expression parsed out of a larger source, not yet checked.
ParsedTemplate
An output value parsed out of a larger source, not yet checked.
Span
A byte range start..end in a source string.
Template
A compiled template.
TemplateUnit
A checked output value, ready to render with an Env.

Enums§

CompileError
Why a template could not be compiled.
EvalError
Why evaluating a unit failed.
LoadError
Why a stored template could not be loaded.
RenderError
Why a template could not be rendered.
Value
A dynamic value: a render’s input and, unless deserialized into your own type, its output. Numbers are integers or exact decimals, never floats; objects keep insertion order.

Constants§

BLOB_VERSION
Version of the blob format Template::to_bytes writes.
VERSION
This crate’s version, for hosts that record it next to stored blobs.

Functions§

is_builtin_function
Whether name is a built-in function, and so can’t name a host function.
is_reserved_name
Whether name is reserved (input, this, let, …) and so can’t name a variable or function.