md-tmpl-core 0.5.3

Core template engine for md-tmpl — parsing, compilation, and rendering
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
use std::sync::Arc;

use crate::{Template, Value};

#[test]
fn test_higher_order_template() {
    let helper = Template::from_source(
        r"---
params: [name = str]
---
Hello {{ name }}!",
    )
    .unwrap();

    let main = Template::from_source(
        r#"---
params: [test = tmpl(name = str)]
---
> {% include test with name="World" %}"#,
    )
    .unwrap();

    let mut ctx = crate::Context::new();
    ctx.set("test", Value::Tmpl(Arc::new(helper)));

    let result = main.render_ctx(&ctx).unwrap();
    assert_eq!(result, "Hello World!");
}

#[test]
fn test_higher_order_template_type_mismatch() {
    let helper = Template::from_source(
        r"---
params: [age = int]
---
Age: {{ age }}",
    )
    .unwrap();

    let main = Template::from_source(
        r#"---
params: [test = tmpl(name = str)]
---
> {% include test with name="World" %}"#,
    )
    .unwrap();

    let mut ctx = crate::Context::new();
    ctx.set("test", Value::Tmpl(Arc::new(helper)));

    let err = main.render_ctx(&ctx).unwrap_err();
    eprintln!("ACTUAL ERROR: {err}");
    assert!(
        err.to_string().contains("type mismatch")
            && err.to_string().contains("test")
            && err.to_string().contains("name")
            && err.to_string().contains("expected str"),
        "expected type mismatch error for 'test' at '.name', got: {err}"
    );
}

#[test]
fn test_higher_order_template_with_defaults() {
    let helper = Template::from_source(
        r#"---
params:
  - name = str
  - greeting = str := "Hi"
---
{{ greeting }} {{ name }}!"#,
    )
    .unwrap();

    let main = Template::from_source(
        r#"---
params: [test = tmpl(name = str)]
---
> {% include test with name="World" %}"#,
    )
    .unwrap();

    let mut ctx = crate::Context::new();
    ctx.set("test", Value::Tmpl(Arc::new(helper)));

    let result = main.render_ctx(&ctx).unwrap();
    assert_eq!(result, "Hi World!");
}

#[test]
fn test_higher_order_template_nested() {
    let inner = Template::from_source(
        r"---
params: [val = str]
---
Inner: {{ val }}",
    )
    .unwrap();

    let middle = Template::from_source(
        r"---
params:
  - target = tmpl(val = str)
  - value = str
---
> {% include target with val=value %}",
    )
    .unwrap();

    let main = Template::from_source(
        r#"---
params:
  - processor = tmpl(target = tmpl(val = str), value = str)
  - callback = tmpl(val = str)
---
> {% include processor with target=callback, value="Success" %}"#,
    )
    .unwrap();

    let mut ctx = crate::Context::new();
    ctx.set("processor", Value::Tmpl(Arc::new(middle)));
    ctx.set("callback", Value::Tmpl(Arc::new(inner)));

    let result = main.render_ctx(&ctx).unwrap();
    assert_eq!(result, "Inner: Success");
}

/// `tmpl()` with empty parens accepts a template with no required params.
/// This is the pattern ARTIST uses for the `preamble` parameter.
#[test]
fn test_higher_order_empty_tmpl() {
    let helper = Template::from_source(
        r"---
params: []
---
Preamble content here.",
    )
    .unwrap();

    let main = Template::from_source(
        r"---
params: [preamble = tmpl()]
---

> {% include preamble %}

Done.",
    )
    .unwrap();

    let mut ctx = crate::Context::new();
    ctx.set("preamble", Value::Tmpl(Arc::new(helper)));

    let result = main.render_ctx(&ctx).unwrap();
    assert!(result.contains("Preamble content here."), "got: {result}");
    assert!(result.contains("Done."), "got: {result}");
}

/// A template with extra defaulted params matches a `tmpl()` signature that
/// doesn't list them — the defaults are used automatically.
#[test]
fn test_higher_order_extra_defaulted_params_match() {
    let helper = Template::from_source(
        r#"---
params:
  - x = str
  - extra = str := "fallback"
---
{{ x }} {{ extra }}"#,
    )
    .unwrap();

    let main = Template::from_source(
        r#"---
params: [widget = tmpl(x = str)]
---
> {% include widget with x="hello" %}"#,
    )
    .unwrap();

    let mut ctx = crate::Context::new();
    ctx.set("widget", Value::Tmpl(Arc::new(helper)));

    let result = main.render_ctx(&ctx).unwrap();
    assert_eq!(result, "hello fallback");
}

/// A template with extra REQUIRED params (no default) does NOT match a
/// `tmpl()` signature that doesn't list them — this is a type mismatch.
#[test]
fn test_higher_order_extra_required_params_reject() {
    let helper = Template::from_source(
        r"---
params:
  - x = str
  - extra = str
---
{{ x }} {{ extra }}",
    )
    .unwrap();

    let main = Template::from_source(
        r#"---
params: [widget = tmpl(x = str)]
---
> {% include widget with x="hello" %}"#,
    )
    .unwrap();

    let mut ctx = crate::Context::new();
    ctx.set("widget", Value::Tmpl(Arc::new(helper)));

    let err = main.render_ctx(&ctx).unwrap_err();
    assert!(
        err.to_string().contains("type mismatch") || err.to_string().contains("extra"),
        "expected type mismatch for extra required param, got: {err}"
    );
}

/// Full external flow: outer template declares `tmpl(x = str, y = int)`,
/// passes multiple params via `with`, and the inner template uses them.
#[test]
fn test_higher_order_multi_param_forwarding() {
    let helper = Template::from_source(
        r"---
params:
  - x = str
  - y = int
---
{{ x }}-{{ y }}",
    )
    .unwrap();

    let main = Template::from_source(
        r#"---
params:
  - widget = tmpl(x = str, y = int)
  - label = str
  - num = int
---
Label: {{ label }}

> {% include widget with x="hello", y=num %}"#,
    )
    .unwrap();

    let mut ctx = crate::Context::new();
    ctx.set("widget", Value::Tmpl(Arc::new(helper)));
    ctx.set("label", "test");
    ctx.set("num", 42);

    let result = main.render_ctx(&ctx).unwrap();
    assert!(result.contains("Label: test"), "got: {result}");
    assert!(result.contains("hello-42"), "got: {result}");
}

/// Passing a non-template value for a `tmpl()` param must be rejected.
#[test]
fn test_higher_order_non_template_value_rejected() {
    let main = Template::from_source(
        r#"---
params: [widget = tmpl(name = str)]
---
> {% include widget with name="test" %}"#,
    )
    .unwrap();

    let mut ctx = crate::Context::new();
    // Pass a plain string instead of a Template — should error.
    ctx.set("widget", "not a template");

    let err = main.render_ctx(&ctx).unwrap_err();
    assert!(
        err.to_string().contains("type mismatch")
            || err.to_string().contains("widget")
            || err.to_string().contains("tmpl"),
        "expected type mismatch for non-template value, got: {err}"
    );
}

/// Using `{{ widget }}` with a `tmpl()` param errors at render time
/// (templates are not directly renderable — use `{% include %}` instead).
///
/// NOTE: The SPEC says this should be a compile-time error, and it IS
/// rejected at compile-time through the `compile()` / proc-macro path
/// (validated by `is_displayable()` in `type_check.rs`). The `from_source()`
/// interpretation path only catches this at render time.
#[test]
fn test_higher_order_display_tmpl_rejected_at_render() {
    let main = Template::from_source(
        r"---
params: [widget = tmpl()]
---
{{ widget }}",
    )
    .unwrap();

    let helper = Template::from_source(
        r"---
params: []
---
content",
    )
    .unwrap();

    let mut ctx = crate::Context::new();
    ctx.set("widget", Value::Tmpl(Arc::new(helper)));

    let err = main.render_ctx(&ctx).unwrap_err();
    assert!(
        err.to_string().contains("display")
            || err.to_string().contains("cannot")
            || err.to_string().contains("tmpl"),
        "expected display error for tmpl() param, got: {err}"
    );
}

/// `tmpl()` param is truthy when set (should work in {% if %} guards).
#[test]
fn test_higher_order_tmpl_is_truthy() {
    let helper = Template::from_source(
        r"---
params: []
---
present",
    )
    .unwrap();

    let main = Template::from_source(
        r"---
params: [widget = tmpl()]
---
> {% if widget %}

yes

> {% /if %}",
    )
    .unwrap();

    let mut ctx = crate::Context::new();
    ctx.set("widget", Value::Tmpl(Arc::new(helper)));

    let result = main.render_ctx(&ctx).unwrap();
    assert!(
        result.contains("yes"),
        "tmpl should be truthy, got: {result}"
    );
}

#[test]
fn test_higher_order_option_tmpl() {
    let helper = Template::from_source(
        r"---
params: [test = str]
---
Helper: {{ test }}",
    )
    .unwrap();

    let main = Template::from_source(
        r#"---
params: [cb = option(tmpl(test = str))]
---
> {% if has(cb) %}
> {% include cb with test="Hello Option" %}
> {% else %}

No callback

> {% /if %}"#,
    )
    .unwrap();

    let mut ctx_some = crate::Context::new();
    ctx_some.set("cb", Value::Tmpl(Arc::new(helper)));
    let result_some = main.render_ctx(&ctx_some).unwrap();
    assert!(
        result_some.contains("Helper: Hello Option"),
        "got: {result_some}"
    );

    let mut ctx_none = crate::Context::new();
    ctx_none.set("cb", Value::None);
    let result_none = main.render_ctx(&ctx_none).unwrap();
    assert!(result_none.contains("No callback"), "got: {result_none}");
}

#[test]
fn test_higher_order_nested_option_tmpl() {
    let inner = Template::from_source(
        r"---
params: [test = str]
---
Inner: {{ test }}",
    )
    .unwrap();

    let middle = Template::from_source(
        r#"---
params: [sub = option(tmpl(test = str))]
---
> {% if has(sub) %}
> {% include sub with test="Nested Success" %}
> {% else %}

No sub

> {% /if %}"#,
    )
    .unwrap();

    let main = Template::from_source(
        r"---
params:
  - cb = option(tmpl(sub = option(tmpl(test = str))))
  - target = option(tmpl(test = str))
---
> {% if has(cb) %}
> {% include cb with sub=target %}
> {% else %}

No cb

> {% /if %}",
    )
    .unwrap();

    let mut ctx = crate::Context::new();
    ctx.set("cb", Value::Tmpl(Arc::new(middle)));
    ctx.set("target", Value::Tmpl(Arc::new(inner)));

    let result = main.render_ctx(&ctx).unwrap();
    assert!(result.contains("Inner: Nested Success"), "got: {result}");
}