md-tmpl-core 0.6.2

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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
use super::*;

fn make_context() -> Context {
    let mut ctx = Context::new();
    ctx.set("name", "Alice");
    ctx.set("count", 3_i64);
    ctx
}

// -- simple resolution --

#[test]
fn resolve_from_context() {
    let ctx = make_context();
    let scope = Scope::new(&ctx);
    assert_eq!(scope.resolve("name"), Some(&Value::Str("Alice".into())));
    assert_eq!(scope.resolve("count"), Some(&Value::Int(3)));
}

#[test]
fn resolve_missing_returns_none() {
    let ctx = Context::new();
    let scope = Scope::new(&ctx);
    assert_eq!(scope.resolve("nope"), None);
}

// -- layered resolution --

#[test]
fn resolve_from_pushed_layer() {
    let ctx = make_context();
    let mut scope = Scope::new(&ctx);
    let layer = scope.push_layer();
    layer.insert("index".into(), Value::Int(1));
    layer.insert("item".into(), Value::Str("task-a".into()));

    assert_eq!(scope.resolve("index"), Some(&Value::Int(1)));
    assert_eq!(scope.resolve("item"), Some(&Value::Str("task-a".into())));
    // Context values still accessible.
    assert_eq!(scope.resolve("name"), Some(&Value::Str("Alice".into())));
}

#[test]
fn pop_layer_restores_previous() {
    let ctx = make_context();
    let mut scope = Scope::new(&ctx);
    let layer = scope.push_layer();
    layer.insert("name".into(), Value::Str("shadowed".into()));
    assert_eq!(scope.resolve("name"), Some(&Value::Str("shadowed".into())));

    scope.pop_layer();
    assert_eq!(scope.resolve("name"), Some(&Value::Str("Alice".into())));
}

// -- shadowing --

#[test]
fn inner_layer_shadows_outer() {
    let ctx = make_context();
    let mut scope = Scope::new(&ctx);

    let layer1 = scope.push_layer();
    layer1.insert("x".into(), Value::Int(10));

    let layer2 = scope.push_layer();
    layer2.insert("x".into(), Value::Int(20));

    assert_eq!(scope.resolve("x"), Some(&Value::Int(20)));

    scope.pop_layer();
    assert_eq!(scope.resolve("x"), Some(&Value::Int(10)));

    scope.pop_layer();
    assert_eq!(scope.resolve("x"), None);
}

// -- dotted path resolution --

#[test]
fn resolve_path_simple() {
    let ctx = make_context();
    let scope = Scope::new(&ctx);
    let val = scope.resolve_path_str("name").unwrap();
    assert_eq!(val, &Value::Str("Alice".into()));
}

#[test]
fn resolve_path_dotted() {
    let mut ctx = Context::new();
    let inner = Value::Struct(Arc::new(
        [("label".into(), Value::Str("important".into()))]
            .into_iter()
            .collect(),
    ));
    ctx.set("task", inner);

    let scope = Scope::new(&ctx);
    let val = scope.resolve_path_str("task.label").unwrap();
    assert_eq!(val, &Value::Str("important".into()));
}

#[test]
fn resolve_path_deeply_nested() {
    let mut ctx = Context::new();
    let deep = Value::Struct(Arc::new(
        [(
            "a".into(),
            Value::Struct(Arc::new(
                [(
                    "b".into(),
                    Value::Struct(Arc::new(
                        [("c".into(), Value::Int(42))].into_iter().collect(),
                    )),
                )]
                .into_iter()
                .collect(),
            )),
        )]
        .into_iter()
        .collect(),
    ));
    ctx.set("root", deep);

    let scope = Scope::new(&ctx);
    assert_eq!(
        scope.resolve_path_str("root.a.b.c").unwrap(),
        &Value::Int(42)
    );
}

#[test]
fn resolve_path_missing_root() {
    let ctx = Context::new();
    let scope = Scope::new(&ctx);
    let err = scope.resolve_path_str("absent").unwrap_err();
    assert!(matches!(err, TemplateError::UndefinedVariable(_)));
}

#[test]
fn resolve_path_missing_field() {
    let mut ctx = Context::new();
    ctx.set(
        "item",
        Value::Struct(Arc::new(
            [("name".into(), Value::Str("x".into()))]
                .into_iter()
                .collect(),
        )),
    );
    let scope = Scope::new(&ctx);
    let err = scope.resolve_path_str("item.missing").unwrap_err();
    assert!(matches!(err, TemplateError::UndefinedVariable(_)));
}

#[test]
fn resolve_path_field_on_non_dict() {
    let mut ctx = Context::new();
    ctx.set("val", 10_i64);
    let scope = Scope::new(&ctx);
    let err = scope.resolve_path_str("val.field").unwrap_err();
    assert!(matches!(err, TemplateError::UndefinedVariable(_)));
}

// -- dotted path in layers --

#[test]
fn resolve_path_through_layer() {
    let ctx = Context::new();
    let mut scope = Scope::new(&ctx);
    let layer = scope.push_layer();
    layer.insert(
        "item".into(),
        Value::Struct(Arc::new(
            [("name".into(), Value::Str("from-layer".into()))]
                .into_iter()
                .collect(),
        )),
    );

    let val = scope.resolve_path_str("item.name").unwrap();
    assert_eq!(val, &Value::Str("from-layer".into()));
}

#[test]
fn test_layer_allocation_reuse() {
    let ctx = Context::new();
    let mut scope = Scope::new(&ctx);

    // Initially empty
    assert_eq!(scope.layers.len(), 0);
    assert_eq!(scope.active_len, 0);

    // Push 1
    {
        let layer = scope.push_layer();
        layer.insert("k1".into(), Value::Int(100));
    }
    assert_eq!(scope.layers.len(), 1);
    assert_eq!(scope.active_len, 1);
    assert_eq!(scope.resolve("k1"), Some(&Value::Int(100)));

    // Pop 1
    scope.pop_layer();
    assert_eq!(scope.layers.len(), 1); // Allocation kept
    assert_eq!(scope.active_len, 0);
    assert_eq!(scope.resolve("k1"), None); // k1 should not resolve because active_len is 0

    // Push again - should reuse
    {
        let layer = scope.push_layer();
        // Verify it was cleared! It shouldn't contain "k1" anymore.
        assert!(layer.is_empty());
        layer.insert("k2".into(), Value::Int(200));
    }
    assert_eq!(scope.layers.len(), 1); // Still 1! Reused!
    assert_eq!(scope.active_len, 1);
    assert_eq!(scope.resolve("k1"), None);
    assert_eq!(scope.resolve("k2"), Some(&Value::Int(200)));
}

// -- kind() function tests --

#[test]
fn kind_extracts_enum_variant_name() {
    let tmpl = crate::Template::from_source(
        r"---
params: [outcome = struct(evidence = str)]
---
{{ kind(outcome) }}",
    )
    .unwrap();
    let mut ctx = crate::Context::new();
    ctx.set(
        "outcome",
        Value::Struct(Arc::new(HashMap::from([
            (
                crate::consts::ENUM_TAG_KEY.into(),
                Value::Str("Confirmed".into()),
            ),
            ("evidence".into(), Value::Str("confirmed finding".into())),
        ]))),
    );
    assert_eq!(tmpl.render_ctx(&ctx).unwrap(), "Confirmed");
}

#[test]
fn kind_rejects_non_dict() {
    let tmpl = crate::Template::from_source(
        r"---
params: [count = int]
---
{{ kind(count) }}",
    )
    .unwrap();
    let mut ctx = crate::Context::new();
    ctx.set("count", 42);
    let err = tmpl.render_ctx(&ctx).unwrap_err();
    assert!(
        err.to_string().contains("enum"),
        "should mention enum requirement: {err}"
    );
}

#[test]
fn kind_rejects_dict_without_variant_tag() {
    let tmpl = crate::Template::from_source(
        r"---
params: [data = struct(name = str)]
---
{{ kind(data) }}",
    )
    .unwrap();
    let mut ctx = crate::Context::new();
    ctx.set(
        "data",
        Value::Struct(Arc::new(HashMap::from([(
            "name".into(),
            Value::Str("x".into()),
        )]))),
    );
    let err = tmpl.render_ctx(&ctx).unwrap_err();
    assert!(
        err.to_string().contains("enum"),
        "should mention enum requirement: {err}"
    );
}

#[test]
fn kind_key_not_accessible_via_dot_path() {
    // The internal __kind__ key must be rejected at compile time,
    // not at render time.
    let err = crate::Template::from_source(
        r"---
params: [outcome = struct(evidence = str)]
---
{{ outcome.__kind__ }}",
    )
    .unwrap_err();
    assert!(
        err.to_string().contains("not allowed") || err.to_string().contains("internal key"),
        "__kind__ should be rejected at compile time: {err}"
    );
}

#[test]
fn kind_key_rejected_in_condition() {
    // __kind__ access in conditions should also be rejected at compile time.
    let err = crate::Template::from_source(
        "---\nparams: [x = struct(name = str)]\n---\n\
         > {% if x.__kind__ == \"foo\" %}\n\n\
         yes\n\n\
         > {% /if %}",
    )
    .unwrap_err();
    assert!(
        err.to_string().contains("not allowed") || err.to_string().contains("internal key"),
        "__kind__ in condition should be rejected at compile time: {err}"
    );
}

#[test]
fn get_field_unchecked_returns_normal_fields() {
    let dict = Value::Struct(Arc::new(HashMap::from([
        ("name".into(), Value::Str("Alice".into())),
        ("score".into(), Value::Int(95)),
    ])));
    assert_eq!(
        dict.get_field_unchecked("name"),
        Some(&Value::Str("Alice".into()))
    );
    assert_eq!(dict.get_field_unchecked("score"), Some(&Value::Int(95)));
    assert_eq!(dict.get_field_unchecked("missing"), None);
}

#[test]
fn get_field_unchecked_on_non_struct_returns_none() {
    assert_eq!(Value::Str("x".into()).get_field_unchecked("any"), None);
    assert_eq!(Value::Int(1).get_field_unchecked("any"), None);
}

#[test]
#[cfg(debug_assertions)]
#[should_panic(expected = "ENUM_TAG_KEY")]
fn get_field_unchecked_debug_asserts_on_tag_key() {
    let dict = Value::Struct(Arc::new(HashMap::from([(
        crate::consts::ENUM_TAG_KEY.into(),
        Value::Str("Variant".into()),
    )])));
    // This should trigger the debug_assert.
    // NOLINT: test asserts the field exists without using the value
    let _ = dict.get_field_unchecked(crate::consts::ENUM_TAG_KEY);
}

#[test]
fn user_field_named_tag_does_not_collide() {
    // A user field named "tag" must not collide with the internal __kind__ key.
    let tmpl = crate::Template::from_source(
        r"---
params: [entry = struct(tag = str)]
---
{{ kind(entry) }}: {{ entry.tag }}",
    )
    .unwrap();
    let mut ctx = crate::Context::new();
    ctx.set(
        "entry",
        Value::Struct(Arc::new(HashMap::from([
            (
                crate::consts::ENUM_TAG_KEY.into(),
                Value::Str("Woche".into()),
            ),
            ("tag".into(), Value::Str("Montag".into())),
        ]))),
    );
    assert_eq!(tmpl.render_ctx(&ctx).unwrap(), "Woche: Montag");
}

// -- parse_function_call edge cases --

#[test]
fn parse_function_call_valid() {
    let result = parse_function_call("idx(item)");
    assert_eq!(result, Some(("idx", "item")));
}

#[test]
fn parse_function_call_empty_func_returns_none() {
    // `(arg)` — empty function name.
    assert_eq!(parse_function_call("(arg)"), None);
}

#[test]
fn parse_function_call_empty_arg_returns_none() {
    // `func()` — empty argument.
    assert_eq!(parse_function_call("func()"), None);
}

#[test]
fn parse_function_call_no_parens_returns_none() {
    assert_eq!(parse_function_call("just_a_name"), None);
}

#[test]
fn parse_function_call_dotted_name_returns_none() {
    // Dotted names are not valid function identifiers.
    assert_eq!(parse_function_call("foo.bar(x)"), None);
}

// -- ConditionOperand compile & resolve --

#[test]
fn resolve_value_or_literal_string_literal() {
    let ctx = Context::new();
    let scope = Scope::new(&ctx);
    let operand = ConditionOperand::compile("\"hello\"").unwrap();
    let val = operand.resolve(&scope).unwrap();
    assert_eq!(*val, Value::Str("hello".into()));
}

#[test]
fn resolve_value_or_literal_bool_true() {
    let ctx = Context::new();
    let scope = Scope::new(&ctx);
    let operand = ConditionOperand::compile("true").unwrap();
    assert_eq!(*operand.resolve(&scope).unwrap(), Value::Bool(true));
}

#[test]
fn resolve_value_or_literal_integer() {
    let ctx = Context::new();
    let scope = Scope::new(&ctx);
    let operand = ConditionOperand::compile("42").unwrap();
    assert_eq!(*operand.resolve(&scope).unwrap(), Value::Int(42));
}

#[test]
fn resolve_value_or_literal_float() {
    let ctx = Context::new();
    let scope = Scope::new(&ctx);
    let operand = ConditionOperand::compile("2.78").unwrap();
    assert_eq!(*operand.resolve(&scope).unwrap(), Value::Float(2.78));
}

#[test]
fn resolve_value_or_literal_empty_token_returns_error() {
    let err = ConditionOperand::compile("").unwrap_err();
    assert!(matches!(err, TemplateError::Syntax(_)));
}

// -- include depth tracking --

#[test]
fn enter_include_enforces_max_depth() {
    let ctx = Context::new();
    let mut scope = Scope::new(&ctx).with_max_include_depth(2);
    scope.enter_include().unwrap();
    scope.enter_include().unwrap();
    // Third should exceed depth of 2.
    let err = scope.enter_include().unwrap_err();
    assert!(err.to_string().contains("maximum include depth"));
}

#[test]
fn exit_include_decrements_and_allows_reentry() {
    let ctx = Context::new();
    let mut scope = Scope::new(&ctx).with_max_include_depth(1);
    scope.enter_include().unwrap();
    scope.exit_include();
    // After exiting, re-entering should succeed.
    scope.enter_include().unwrap();
}

// -- pop_layer on empty scope --

#[test]
fn pop_layer_on_empty_scope_is_noop() {
    let ctx = Context::new();
    let mut scope = Scope::new(&ctx);
    // Should not panic.
    scope.pop_layer();
    scope.pop_layer();
    assert_eq!(scope.resolve("anything"), None);
}

// -- constants resolution --

#[test]
fn consts_take_priority_over_context() {
    let mut ctx = Context::new();
    ctx.set("x", "from_ctx");
    let mut scope = Scope::new(&ctx);
    let consts = Arc::new(HashMap::from([(
        "x".into(),
        Value::Str("from_const".into()),
    )]));
    let imported = Arc::new(HashMap::new());
    scope.set_consts(&consts, &imported);
    // Constants should shadow context values.
    assert_eq!(scope.resolve("x"), Some(&Value::Str("from_const".into())));
}

#[test]
fn push_pop_consts_restores_context_value() {
    let mut ctx = Context::new();
    ctx.set("y", "original");
    let mut scope = Scope::new(&ctx);
    scope.push_consts(
        HashMap::from([("y".into(), Value::Str("overridden".into()))]),
        HashMap::new(),
    );
    assert_eq!(scope.resolve("y"), Some(&Value::Str("overridden".into())));
    scope.pop_consts();
    assert_eq!(scope.resolve("y"), Some(&Value::Str("original".into())));
}

#[test]
fn push_loop_binding_reuses_string_alloc() {
    let ctx = Context::new();
    let mut scope = Scope::new(&ctx);

    // First iteration allocates a new string.
    let val1 = Value::Str("hello".into());
    scope.push_loop_binding("item", &val1);
    assert_eq!(scope.resolve("item"), Some(&Value::Str("hello".into())));
    scope.pop_loop_binding();

    // Second iteration should reuse the allocation (no new alloc).
    let val2 = Value::Str("world".into());
    scope.push_loop_binding("item", &val2);
    assert_eq!(scope.resolve("item"), Some(&Value::Str("world".into())));
    scope.pop_loop_binding();

    // Mixed types: string → int → string — should still work.
    let val_int = Value::Int(42);
    scope.push_loop_binding("item", &val_int);
    assert_eq!(scope.resolve("item"), Some(&Value::Int(42)));
    scope.pop_loop_binding();

    let val3 = Value::Str("back to string".into());
    scope.push_loop_binding("item", &val3);
    assert_eq!(
        scope.resolve("item"),
        Some(&Value::Str("back to string".into()))
    );
    scope.pop_loop_binding();
}

#[test]
fn for_loop_string_items_render_correctly() {
    // End-to-end test: loop over strings renders each one correctly.
    let tmpl = crate::Template::from_source(
        "---\nparams: [items = list(str)]\n---\n\
         > {% for item in items %}\n\n\
         - {{ item }}\n\n\
         > {% /for %}",
    )
    .unwrap();
    let mut ctx = Context::new();
    ctx.set(
        "items",
        Value::List(std::sync::Arc::new(vec![
            Value::Str("alpha".into()),
            Value::Str("beta".into()),
            Value::Str("gamma".into()),
        ])),
    );
    let output = tmpl.render_ctx(&ctx).unwrap();
    assert!(output.contains("- alpha"), "missing alpha: {output}");
    assert!(output.contains("- beta"), "missing beta: {output}");
    assert!(output.contains("- gamma"), "missing gamma: {output}");
}