temple-dsl 1.0.0

A small, fast Rust DSL for shaping data — input in, strongly-typed Rust value out.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
//! The evaluator recurses once per tree level, and a debug build gives every
//! local its own stack slot for the whole call, so functions on that path
//! stay small and split by kind; `#[inline(never)]` keeps a big one out.
//! `#[inline]` sits only where the benchmarks show it matters.

pub(crate) mod budget;
pub(crate) mod builtins;
mod methods;
mod ops;
mod path;
pub(crate) mod scope;

use crate::common::error::{RenderError, Span};
use crate::common::value::Value;
use crate::syntax::ast::{
    Expr, ExprKind, InterpPart, LitKey, Module, ObjEntry, ObjField, OutKind, OutNode, WhenBranch,
};
use budget::{Meter, Unmetered};
use indexmap::IndexMap;
use rustc_hash::FxHashMap;
use scope::{Scope, Vars};
use smol_str::SmolStr;
use std::borrow::Cow;

#[derive(Debug, Clone)]
pub(crate) struct FunctionDef {
    pub params: Vec<SmolStr>,
    pub body: Expr,
}

pub(crate) type FunctionTable = FxHashMap<SmolStr, FunctionDef>;

pub(crate) fn run_template(
    module: &Module,
    order: &[usize],
    input: &Value,
) -> Result<Value, RenderError> {
    let empty = Vars::default();
    let ctx = Context::new(input, &empty, &empty, None, &Unmetered);
    let mut lets = Vars::with_capacity_and_hasher(module.lets.len(), Default::default());
    for binding in &module.lets {
        // each `let` sees the ones above it
        let value = evaluate_expr(&binding.expr, &ctx, &Scope::base(&lets))?;
        lets.insert(binding.name.clone(), value);
    }
    render_output(&module.output, order, &ctx, &Scope::base(&lets))
}

pub(crate) fn run_expr<M: Meter>(
    expr: &Expr,
    input: &Value,
    vars: &Vars,
    functions: Option<&FunctionTable>,
    meter: &M,
) -> Result<Value, M::Err> {
    let no_this = Vars::default();
    let ctx = Context::new(input, &no_this, vars, functions, meter);
    evaluate_expr(expr, &ctx, &Scope::base(vars))
}

pub(crate) fn run_output<M: Meter>(
    node: &OutNode,
    order: &[usize],
    input: &Value,
    vars: &Vars,
    functions: Option<&FunctionTable>,
    meter: &M,
) -> Result<Value, M::Err> {
    let no_this = Vars::default();
    let ctx = Context::new(input, &no_this, vars, functions, meter);
    render_output(node, order, &ctx, &Scope::base(vars))
}

struct Context<'a, M: Meter> {
    input: &'a Value,
    /// The sibling fields already rendered, for `this.key`.
    this: &'a Vars,
    functions: Option<&'a FunctionTable>,
    globals: &'a Vars,
    meter: &'a M,
    depth: u32,
}

impl<'a, M: Meter> Context<'a, M> {
    fn new(
        input: &'a Value,
        this: &'a Vars,
        globals: &'a Vars,
        functions: Option<&'a FunctionTable>,
        meter: &'a M,
    ) -> Self {
        Context {
            input,
            this,
            functions,
            globals,
            meter,
            depth: 0,
        }
    }

    fn with_this<'b>(&'b self, this: &'b Vars) -> Context<'b, M> {
        Context { this, ..*self }
    }

    #[inline(always)]
    fn deeper(&self) -> Context<'a, M> {
        Context {
            depth: self.depth + 1,
            ..*self
        }
    }
}

/// Object fields are filled in `order` so each `this.key` sees its
/// sibling; keys still come out in source order.
fn render_output<M: Meter>(
    output: &OutNode,
    order: &[usize],
    ctx: &Context<M>,
    scope: &Scope,
) -> Result<Value, M::Err> {
    let OutKind::Object(fields) = &output.kind else {
        return evaluate(output, ctx, scope);
    };
    let mut this = Vars::with_capacity_and_hasher(fields.len(), Default::default());
    for &position in order {
        let field = &fields[position];
        let value = evaluate(&field.value, &ctx.with_this(&this), scope)?;
        this.insert(field.key.clone(), value);
    }
    let mut result = IndexMap::with_capacity(fields.len());
    for field in fields {
        if let Some(value) = this.remove(&field.key) {
            if field.optional && matches!(value, Value::Null) {
                continue;
            }
            result.insert(field.key.clone(), value);
        }
    }
    Ok(Value::Obj(result))
}

fn evaluate<M: Meter>(node: &OutNode, ctx: &Context<M>, scope: &Scope) -> Result<Value, M::Err> {
    match &node.kind {
        OutKind::Literal(literal) => Ok(literal.to_value()),
        OutKind::Hole(expr) => evaluate_expr(expr, ctx, scope),
        OutKind::Object(fields) => output_object(fields, ctx, scope),
        OutKind::Array(items) => output_array(items, ctx, scope),
        OutKind::Interp(parts) => output_interp(parts, ctx, scope),
    }
}

fn output_object<M: Meter>(
    fields: &[ObjField],
    ctx: &Context<M>,
    scope: &Scope,
) -> Result<Value, M::Err> {
    let mut object = IndexMap::with_capacity(fields.len());
    for field in fields {
        let value = evaluate(&field.value, ctx, scope)?;
        if field.optional && matches!(value, Value::Null) {
            continue;
        }
        object.insert(field.key.clone(), value);
    }
    Ok(Value::Obj(object))
}

fn output_array<M: Meter>(
    items: &[OutNode],
    ctx: &Context<M>,
    scope: &Scope,
) -> Result<Value, M::Err> {
    let mut array = Vec::with_capacity(items.len());
    for item in items {
        array.push(evaluate(item, ctx, scope)?);
    }
    Ok(Value::Arr(array))
}

fn output_interp<M: Meter>(
    parts: &[InterpPart],
    ctx: &Context<M>,
    scope: &Scope,
) -> Result<Value, M::Err> {
    let mut text = String::new();
    for part in parts {
        match part {
            InterpPart::Text(literal) => text.push_str(literal),
            InterpPart::Hole(expr) => {
                let value = evaluate_expr(expr, ctx, scope)?;
                text.push_str(&builtins::scalar_to_string(&value, expr.span)?);
            }
        }
    }
    Ok(Value::Str(text.into()))
}

fn evaluate_expr<M: Meter>(expr: &Expr, ctx: &Context<M>, scope: &Scope) -> Result<Value, M::Err> {
    ctx.meter.charge(1, expr.span)?;
    if !M::LIMITED {
        return evaluate_kind(expr, ctx, scope);
    }
    ctx.meter.enter(ctx.depth, expr.span)?;
    evaluate_kind(expr, &ctx.deeper(), scope)
}

#[inline(always)]
fn evaluate_kind<M: Meter>(expr: &Expr, ctx: &Context<M>, scope: &Scope) -> Result<Value, M::Err> {
    match &expr.kind {
        ExprKind::Literal(literal) => Ok(literal.to_value()),
        ExprKind::Path {
            root,
            root_span,
            segments,
        } => path::evaluate_path(root, *root_span, segments, ctx, scope),
        ExprKind::Binary { op, lhs, rhs } => {
            ops::evaluate_binary(*op, lhs, rhs, expr.span, ctx, scope)
        }
        ExprKind::Unary { op, operand } => ops::evaluate_unary(*op, operand, expr.span, ctx, scope),
        ExprKind::Ternary {
            cond,
            then_branch,
            else_branch,
        } => evaluate_ternary(cond, then_branch, else_branch, ctx, scope),
        ExprKind::When { branches, fallback } => {
            evaluate_when(branches, fallback.as_deref(), expr.span, ctx, scope)
        }
        ExprKind::Lambda { .. } => Err(RenderError::type_mismatch(
            "value",
            "lambda (only valid as a method argument)",
            expr.span,
        )
        .into()),
        ExprKind::Let {
            name, value, body, ..
        } => evaluate_let(name, value, body, ctx, scope),
        ExprKind::ArrayLit(items) => evaluate_array(items, ctx, scope),
        ExprKind::ObjectLit(entries) => evaluate_object(entries, ctx, scope),
        ExprKind::FuncCall {
            name,
            name_span,
            args,
        } => evaluate_call(name, *name_span, args, ctx, scope),
        ExprKind::Access { base, segments } => {
            let base_value = evaluate_expr(base, ctx, scope)?;
            path::evaluate_access(base_value, base.span, segments, ctx, scope)
        }
    }
}

fn evaluate_ternary<M: Meter>(
    cond: &Expr,
    then_branch: &Expr,
    else_branch: &Expr,
    ctx: &Context<M>,
    scope: &Scope,
) -> Result<Value, M::Err> {
    let condition = evaluate_expr(cond, ctx, scope)?;
    if ops::require_bool(&condition, cond.span)? {
        evaluate_expr(then_branch, ctx, scope)
    } else {
        evaluate_expr(else_branch, ctx, scope)
    }
}

fn evaluate_when<M: Meter>(
    branches: &[WhenBranch],
    fallback: Option<&Expr>,
    span: Span,
    ctx: &Context<M>,
    scope: &Scope,
) -> Result<Value, M::Err> {
    for branch in branches {
        let condition = evaluate_expr(&branch.cond, ctx, scope)?;
        if ops::require_bool(&condition, branch.cond.span)? {
            return evaluate_expr(&branch.result, ctx, scope);
        }
    }
    match fallback {
        Some(fallback) => evaluate_expr(fallback, ctx, scope),
        None => Err(RenderError::WhenNoMatch { span }.into()),
    }
}

fn evaluate_let<M: Meter>(
    name: &SmolStr,
    value: &Expr,
    body: &Expr,
    ctx: &Context<M>,
    scope: &Scope,
) -> Result<Value, M::Err> {
    let bound = evaluate_expr(value, ctx, scope)?;
    evaluate_expr(body, ctx, &scope.layered(name, &bound))
}

fn evaluate_array<M: Meter>(
    items: &[Expr],
    ctx: &Context<M>,
    scope: &Scope,
) -> Result<Value, M::Err> {
    let mut array = Vec::with_capacity(items.len());
    for item in items {
        array.push(evaluate_expr(item, ctx, scope)?);
    }
    Ok(Value::Arr(array))
}

fn evaluate_object<M: Meter>(
    entries: &[ObjEntry],
    ctx: &Context<M>,
    scope: &Scope,
) -> Result<Value, M::Err> {
    let mut object = IndexMap::with_capacity(entries.len());
    for entry in entries {
        let (key, key_span) = match &entry.key {
            LitKey::Static(key) => (key.clone(), entry.value.span),
            LitKey::Computed(key_expr) => match &evaluate_expr(key_expr, ctx, scope)? {
                Value::Str(key) => (key.clone(), key_expr.span),
                other => {
                    return Err(RenderError::TypeMismatch {
                        expected: "string object key",
                        got: other.kind().to_string(),
                        span: key_expr.span,
                    }
                    .into());
                }
            },
        };
        let value = evaluate_expr(&entry.value, ctx, scope)?;
        if entry.optional && matches!(value, Value::Null) {
            continue;
        }
        // two written keys can't collide (the checker refuses that), so a
        // collision here involves a computed key and is an error, not an
        // overwrite
        if object.insert(key.clone(), value).is_some() {
            return Err(RenderError::DuplicateKey {
                key: key.to_string(),
                span: key_span,
            }
            .into());
        }
    }
    Ok(Value::Obj(object))
}

#[inline(never)]
fn evaluate_call<M: Meter>(
    name: &SmolStr,
    name_span: Span,
    args: &[Expr],
    ctx: &Context<M>,
    scope: &Scope,
) -> Result<Value, M::Err> {
    // a host function only reads its arguments, so paths are passed borrowed
    if let Some(function) = ctx.functions.and_then(|table| table.get(name)) {
        let mut values = Vec::with_capacity(args.len());
        for arg in args {
            values.push(ops::operand(arg, ctx, scope)?);
        }
        return call_host_function(function, name, name_span, &values, ctx);
    }
    let mut values = Vec::with_capacity(args.len());
    for arg in args {
        values.push(evaluate_expr(arg, ctx, scope)?);
    }
    ctx.meter.charge(args_weight(&values), name_span)?;
    Ok(builtins::call_builtin(name, &values, name_span)?)
}

/// A host function body sees globals and its parameters, never the caller's
/// locals.
fn call_host_function<M: Meter>(
    function: &FunctionDef,
    name: &SmolStr,
    span: Span,
    args: &[Cow<Value>],
    ctx: &Context<M>,
) -> Result<Value, M::Err> {
    if args.len() != function.params.len() {
        return Err(RenderError::ArityMismatch {
            method: name.to_string(),
            expected: function.params.len(),
            got: args.len(),
            span,
        }
        .into());
    }
    let scope = Scope::with_params(ctx.globals, &function.params, args);
    evaluate_expr(&function.body, ctx, &scope)
}

fn args_weight(args: &[Value]) -> u64 {
    args.iter()
        .map(|arg| match arg {
            Value::Arr(items) => items.len() as u64,
            Value::Obj(object) => object.len() as u64,
            Value::Str(text) => (text.len() / 64) as u64,
            _ => 0,
        })
        .sum()
}