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
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
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
//! Rendering pre-compiled template segments.
//!
//! Walks the compiled segment tree and produces output text by
//! evaluating expressions, loops, conditionals, and includes at
//! render time.

#[cfg(not(feature = "std"))]
use alloc::string::ToString;
use alloc::{borrow::Cow, string::String, sync::Arc};

use super::{ComparisonOp, CompiledInclude, Condition, MatchArm, ParsedFilter, Segment};
use crate::{
    error::TemplateError,
    parser,
    scope::{CompiledExpr, CompiledPath, Scope},
    value::Value,
};

/// Estimated output size for a single expression or include tag.
const ESTIMATED_EXPR_SIZE: usize = 32;

/// Estimated iteration count for for-loop capacity estimation.
const ESTIMATED_LOOP_ITERATIONS: usize = 4;

/// Maximum precision for the fast integer-math path.
/// Above this, f64 loses precision so we fall back to std formatting.
const MAX_FAST_FIXED_PRECISION: usize = 18;

/// Pre-computed powers of 10 for precision 0..=18.
const POW10: [f64; 19] = {
    let mut table = [1.0; 19];
    let mut i = 1;
    while i < 19 {
        table[i] = table[i - 1] * 10.0;
        i += 1;
    }
    table
};

/// Write a float with fixed precision into `output`, avoiding the heavy
/// `std::fmt::float_to_decimal_common_exact` machinery.
///
/// For precision ≤ 18, this uses multiply-round-truncate + `itoa`, which
/// is ~3× faster than `write!("{f:.precision$}")`.
#[inline]
fn write_fixed_float(f: f64, precision: usize, output: &mut String) {
    if precision > MAX_FAST_FIXED_PRECISION || !f.is_finite() {
        // Fallback for extreme precision or NaN/Inf.
        use core::fmt::Write;
        write!(output, "{f:.precision$}").expect("fmt::Write for String is infallible");
        return;
    }

    let is_neg = f.is_sign_negative() && f != 0.0;
    let abs = f.abs();

    // Multiply by 10^precision and round to nearest integer.
    let scale = POW10[precision];
    // Safety: abs >= 0, scale > 0 → always positive; precision ≤ 18 → fits u64.
    #[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
    let scaled = (abs * scale + 0.5) as u64;

    if precision == 0 {
        if is_neg {
            output.push('-');
        }
        let mut buf = itoa::Buffer::new();
        output.push_str(buf.format(scaled));
        return;
    }

    // Split into integer and fractional parts.
    // Safety: POW10 entries are exact positive integers ≤ 10^18, all fit u64.
    #[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
    let divisor = scale as u64;
    let int_part = scaled / divisor;
    let frac_part = scaled % divisor;

    if is_neg {
        output.push('-');
    }

    let mut buf = itoa::Buffer::new();
    output.push_str(buf.format(int_part));
    output.push('.');

    // Pad fractional part with leading zeros.
    let mut frac_buf = itoa::Buffer::new();
    let frac_str = frac_buf.format(frac_part);
    let pad = precision - frac_str.len();
    for _ in 0..pad {
        output.push('0');
    }
    output.push_str(frac_str);
}

/// Estimate the total static text size for `String::with_capacity`.
#[must_use]
pub fn estimate_output_capacity(segments: &[Segment]) -> usize {
    let mut size: usize = 0;
    for seg in segments {
        match seg {
            Segment::Static(s) | Segment::Raw(s) => size += s.len(),
            Segment::Expr { .. } | Segment::Include(_) | Segment::Panic(_) => {
                size += ESTIMATED_EXPR_SIZE;
            }
            Segment::Comment(_) => {} // No output.
            Segment::ForLoop {
                body, else_body, ..
            } => {
                size += estimate_output_capacity(body) * ESTIMATED_LOOP_ITERATIONS;
                size += estimate_output_capacity(else_body);
            }
            Segment::If {
                branches,
                else_body,
            } => {
                // At most one branch is rendered — use the max, not the sum.
                let mut max_branch = estimate_output_capacity(else_body);
                for (_, branch_body) in branches {
                    max_branch = max_branch.max(estimate_output_capacity(branch_body));
                }
                size += max_branch;
            }
            Segment::Match { arms, .. } => {
                // At most one arm is rendered — use the max, not the sum.
                let mut max_arm = 0;
                for arm in arms {
                    max_arm = max_arm.max(estimate_output_capacity(&arm.body));
                }
                size += max_arm;
            }
        }
    }
    size
}

/// Render pre-compiled segments into a string.
///
/// # Errors
///
/// Returns [`TemplateError`] on variable resolution, filter, or include
/// errors.
#[cfg(feature = "std")]
pub(crate) fn render_segments(
    segments: &[Segment],
    scope: &mut Scope<'_>,
    base_dir: Option<&std::path::Path>,
) -> Result<String, TemplateError> {
    let mut output = String::with_capacity(estimate_output_capacity(segments));
    render_segments_into(segments, scope, base_dir, &mut output)?;
    Ok(output)
}

/// Render segments into an existing output buffer.
#[cfg(feature = "std")]
#[inline]
pub(crate) fn render_segments_into(
    segments: &[Segment],
    scope: &mut Scope<'_>,
    base_dir: Option<&std::path::Path>,
    output: &mut String,
) -> Result<(), TemplateError> {
    for segment in segments {
        match segment {
            Segment::Static(text) | Segment::Raw(text) => output.push_str(text),
            Segment::Expr { expr, filters } => {
                eval_compiled_expr_into(expr, filters, scope, output)?;
            }
            Segment::ForLoop {
                binding,
                list_expr,
                body,
                else_body,
            } => {
                render_for_loop(
                    binding.as_ref(),
                    list_expr,
                    body,
                    else_body,
                    scope,
                    base_dir,
                    output,
                )?;
            }
            Segment::If {
                branches,
                else_body,
            } => {
                render_if(branches, else_body, scope, base_dir, output)?;
            }
            Segment::Match {
                expr,
                arms,
                is_option,
            } => {
                render_match(expr, arms, *is_option, scope, base_dir, output)?;
            }
            Segment::Include(inc) => {
                render_include(inc, scope, base_dir, output)?;
            }
            Segment::Panic(segments) => {
                let msg = render_segments(segments, scope, base_dir)?;
                return Err(TemplateError::panic(msg));
            }
            Segment::Comment(_) => {
                // Comments produce no output.
            }
        }
    }
    Ok(())
}

/// Render segments into an existing output buffer (`no_std` variant).
///
/// Include directives are not supported and produce a runtime error.
#[cfg(not(feature = "std"))]
pub(crate) fn render_segments_into_no_std(
    segments: &[Segment],
    scope: &mut Scope<'_>,
    output: &mut String,
) -> Result<(), TemplateError> {
    for segment in segments {
        match segment {
            Segment::Static(text) | Segment::Raw(text) => output.push_str(text),
            Segment::Expr { expr, filters } => {
                eval_compiled_expr_into(expr, filters, scope, output)?;
            }
            Segment::ForLoop {
                binding,
                list_expr,
                body,
                else_body,
            } => {
                render_for_loop_no_std(
                    binding.as_ref(),
                    list_expr,
                    body,
                    else_body,
                    scope,
                    output,
                )?;
            }
            Segment::If {
                branches,
                else_body,
            } => {
                render_if_no_std(branches, else_body, scope, output)?;
            }
            Segment::Match {
                expr,
                arms,
                is_option,
            } => {
                render_match_no_std(expr, arms, *is_option, scope, output)?;
            }
            Segment::Include(inc) => {
                render_include_no_std(inc, scope, output)?;
            }
            Segment::Panic(segments) => {
                let mut msg = String::new();
                render_segments_into_no_std(segments, scope, &mut msg)?;
                return Err(TemplateError::panic(msg));
            }
            Segment::Comment(_) => {
                // Comments produce no output.
            }
        }
    }
    Ok(())
}

/// Write a rendered [`Value`] directly into an output buffer,
/// avoiding an intermediate `String` allocation.
#[inline]
fn render_value_into(val: &Value, output: &mut String) -> Result<(), TemplateError> {
    match val {
        Value::Str(s) => output.push_str(s),
        // Direct push avoids the `write!` → `fmt` machinery.
        Value::Bool(true) => output.push_str("true"),
        Value::Bool(false) => output.push_str("false"),
        // itoa/ryu are ~3x faster than `write!` for number formatting.
        Value::Int(i) => {
            let mut buf = itoa::Buffer::new();
            output.push_str(buf.format(*i));
        }
        // Float formatting via Display — benchmarks show it's faster
        // than ryu+strip_suffix for whole numbers (the common case).
        Value::Float(f) => {
            use core::fmt::Write;
            // SAFETY: `fmt::Write for String` is infallible — it only
            // forwards to `String::push_str` which cannot fail.
            write!(output, "{f}").expect("fmt::Write for String is infallible");
        }
        Value::None => { /* Absent value renders as empty. */ }
        Value::List(_) | Value::Struct(_) | Value::Tmpl(_) => {
            return Err(TemplateError::syntax(alloc::format!(
                "cannot display value of type '{}'",
                val.type_name()
            )));
        }
    }
    Ok(())
}

/// Evaluate a pre-compiled expression (path + filters) and write
/// the result directly into `output`, avoiding intermediate `String`
/// allocations in the common no-filter path.
fn eval_compiled_expr_into(
    expr: &CompiledExpr,
    filters: &[ParsedFilter],
    scope: &Scope<'_>,
    output: &mut String,
) -> Result<(), TemplateError> {
    if try_fast_path_fixed_filter(expr, filters, scope, output)? {
        return Ok(());
    }

    if filters.is_empty() {
        if let CompiledExpr::Path(path) = expr {
            let val = scope.resolve_path(path)?;
            return render_value_into(val, output);
        }
        if let CompiledExpr::Kind(path) = expr {
            let val = scope.resolve_path(path)?;
            if scope.is_option_path(path.as_str()) {
                match val {
                    Value::None => output.push_str(crate::consts::OPTION_NONE),
                    _ => output.push_str(crate::consts::OPTION_SOME),
                }
                return Ok(());
            }
            match val {
                Value::Struct(d) => {
                    if let Some(Value::Str(k)) = d.get(crate::consts::ENUM_TAG_KEY) {
                        output.push_str(k);
                        return Ok(());
                    }
                    return Err(TemplateError::syntax(
                        "kind() requires an enum value (dict with variant tag)",
                    ));
                }
                Value::Str(s) => {
                    output.push_str(s);
                    return Ok(());
                }
                Value::None => {
                    output.push_str(crate::consts::OPTION_NONE);
                    return Ok(());
                }
                _ => {
                    return Err(TemplateError::syntax(alloc::format!(
                        "kind() requires an enum value, got {}",
                        val.type_name()
                    )));
                }
            }
        }
    }

    let value = eval_compiled_expr_val(expr, scope)?;
    apply_filters_and_render(value, filters, output)
}

/// Fast path: single `fixed(n)` filter on a numeric value — write directly
/// into `output` without allocating an intermediate String or Value.
#[inline]
fn try_fast_path_fixed_filter(
    expr: &CompiledExpr,
    filters: &[ParsedFilter],
    scope: &Scope<'_>,
    output: &mut String,
) -> Result<bool, TemplateError> {
    if filters.len() == 1 && filters[0].kind == super::FilterKind::Fixed {
        if let CompiledExpr::Path(path) = expr {
            if let Some(precision) = filters[0].parsed_num {
                let val = scope.resolve_path(path)?;
                match val {
                    Value::Float(f) => {
                        write_fixed_float(*f, precision, output);
                        return Ok(true);
                    }
                    Value::Int(i) => {
                        // Use itoa for the integer part to avoid
                        // i64→f64 precision loss for values > 2^53.
                        let mut buf = itoa::Buffer::new();
                        let int_str = buf.format(*i);
                        output.push_str(int_str);
                        if precision > 0 {
                            output.push('.');
                            for _ in 0..precision {
                                output.push('0');
                            }
                        }
                        return Ok(true);
                    }
                    _ => {}
                }
            }
        }
    }
    Ok(false)
}

fn eval_compiled_expr_val<'a>(
    expr: &'a CompiledExpr,
    scope: &'a Scope<'_>,
) -> Result<Cow<'a, Value>, TemplateError> {
    match expr {
        CompiledExpr::Path(path) => scope.resolve_path(path).map(Cow::Borrowed),
        CompiledExpr::Idx(binding) => {
            let meta = scope.get_loop_meta(binding).ok_or_else(|| {
                TemplateError::syntax(alloc::format!(
                    "idx() requires active loop binding '{binding}'"
                ))
            })?;
            Ok(Cow::Owned(Value::Int(meta.index)))
        }
        CompiledExpr::Len(path) => {
            let val = scope.resolve_path(path)?;
            let count = match val {
                Value::List(l) => i64::try_from(l.len()).expect("collection length fits i64"),
                Value::Str(s) => i64::try_from(s.len()).expect("string length fits i64"),
                _ => {
                    return Err(TemplateError::syntax("len() requires a list or string"));
                }
            };
            Ok(Cow::Owned(Value::Int(count)))
        }
        CompiledExpr::Kind(path) => {
            let val = scope.resolve_path(path)?;
            if scope.is_option_path(path.as_str()) {
                return match val {
                    Value::None => Ok(Cow::Owned(Value::Str(crate::consts::OPTION_NONE.into()))),
                    _ => Ok(Cow::Owned(Value::Str(crate::consts::OPTION_SOME.into()))),
                };
            }
            match val {
                Value::Struct(d) => {
                    if let Some(Value::Str(k)) = d.get(crate::consts::ENUM_TAG_KEY) {
                        Ok(Cow::Owned(Value::Str(k.clone())))
                    } else {
                        Err(TemplateError::syntax(
                            "kind() requires an enum value (dict with variant tag)",
                        ))
                    }
                }
                Value::Str(s) => Ok(Cow::Owned(Value::Str(s.clone()))),
                Value::None => Ok(Cow::Owned(Value::Str(crate::consts::OPTION_NONE.into()))),
                _ => Err(TemplateError::syntax(alloc::format!(
                    "kind() requires an enum value, got {}",
                    val.type_name()
                ))),
            }
        }
        CompiledExpr::Kinds(path) => {
            let val = scope.resolve_path(path)?;
            match val {
                Value::Struct(d) => {
                    if let Some(list_val) = d.get(crate::consts::ENUM_VARIANTS_KEY) {
                        Ok(Cow::Borrowed(list_val))
                    } else {
                        Err(TemplateError::syntax(
                            "kinds() requires an enum type namespace",
                        ))
                    }
                }
                _ => Err(TemplateError::syntax(alloc::format!(
                    "kinds() requires an enum type namespace, got {}",
                    val.type_name()
                ))),
            }
        }
        CompiledExpr::Has(path) => {
            let val = scope.resolve_path(path)?;
            Ok(Cow::Owned(Value::Bool(Scope::is_option_some(val))))
        }
    }
}

/// Apply filters to a resolved value and render the result.
fn apply_filters_and_render(
    value: Cow<'_, Value>,
    filters: &[ParsedFilter],
    output: &mut String,
) -> Result<(), TemplateError> {
    if filters.is_empty() {
        render_value_into(&value, output)
    } else {
        let mut owned_value = value.into_owned();
        for f in filters {
            owned_value = crate::filter::apply_filter_typed(
                f.kind,
                &owned_value,
                f.args.as_ref().map(AsRef::as_ref),
            )?;
        }
        render_value_into(&owned_value, output)
    }
}

/// Register loop metadata for a for-loop binding.
///
/// After pushing a scope layer and inserting the binding variable,
/// call this to associate `{{ idx(binding) }}` metadata.
#[inline]
pub(crate) fn register_loop_meta(scope: &mut Scope<'_>, binding: &str, i: usize) {
    // SAFETY: `usize` always fits in `i64` on both 32-bit and 64-bit
    // platforms (`usize::MAX <= i64::MAX` even on 64-bit because
    // `usize::MAX` is 2^64-1 which exceeds `i64::MAX`, but collection
    // lengths are bounded by `isize::MAX` which always fits).
    #[allow(clippy::cast_possible_wrap)]
    let index = i as i64;
    debug_assert!(index >= 0, "loop index overflowed i64");
    scope.set_loop_meta(binding, crate::scope::LoopMeta { index });
}

/// Render a compiled for-loop.
#[cfg(feature = "std")]
#[inline]
fn render_for_loop(
    binding: &str,
    list_expr: &CompiledExpr,
    body: &[Segment],
    else_body: &[Segment],
    scope: &mut Scope<'_>,
    base_dir: Option<&std::path::Path>,
    output: &mut String,
) -> Result<(), TemplateError> {
    let list_ref = eval_compiled_expr_val(list_expr, scope)?;
    let items = if let Value::List(items) = &*list_ref {
        Arc::clone(items)
    } else {
        let expr_str = match list_expr {
            CompiledExpr::Path(p)
            | CompiledExpr::Len(p)
            | CompiledExpr::Kind(p)
            | CompiledExpr::Kinds(p)
            | CompiledExpr::Has(p) => p.as_str(),
            CompiledExpr::Idx(b) => b.as_ref(),
        };
        return Err(TemplateError::syntax(alloc::format!(
            "'{expr_str}' is not a list"
        )));
    };

    if items.is_empty() && !else_body.is_empty() {
        return render_segments_into(else_body, scope, base_dir, output);
    }

    for (i, item) in items.iter().enumerate() {
        scope.push_loop_binding(binding, item);
        register_loop_meta(scope, binding, i);
        render_segments_into(body, scope, base_dir, output)?;
        scope.pop_loop_binding();
    }

    Ok(())
}

/// Render a compiled for-loop (`no_std` variant).
#[cfg(not(feature = "std"))]
fn render_for_loop_no_std(
    binding: &str,
    list_expr: &CompiledExpr,
    body: &[Segment],
    else_body: &[Segment],
    scope: &mut Scope<'_>,
    output: &mut String,
) -> Result<(), TemplateError> {
    let list_ref = eval_compiled_expr_val(list_expr, scope)?;
    let items = match &*list_ref {
        Value::List(items) => Arc::clone(items),
        _ => {
            let expr_str = match list_expr {
                CompiledExpr::Path(p)
                | CompiledExpr::Len(p)
                | CompiledExpr::Kind(p)
                | CompiledExpr::Kinds(p)
                | CompiledExpr::Has(p) => p.as_str(),
                CompiledExpr::Idx(b) => b.as_ref(),
            };
            return Err(TemplateError::syntax(alloc::format!(
                "'{}' is not a list",
                expr_str
            )));
        }
    };

    if items.is_empty() && !else_body.is_empty() {
        return render_segments_into_no_std(else_body, scope, output);
    }

    for (i, item) in items.iter().enumerate() {
        scope.push_loop_binding(binding, item);
        register_loop_meta(scope, binding, i);
        render_segments_into_no_std(body, scope, output)?;
        scope.pop_loop_binding();
    }

    Ok(())
}

/// Render a compiled conditional (if/elif/else chain).
///
/// Evaluates each branch's [`Condition`] in order, rendering the body
/// of the first match. Falls through to `else_body` when no branch
/// matches.
#[cfg(feature = "std")]
#[inline]
fn render_if(
    branches: &[(Condition, alloc::vec::Vec<Segment>)],
    else_body: &[Segment],
    scope: &mut Scope<'_>,
    base_dir: Option<&std::path::Path>,
    output: &mut String,
) -> Result<(), TemplateError> {
    for (condition, body) in branches {
        if eval_condition(condition, scope)? {
            return render_segments_into(body, scope, base_dir, output);
        }
    }

    if !else_body.is_empty() {
        render_segments_into(else_body, scope, base_dir, output)?;
    }

    Ok(())
}

/// Render a compiled conditional (`no_std` variant).
#[cfg(not(feature = "std"))]
fn render_if_no_std(
    branches: &[(Condition, alloc::vec::Vec<Segment>)],
    else_body: &[Segment],
    scope: &mut Scope<'_>,
    output: &mut String,
) -> Result<(), TemplateError> {
    for (condition, body) in branches {
        if eval_condition(condition, scope)? {
            return render_segments_into_no_std(body, scope, output);
        }
    }

    if !else_body.is_empty() {
        render_segments_into_no_std(else_body, scope, output)?;
    }

    Ok(())
}

/// Render a compiled match block.
///
/// Resolves the expression to determine the active enum variant, then
/// renders the body of the matching `{% case %}` arm. Silently produces
/// no output if no arm matches (non-exhaustive matches are caught at
/// compile time by the type system).
#[cfg(feature = "std")]
fn render_match(
    expr: &CompiledPath,
    arms: &[MatchArm],
    is_option: bool,
    scope: &mut Scope<'_>,
    base_dir: Option<&std::path::Path>,
    output: &mut String,
) -> Result<(), TemplateError> {
    let active_variant = resolve_match_variant(expr, is_option, scope)?;

    for arm in arms {
        let variant_matches = arm
            .variants
            .iter()
            .any(|v| v.as_ref() == crate::consts::MATCH_DEFAULT || active_variant == v.as_ref());
        if variant_matches {
            // Evaluate guard if present.
            if let Some(ref guard) = arm.guard {
                if !eval_condition(guard, scope)? {
                    continue;
                }
            }
            return render_segments_into(&arm.body, scope, base_dir, output);
        }
    }

    // No arm matched — silently skip (non-exhaustive is valid for
    // inline `{% match x case Y %}` single-arm guards).
    Ok(())
}

/// Render a compiled match block (`no_std` variant).
#[cfg(not(feature = "std"))]
fn render_match_no_std(
    expr: &CompiledPath,
    arms: &[MatchArm],
    is_option: bool,
    scope: &mut Scope<'_>,
    output: &mut String,
) -> Result<(), TemplateError> {
    let active_variant = resolve_match_variant(expr, is_option, scope)?;

    for arm in arms {
        let variant_matches = arm
            .variants
            .iter()
            .any(|v| v.as_ref() == crate::consts::MATCH_DEFAULT || active_variant == v.as_ref());
        if variant_matches {
            if let Some(ref guard) = arm.guard {
                if !eval_condition(guard, scope)? {
                    continue;
                }
            }
            return render_segments_into_no_std(&arm.body, scope, output);
        }
    }

    Ok(())
}

/// Resolve the active enum/option variant name for a match expression.
///
/// When `is_option` is `true`, uses `Value::None` discriminant check
/// (zero-cost) instead of string-based variant tag lookup.
fn resolve_match_variant<'a>(
    expr: &CompiledPath,
    is_option: bool,
    scope: &'a Scope<'_>,
) -> Result<&'a str, TemplateError> {
    let value = scope.resolve_path(expr)?;

    match value {
        // Absent option value → "None" variant.
        Value::None => Ok(crate::consts::OPTION_NONE),
        // For option(T): any non-None value is the "Some" branch.
        _ if is_option => Ok(crate::consts::OPTION_SOME),
        // Unit enum variant stored as plain string.
        Value::Str(s) => Ok(s.as_str()),
        // Struct variant stored as dict with "tag" key.
        Value::Struct(map) => {
            let tag_key = crate::consts::ENUM_TAG_KEY;
            match map.get(tag_key) {
                Some(Value::Str(tag)) => Ok(tag.as_str()),
                _ => Err(TemplateError::syntax(alloc::format!(
                    "match: \'{}\' is a dict without a \'tag\' field",
                    expr.as_str()
                ))),
            }
        }
        _ => Err(TemplateError::syntax(alloc::format!(
            "match: \'{}\' is not an enum value (got {})",
            expr.as_str(),
            value.type_name()
        ))),
    }
}

// ---------------------------------------------------------------------------
// Value comparison helpers (used by eval_condition)
// ---------------------------------------------------------------------------

/// Compare an `i64` against an `f64` without any `as`-based integer↔float casts.
///
/// Decomposes the `f64` into its integer and fractional parts using IEEE 754 bit
/// manipulation, then compares using only integer arithmetic.
fn cmp_int_float(i: i64, f: f64) -> Option<core::cmp::Ordering> {
    if f.is_nan() {
        return None;
    }
    if f.is_infinite() {
        return if f.is_sign_positive() {
            Some(core::cmp::Ordering::Less)
        } else {
            Some(core::cmp::Ordering::Greater)
        };
    }

    let (f_int, f_has_frac, f_negative) = decompose_f64(f);

    let i_wide = i128::from(i);
    let f_signed = if f_negative {
        -i128::from(f_int)
    } else {
        i128::from(f_int)
    };

    match i_wide.cmp(&f_signed) {
        core::cmp::Ordering::Less => Some(core::cmp::Ordering::Less),
        core::cmp::Ordering::Greater => Some(core::cmp::Ordering::Greater),
        core::cmp::Ordering::Equal => {
            if !f_has_frac {
                Some(core::cmp::Ordering::Equal)
            } else if f_negative {
                Some(core::cmp::Ordering::Greater)
            } else {
                Some(core::cmp::Ordering::Less)
            }
        }
    }
}

/// Decompose a finite `f64` into `(integer_abs: u64, has_frac: bool, negative: bool)`.
///
/// Uses IEEE 754 double-precision bit layout to extract the integer part
/// without any float→int or int→float casts.
fn decompose_f64(f: f64) -> (u64, bool, bool) {
    debug_assert!(f.is_finite(), "decompose_f64 requires finite input");

    let bits = f.to_bits();
    let negative = (bits >> 63) != 0;
    let raw_exp = (bits >> 52) & 0x7FF; // u64, 11 bits
    let mantissa = bits & 0x000F_FFFF_FFFF_FFFF;

    // Zero (positive or negative).
    if raw_exp == 0 && mantissa == 0 {
        return (0, false, negative);
    }
    // Subnormal: |f| < 1 → integer part is 0.
    if raw_exp == 0 {
        return (0, true, negative);
    }
    // Normal: exponent = raw_exp - 1023. If raw_exp < 1023, |f| < 1.
    if raw_exp < 1023 {
        return (0, true, negative);
    }

    let exp = raw_exp - 1023; // >= 0, u64

    // Full mantissa with implicit leading 1: (2^52 + mantissa)
    let full_mantissa = (1_u64 << 52) | mantissa;

    if exp >= 52 {
        // All mantissa bits are integer; no fractional part.
        let shift = exp - 52;
        if shift >= 64 {
            (u64::MAX, false, negative) // overflow; handled via i128 above
        } else {
            (full_mantissa << shift, false, negative)
        }
    } else {
        let shift = 52 - exp;
        let int_part = full_mantissa >> shift;
        let frac_mask = (1_u64 << shift) - 1;
        let has_frac = (full_mantissa & frac_mask) != 0;
        (int_part, has_frac, negative)
    }
}

/// Mixed-type numeric partial ordering.
///
/// Returns `Some(Ordering)` for numeric comparisons (int vs int, float vs float,
/// int vs float, float vs int). Returns `None` for non-numeric types.
fn partial_cmp_values(a: &Value, b: &Value) -> Option<core::cmp::Ordering> {
    match (a, b) {
        (Value::Int(x), Value::Int(y)) => x.partial_cmp(y),
        (Value::Float(x), Value::Float(y)) => x.partial_cmp(y),
        (Value::Int(x), Value::Float(y)) => cmp_int_float(*x, *y),
        (Value::Float(x), Value::Int(y)) => cmp_int_float(*y, *x).map(core::cmp::Ordering::reverse),
        _ => None,
    }
}

/// Evaluate a pre-parsed condition without any string scanning.
pub(super) fn eval_condition(
    condition: &Condition,
    scope: &Scope<'_>,
) -> Result<bool, TemplateError> {
    match condition {
        Condition::Truthy(operand) => {
            let value = operand.resolve(scope)?;
            Ok(value.is_truthy())
        }
        Condition::Not(inner) => {
            let result = eval_condition(inner, scope)?;
            Ok(!result)
        }
        Condition::And(left, right) => {
            // Short-circuit: if left is false, don't evaluate right.
            if !eval_condition(left, scope)? {
                return Ok(false);
            }
            eval_condition(right, scope)
        }
        Condition::Or(left, right) => {
            // Short-circuit: if left is true, don't evaluate right.
            if eval_condition(left, scope)? {
                return Ok(true);
            }
            eval_condition(right, scope)
        }
        Condition::Comparison { left, op, right } => {
            let left_val = left.resolve(scope)?;
            let right_val = right.resolve(scope)?;
            let result = match op {
                ComparisonOp::Eq => *left_val == *right_val,
                ComparisonOp::Ne => *left_val != *right_val,
                ComparisonOp::Le => partial_cmp_values(&left_val, &right_val)
                    .is_some_and(core::cmp::Ordering::is_le),
                ComparisonOp::Ge => partial_cmp_values(&left_val, &right_val)
                    .is_some_and(core::cmp::Ordering::is_ge),
                ComparisonOp::Lt => partial_cmp_values(&left_val, &right_val)
                    .is_some_and(core::cmp::Ordering::is_lt),
                ComparisonOp::Gt => partial_cmp_values(&left_val, &right_val)
                    .is_some_and(core::cmp::Ordering::is_gt),
                ComparisonOp::In => match &*right_val {
                    Value::List(right_items) => match &*left_val {
                        Value::List(left_items) => {
                            left_items.iter().all(|l| right_items.contains(l))
                        }
                        scalar => right_items.contains(scalar),
                    },
                    Value::Str(right_str) => match &*left_val {
                        Value::Str(left_str) => right_str.contains(left_str.as_str()),
                        Value::List(left_items) => left_items.iter().all(|l| {
                            if let Value::Str(s) = l {
                                right_str.contains(s.as_str())
                            } else {
                                false
                            }
                        }),
                        _ => false,
                    },
                    _ => false,
                },
            };
            Ok(result)
        }
        Condition::MatchVariant {
            expr,
            variants,
            is_option,
        } => {
            let active_variant = resolve_match_variant(expr, *is_option, scope)?;
            Ok(variants.iter().any(|v| {
                v.as_ref() == crate::consts::MATCH_DEFAULT || active_variant == v.as_ref()
            }))
        }
    }
}

/// Render a compiled include directive.
///
/// Includes still load files at runtime (since the included file might
/// change), but the host template's structure is pre-compiled.
#[cfg(feature = "std")]
fn render_include(
    inc: &CompiledInclude,
    scope: &mut Scope<'_>,
    base_dir: Option<&std::path::Path>,
    output: &mut String,
) -> Result<(), TemplateError> {
    // Build an IncludeDirective from the compiled data, borrowing from the
    // owned strings.
    let with_vars: alloc::vec::Vec<(&str, &str)> = inc
        .with_vars
        .iter()
        .map(|(k, v)| (k.as_ref(), v.as_ref()))
        .collect();

    let for_each = inc.for_each.as_ref().map(|(b, l)| (b.as_ref(), l.as_ref()));

    let directive = parser::IncludeDirective {
        path: inc.path.as_ref(),
        with_vars,
        for_each,
    };

    // Depth tracking is handled inside resolve_include.
    crate::include::resolve_include_into(
        &directive,
        scope,
        base_dir,
        inc.inline_compiled.as_ref(),
        output,
    )
}

/// Render a compiled include directive under `no_std`.
///
/// Supports three resolution strategies (in order):
///
/// 1. **Pre-compiled inline AST** (`inline_compiled`) — e.g. from `{% tmpl %}`.
/// 2. **Inline template** — defined via `{% tmpl name %}` in the current file.
/// 3. **`Value::Tmpl` parameter** — a template passed as a typed parameter.
///
/// Filesystem-based includes are not available under `no_std` and produce
/// a descriptive error.
#[cfg(not(feature = "std"))]
fn render_include_no_std(
    inc: &CompiledInclude,
    scope: &mut Scope<'_>,
    output: &mut String,
) -> Result<(), TemplateError> {
    scope.enter_include()?;

    let result = render_include_no_std_inner(inc, scope, output);
    scope.exit_include();

    result.map_err(|e| match e {
        TemplateError::Syntax(ref syn) if syn.message.contains(&*inc.path) => e,
        TemplateError::IncludeNotFound(_) => e,
        TemplateError::Syntax(syn) => {
            TemplateError::syntax(alloc::format!("in include '{}': {}", inc.path, syn.message))
        }
        TemplateError::UndefinedVariable(name) => TemplateError::syntax(alloc::format!(
            "in include '{}': undefined variable '{name}'",
            inc.path
        )),
        other => other,
    })
}

/// Inner include resolution for `no_std`.
#[cfg(not(feature = "std"))]
fn render_include_no_std_inner(
    inc: &CompiledInclude,
    scope: &mut Scope<'_>,
    output: &mut String,
) -> Result<(), TemplateError> {
    // Build an IncludeDirective from the compiled data.
    let with_vars: alloc::vec::Vec<(&str, &str)> = inc
        .with_vars
        .iter()
        .map(|(k, v)| (k.as_ref(), v.as_ref()))
        .collect();
    let for_each = inc.for_each.as_ref().map(|(b, l)| (b.as_ref(), l.as_ref()));
    let directive = parser::IncludeDirective {
        path: inc.path.as_ref(),
        with_vars,
        for_each,
    };

    // 0. Pre-compiled inline AST.
    if let Some(compiled) = &inc.inline_compiled {
        scope.push_consts(
            (*compiled.consts).clone(),
            (*compiled.imported_consts).clone(),
        );
        let result = validate_and_render_no_std(
            &compiled.segments,
            &compiled.declarations,
            &directive,
            scope,
            output,
        );
        scope.pop_consts();
        return result;
    }

    // 1. Inline templates from the current scope.
    if let Some(compiled) = scope.get_inline_template(directive.path).cloned() {
        scope.push_consts(
            (*compiled.consts).clone(),
            (*compiled.imported_consts).clone(),
        );
        let result = validate_and_render_no_std(
            &compiled.segments,
            &compiled.declarations,
            &directive,
            scope,
            output,
        );
        scope.pop_consts();
        return result;
    }

    // 2. Value::Tmpl parameter.
    if let Ok(Value::Tmpl(tmpl)) = scope.resolve_path_str(directive.path) {
        let tmpl = tmpl.clone();
        scope.push_inline_templates(tmpl.inline_templates().clone());
        scope.push_consts((*tmpl.consts()).clone(), (*tmpl.imported_consts()).clone());

        let result = validate_and_render_no_std(
            tmpl.segments(),
            tmpl.declarations(),
            &directive,
            scope,
            output,
        );

        scope.pop_consts();
        scope.pop_inline_templates();
        return result;
    }

    // 3. No match — filesystem includes not available under no_std.
    Err(TemplateError::IncludeNotFound(alloc::format!(
        "cannot resolve '{}': filesystem includes require the `std` feature",
        directive.path
    )))
}

/// Common validation + render path for `no_std` includes.
#[cfg(not(feature = "std"))]
fn validate_and_render_no_std(
    segments: &[Segment],
    declarations: &[crate::types::VarDecl],
    directive: &parser::IncludeDirective<'_>,
    scope: &mut Scope<'_>,
    output: &mut String,
) -> Result<(), TemplateError> {
    use crate::include_core::{
        build_overrides, inject_defaults_into_layer, validate_include_contract,
        validate_include_types,
    };

    validate_include_contract(declarations, directive)?;
    let overrides = build_overrides(directive, scope)?;
    validate_include_types(declarations, &overrides, directive)?;

    scope.push_declarations(declarations);

    let res = if let Some((binding, list_expr)) = &directive.for_each {
        // Iterated include.
        let list_value = crate::parser::eval_expr(list_expr.trim(), scope)?;
        let Value::List(items) = list_value else {
            scope.pop_declarations(declarations);
            return Err(TemplateError::syntax(alloc::format!(
                "'{list_expr}' is not a list"
            )));
        };

        for (i, item) in items.iter().enumerate() {
            {
                let layer = scope.push_layer();
                layer.insert(binding.to_string(), item.clone());
                for (k, v) in &overrides {
                    layer.insert(k.clone(), v.clone());
                }
                inject_defaults_into_layer(layer, declarations, &overrides);
            }
            register_loop_meta(scope, binding, i);
            render_segments_into_no_std(segments, scope, output)?;
            scope.pop_layer();
        }
        Ok(())
    } else {
        // Simple include.
        let has_defaults = declarations.iter().any(|d| d.default_value.is_some());
        let needs_layer = !directive.with_vars.is_empty() || has_defaults;
        if needs_layer {
            let layer = scope.push_layer();
            for (k, v) in &overrides {
                layer.insert(k.clone(), v.clone());
            }
            inject_defaults_into_layer(layer, declarations, &overrides);
        }
        let r = render_segments_into_no_std(segments, scope, output);
        if needs_layer {
            scope.pop_layer();
        }
        r
    };
    scope.pop_declarations(declarations);
    res
}

// ---------------------------------------------------------------------------
// String interpolation helper
// ---------------------------------------------------------------------------

/// Render interpolated string segments (from `{{ expr }}` inside quoted strings)
/// against an **immutable** scope.
///
/// This is a lightweight renderer that handles only `Static`/`Raw` and `Expr`
/// segments — the only segment types produced by `compile_body` on a simple
/// interpolated string literal (no control flow tags allowed inside string
/// literals). Any other segment type is a compile-time logic error and will
/// produce a syntax error.
pub(crate) fn render_interpolated_str(
    segments: &[Segment],
    scope: &Scope<'_>,
) -> Result<String, TemplateError> {
    let mut output = String::new();
    for segment in segments {
        match segment {
            Segment::Static(text) | Segment::Raw(text) => output.push_str(text),
            Segment::Expr { expr, filters } => {
                eval_compiled_expr_into(expr, filters, scope, &mut output)?;
            }
            Segment::Comment(_) => {}
            _ => {
                return Err(TemplateError::syntax(
                    "control-flow tags are not allowed inside interpolated strings",
                ));
            }
        }
    }
    Ok(output)
}

// ---------------------------------------------------------------------------
// Tests for numeric comparison helpers
// ---------------------------------------------------------------------------

#[cfg(all(test, feature = "std"))]
#[path = "render_tests.rs"]
mod render_tests;