dustnet-core 0.2.0

ATP/AML protocol, parser, scanner, URI, origin, and session primitives
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
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
use crate::scanner::{Attribute, AttributeValue, Token};

use super::{
    Diagnostic, DiagnosticLevel, ParserAllocationSite, mark_allocation_failure, reject_allocation,
    try_clone_token, try_copy_string, try_diagnostic, try_push,
};

/// Maximum component definitions per document.
const MAX_DEFS: usize = 32;
/// Maximum usages per component.
const MAX_USAGES_PER: usize = 64;
/// Maximum total usages across all components.
const MAX_TOTAL_USAGES: usize = 256;
/// Hard ceiling on the number of tokens produced by expansion. Because a
/// component body may itself contain component usages, expansion is
/// potentially geometric across passes (the "billion laughs" / entity-
/// expansion attack). This bound halts expansion the moment output crosses
/// it, before the tokens are ever allocated into the final stream.
const MAX_EXPANDED_TOKENS: usize = 100_000;

/// A registered component definition.
#[derive(Debug, Clone)]
struct ComponentDef {
    name: String,
    /// Attribute names with optional defaults: `("color", Some("white"))`.
    attrs: Vec<(String, Option<String>)>,
    /// Slot names.
    slots: Vec<String>,
    /// The raw tokens of the definition body (between [def] and [/def]).
    body_tokens: Vec<Token>,
}

/// Built-in element names that components cannot shadow.
const BUILTIN_ELEMENTS: &[&str] = &[
    "page",
    "meta",
    "style",
    "box",
    "row",
    "col",
    "hr",
    "divider",
    "spacer",
    "header",
    "body",
    "footer",
    "nav",
    "text",
    "pre",
    "heading",
    "list",
    "item",
    "link",
    "input",
    "select",
    "option",
    "button",
    "form",
    "art",
    "table",
    "thead",
    "tbody",
    "tr",
    "td",
    "th",
    "animate",
    "frame",
    "element",
    "tween",
    "at",
    "text-animate",
    "live",
    "panel",
    "state",
    "pagination",
    "def",
    "slot",
    "slot-content",
    "br",
    "include",
];

/// Expand components in a token stream.
///
/// 1. First pass: extract `[def]` blocks into a registry
/// 2. Second pass: replace component usages with expanded tokens
///
/// Returns the expanded token stream and any diagnostics.
pub fn expand_components(tokens: Vec<Token>) -> (Vec<Token>, Vec<Diagnostic>, bool) {
    let mut diagnostics = Vec::new();
    let mut allocation_failed = false;

    // First pass: extract definitions
    let (defs, remaining_tokens) =
        extract_definitions(tokens, &mut diagnostics, &mut allocation_failed);

    if defs.is_empty() {
        return (remaining_tokens, diagnostics, allocation_failed);
    }

    // Second pass: expand usages
    let expanded = expand_usages(
        remaining_tokens,
        &defs,
        &mut diagnostics,
        &mut allocation_failed,
    );

    (expanded, diagnostics, allocation_failed)
}

/// Extract [def] blocks from the token stream.
/// Returns (definitions, remaining tokens with defs removed).
fn extract_definitions(
    tokens: Vec<Token>,
    diagnostics: &mut Vec<Diagnostic>,
    allocation_failed: &mut bool,
) -> (Vec<ComponentDef>, Vec<Token>) {
    let mut defs: Vec<ComponentDef> = Vec::new();
    let mut remaining = Vec::new();
    let mut i = 0;

    while let Some(current) = tokens.get(i) {
        match current {
            Token::OpenTag {
                name,
                attributes,
                self_closing,
            } if name == "def" => {
                if *self_closing {
                    try_diagnostic(
                        diagnostics,
                        DiagnosticLevel::Warning,
                        "W001",
                        format_args!("[def] cannot be self-closing"),
                        allocation_failed,
                    );
                    i += 1;
                    continue;
                }

                // Parse the def attributes
                let mut comp_name = String::new();
                let mut attrs_str = String::new();
                let mut slots_str = String::new();

                for attr in attributes {
                    match attr.name.as_str() {
                        "name" => {
                            comp_name =
                                try_copy_string(attr_value_str(&attr.value), allocation_failed)
                        }
                        "attrs" => {
                            attrs_str =
                                try_copy_string(attr_value_str(&attr.value), allocation_failed)
                        }
                        "slots" => {
                            slots_str =
                                try_copy_string(attr_value_str(&attr.value), allocation_failed)
                        }
                        _ => {}
                    }
                }

                if comp_name.is_empty() {
                    try_diagnostic(
                        diagnostics,
                        DiagnosticLevel::Error,
                        "E011",
                        format_args!("[def] requires name attribute"),
                        allocation_failed,
                    );
                    i += 1;
                    // Skip until [/def]
                    while let Some(current) = tokens.get(i) {
                        if matches!(current, Token::CloseTag { name } if name == "def") {
                            i += 1;
                            break;
                        }
                        i += 1;
                    }
                    continue;
                }

                // Check for conflicts
                if BUILTIN_ELEMENTS.contains(&comp_name.as_str()) {
                    try_diagnostic(
                        diagnostics,
                        DiagnosticLevel::Error,
                        "E037",
                        format_args!(
                            "component name \"{comp_name}\" conflicts with built-in element"
                        ),
                        allocation_failed,
                    );
                }

                // Check for duplicates
                if defs.iter().any(|d| d.name == comp_name) {
                    try_diagnostic(
                        diagnostics,
                        DiagnosticLevel::Error,
                        "E036",
                        format_args!("duplicate component name \"{comp_name}\""),
                        allocation_failed,
                    );
                }

                // Check max defs
                if defs.len() >= MAX_DEFS {
                    try_diagnostic(
                        diagnostics,
                        DiagnosticLevel::Error,
                        "E038",
                        format_args!("maximum component definitions exceeded ({MAX_DEFS})"),
                        allocation_failed,
                    );
                }

                // Parse attrs with defaults
                let attrs = parse_def_attrs(&attrs_str, allocation_failed);
                let mut slots = Vec::new();
                for slot in slots_str
                    .split(',')
                    .map(str::trim)
                    .filter(|slot| !slot.is_empty())
                {
                    let slot = try_copy_string(slot, allocation_failed);
                    try_push(&mut slots, slot, allocation_failed);
                }

                // Collect body tokens until [/def]
                i += 1; // skip the [def] tag
                let mut body_tokens = Vec::new();
                let mut def_depth = 1u32;

                while let Some(current) = tokens.get(i) {
                    match current {
                        Token::CloseTag { name } if name == "def" => {
                            def_depth -= 1;
                            if def_depth == 0 {
                                i += 1;
                                break;
                            }
                            let token = try_clone_token(current, allocation_failed);
                            try_push(&mut body_tokens, token, allocation_failed);
                        }
                        Token::OpenTag { name, .. } if name == "def" => {
                            def_depth += 1;
                            let token = try_clone_token(current, allocation_failed);
                            try_push(&mut body_tokens, token, allocation_failed);
                        }
                        _ => {
                            let token = try_clone_token(current, allocation_failed);
                            try_push(&mut body_tokens, token, allocation_failed);
                        }
                    }
                    i += 1;
                }

                // Check body doesn't reference other components or itself
                for token in &body_tokens {
                    if let Token::OpenTag { name, .. } = token {
                        if name == &comp_name {
                            try_diagnostic(
                                diagnostics,
                                DiagnosticLevel::Error,
                                "E031",
                                format_args!(
                                    "component \"{comp_name}\" references itself (recursive)"
                                ),
                                allocation_failed,
                            );
                        }
                        // An [include] in a component body would be expanded
                        // into every call site and then have $attr
                        // substitution run over it. Generated content is
                        // spliced in at serve time, after this pass, so it
                        // would land inside a region where `$` is live and a
                        // submitted `$name` could reach a substitution. Keeping
                        // the two mechanisms disjoint is cheaper to reason
                        // about than making generated content `$`-safe.
                        if name == "include" {
                            try_diagnostic(
                                diagnostics,
                                DiagnosticLevel::Error,
                                "E052",
                                format_args!(
                                    "component \"{comp_name}\" contains [include]; \
                                     server-resolved content cannot appear in a [def] body"
                                ),
                                allocation_failed,
                            );
                        }
                        // Check for references to other defined components
                        if defs.iter().any(|d| &d.name == name) {
                            try_diagnostic(
                                diagnostics,
                                DiagnosticLevel::Error,
                                "E030",
                                format_args!(
                                    "component \"{comp_name}\" references another component \"{name}\""
                                ),
                                allocation_failed,
                            );
                        }
                    }
                }

                try_push(
                    &mut defs,
                    ComponentDef {
                        name: comp_name,
                        attrs,
                        slots,
                        body_tokens,
                    },
                    allocation_failed,
                );
            }
            _ => {
                let token = try_clone_token(current, allocation_failed);
                try_push(&mut remaining, token, allocation_failed);
                i += 1;
            }
        }
    }

    (defs, remaining)
}

/// Expand component usages in a token stream.
/// Runs iteratively until no more component tags remain.
fn expand_usages(
    tokens: Vec<Token>,
    defs: &[ComponentDef],
    diagnostics: &mut Vec<Diagnostic>,
    allocation_failed: &mut bool,
) -> Vec<Token> {
    let mut tokens = tokens;
    // Iterate expansion until stable (components in slot content get expanded)
    for _ in 0..4 {
        let has_component = tokens.iter().any(|t| match t {
            Token::OpenTag { name, .. } => defs.iter().any(|d| d.name == *name),
            _ => false,
        });
        if !has_component {
            break;
        }
        // Stop before another multiplying pass if we are already at the
        // ceiling; `expand_usages_once` also halts mid-pass, this bounds
        // accumulation across the four passes.
        if tokens.len() > MAX_EXPANDED_TOKENS {
            break;
        }
        tokens = expand_usages_once(tokens, defs, diagnostics, allocation_failed);
    }
    tokens
}

fn expand_usages_once(
    tokens: Vec<Token>,
    defs: &[ComponentDef],
    diagnostics: &mut Vec<Diagnostic>,
    allocation_failed: &mut bool,
) -> Vec<Token> {
    let mut usage_counts = Vec::new();
    if usage_counts.try_reserve_exact(defs.len()).is_err() {
        mark_allocation_failure(allocation_failed);
        return Vec::new();
    }
    usage_counts.resize(defs.len(), 0usize);
    let mut total_usages = 0usize;
    let mut output = Vec::new();
    let mut i = 0;

    while let Some(current) = tokens.get(i) {
        match current {
            Token::OpenTag {
                name,
                attributes,
                self_closing,
            } => {
                // Check if this tag is a component usage
                let def_idx = defs.iter().position(|d| d.name == *name);

                if let Some(idx) = def_idx {
                    // Component usage — expand it
                    total_usages += 1;
                    // `idx` came from `defs.iter().position` and `usage_counts`
                    // is sized to `defs`, so the slot always resolves.
                    let usages = match usage_counts.get_mut(idx) {
                        Some(count) => {
                            *count += 1;
                            *count
                        }
                        None => 0,
                    };

                    if usages > MAX_USAGES_PER {
                        try_diagnostic(
                            diagnostics,
                            DiagnosticLevel::Error,
                            "E039",
                            format_args!(
                                "max usages for component \"{}\" exceeded ({MAX_USAGES_PER})",
                                name
                            ),
                            allocation_failed,
                        );
                        // Halt expansion: the document is already rejected, and
                        // continuing would let the abusive component keep growing
                        // the token stream.
                        return output;
                    }

                    if total_usages > MAX_TOTAL_USAGES {
                        try_diagnostic(
                            diagnostics,
                            DiagnosticLevel::Error,
                            "E040",
                            format_args!(
                                "max total component usages exceeded ({MAX_TOTAL_USAGES})"
                            ),
                            allocation_failed,
                        );
                        return output;
                    }

                    // Same index, same reason it resolves; taken through
                    // `get` so the expander has no panic on remote input.
                    let Some(def) = defs.get(idx) else {
                        return output;
                    };

                    // Build attribute map from usage
                    let attr_map = build_attr_map(def, attributes, diagnostics, allocation_failed);

                    // Collect slot content from children (if not self-closing)
                    let slot_content = if *self_closing {
                        i += 1;
                        std::collections::HashMap::new()
                    } else {
                        i += 1;
                        collect_slot_content(&tokens, &mut i, name, &def.slots, allocation_failed)
                    };

                    // Expand: substitute attributes and slots in body
                    let expanded = expand_body(
                        &def.body_tokens,
                        &attr_map,
                        &slot_content,
                        allocation_failed,
                    );

                    if output.len() + expanded.len() > MAX_EXPANDED_TOKENS {
                        try_diagnostic(
                            diagnostics,
                            DiagnosticLevel::Error,
                            "E041",
                            format_args!(
                                "expanded component output exceeds token limit ({MAX_EXPANDED_TOKENS})"
                            ),
                            allocation_failed,
                        );
                        return output;
                    }

                    if output.try_reserve(expanded.len()).is_err() {
                        mark_allocation_failure(allocation_failed);
                        return output;
                    }
                    output.extend(expanded);
                } else {
                    // Not a component — pass through
                    let token = try_clone_token(current, allocation_failed);
                    try_push(&mut output, token, allocation_failed);
                    i += 1;
                }
            }
            Token::CloseTag { name } => {
                // Check if closing a component tag — skip it (already consumed)
                let is_component = defs.iter().any(|d| d.name == *name);
                if !is_component {
                    let token = try_clone_token(current, allocation_failed);
                    try_push(&mut output, token, allocation_failed);
                }
                i += 1;
            }
            _ => {
                let token = try_clone_token(current, allocation_failed);
                try_push(&mut output, token, allocation_failed);
                i += 1;
            }
        }
    }

    output
}

/// Build an attribute value map from a component usage, validating against the definition.
fn build_attr_map(
    def: &ComponentDef,
    usage_attrs: &[Attribute],
    diagnostics: &mut Vec<Diagnostic>,
    allocation_failed: &mut bool,
) -> std::collections::HashMap<String, String> {
    let mut map = std::collections::HashMap::new();
    if reject_allocation(ParserAllocationSite::ComponentMap)
        || map.try_reserve(def.attrs.len()).is_err()
    {
        mark_allocation_failure(allocation_failed);
        return map;
    }

    // Start with defaults
    for (name, default) in &def.attrs {
        if let Some(val) = default {
            map.insert(
                try_copy_string(name, allocation_failed),
                try_copy_string(val, allocation_failed),
            );
        }
    }

    // Override with provided values
    for attr in usage_attrs {
        let known = def.attrs.iter().any(|(n, _)| *n == attr.name);
        if known {
            map.insert(
                try_copy_string(&attr.name, allocation_failed),
                try_copy_string(attr_value_str(&attr.value), allocation_failed),
            );
        } else {
            try_diagnostic(
                diagnostics,
                DiagnosticLevel::Error,
                "E034",
                format_args!(
                    "unknown attribute \"{}\" on component [{}]",
                    attr.name, def.name
                ),
                allocation_failed,
            );
        }
    }

    // Check required attrs (no default) are provided
    for (name, default) in &def.attrs {
        if default.is_none() && !map.contains_key(name) {
            try_diagnostic(
                diagnostics,
                DiagnosticLevel::Error,
                "E033",
                format_args!(
                    "required attribute \"{name}\" missing on component [{}]",
                    def.name
                ),
                allocation_failed,
            );
        }
    }

    map
}

/// Collect slot content from caller's children until [/component-name].
/// Returns a map of slot_name → token stream.
fn collect_slot_content(
    tokens: &[Token],
    i: &mut usize,
    close_tag: &str,
    slot_names: &[String],
    allocation_failed: &mut bool,
) -> std::collections::HashMap<String, Vec<Token>> {
    let mut slots: std::collections::HashMap<String, Vec<Token>> = std::collections::HashMap::new();
    if reject_allocation(ParserAllocationSite::SlotMap)
        || slots.try_reserve(slot_names.len()).is_err()
    {
        mark_allocation_failure(allocation_failed);
        return slots;
    }
    let mut default_content: Vec<Token> = Vec::new();

    while let Some(current) = tokens.get(*i) {
        match current {
            Token::CloseTag { name } if name == close_tag => {
                *i += 1;
                break;
            }
            // [slot-content name="..."] ... [/slot-content]
            Token::OpenTag {
                name,
                attributes,
                self_closing,
            } if name == "slot-content" => {
                let slot_name = attributes
                    .iter()
                    .find(|a| a.name == "name")
                    .map(|a| attr_value_str(&a.value))
                    .unwrap_or_default();

                *i += 1;

                if *self_closing {
                    continue;
                }

                // Collect tokens until [/slot-content]
                let mut content = Vec::new();
                let mut depth = 1u32;
                while let Some(current) = tokens.get(*i) {
                    match current {
                        Token::CloseTag { name } if name == "slot-content" => {
                            depth -= 1;
                            if depth == 0 {
                                *i += 1;
                                break;
                            }
                            let token = try_clone_token(current, allocation_failed);
                            try_push(&mut content, token, allocation_failed);
                        }
                        Token::OpenTag { name, .. } if name == "slot-content" => {
                            depth += 1;
                            let token = try_clone_token(current, allocation_failed);
                            try_push(&mut content, token, allocation_failed);
                        }
                        _ => {
                            let token = try_clone_token(current, allocation_failed);
                            try_push(&mut content, token, allocation_failed);
                        }
                    }
                    *i += 1;
                }

                let slot_name = try_copy_string(slot_name, allocation_failed);
                if slots.contains_key(slot_name.as_str()) {
                    slots.insert(slot_name, content);
                } else if reject_allocation(ParserAllocationSite::SlotMap)
                    || slots.try_reserve(1).is_err()
                {
                    mark_allocation_failure(allocation_failed);
                } else {
                    slots.insert(slot_name, content);
                }
            }
            _ => {
                // Content not wrapped in [slot-content] goes to the first/only slot
                let token = try_clone_token(current, allocation_failed);
                try_push(&mut default_content, token, allocation_failed);
                *i += 1;
            }
        }
    }

    // If there's default content and a single slot, assign it
    if !default_content.is_empty() {
        // Trim whitespace-only text tokens from the default content
        let has_meaningful = default_content.iter().any(|t| match t {
            Token::Text(s) => !s.trim().is_empty(),
            _ => true,
        });

        if has_meaningful {
            if slot_names.len() == 1 {
                let slot_name = try_copy_string(
                    slot_names.first().map_or("", String::as_str),
                    allocation_failed,
                );
                if !slots.contains_key(slot_name.as_str()) {
                    if reject_allocation(ParserAllocationSite::SlotMap)
                        || slots.try_reserve(1).is_err()
                    {
                        mark_allocation_failure(allocation_failed);
                    } else {
                        slots.insert(slot_name, default_content);
                    }
                }
            } else if !slot_names.is_empty() {
                // Multiple slots but no [slot-content] — assign to first slot
                let slot_name = try_copy_string(
                    slot_names.first().map_or("", String::as_str),
                    allocation_failed,
                );
                if !slots.contains_key(slot_name.as_str()) {
                    if reject_allocation(ParserAllocationSite::SlotMap)
                        || slots.try_reserve(1).is_err()
                    {
                        mark_allocation_failure(allocation_failed);
                    } else {
                        slots.insert(slot_name, default_content);
                    }
                }
            }
        }
    }

    slots
}

/// Expand a component body by substituting $attr and [slot] references.
fn expand_body(
    body: &[Token],
    attrs: &std::collections::HashMap<String, String>,
    slot_content: &std::collections::HashMap<String, Vec<Token>>,
    allocation_failed: &mut bool,
) -> Vec<Token> {
    let mut output = Vec::new();

    let mut i = 0;
    while let Some(current) = body.get(i) {
        match current {
            // [slot name="..."] or [slot name="..." /] — insert slot content
            Token::OpenTag {
                name,
                attributes,
                self_closing,
            } if name == "slot" => {
                let slot_name = attributes
                    .iter()
                    .find(|a| a.name == "name")
                    .map(|a| attr_value_str(&a.value))
                    .unwrap_or_default();

                if let Some(content) = slot_content.get(slot_name) {
                    for token in content {
                        let token = try_clone_token(token, allocation_failed);
                        try_push(&mut output, token, allocation_failed);
                    }
                } else if !*self_closing {
                    // Use default slot content (between [slot] and [/slot])
                    i += 1;
                    let mut default = Vec::new();
                    let mut depth = 1u32;
                    while let Some(current) = body.get(i) {
                        match current {
                            Token::CloseTag { name } if name == "slot" => {
                                depth -= 1;
                                if depth == 0 {
                                    break;
                                }
                                let token = try_clone_token(current, allocation_failed);
                                try_push(&mut default, token, allocation_failed);
                            }
                            Token::OpenTag { name, .. } if name == "slot" => {
                                depth += 1;
                                let token = try_clone_token(current, allocation_failed);
                                try_push(&mut default, token, allocation_failed);
                            }
                            _ => {
                                let token = try_clone_token(current, allocation_failed);
                                try_push(&mut default, token, allocation_failed);
                            }
                        }
                        i += 1;
                    }
                    if output.try_reserve(default.len()).is_err() {
                        mark_allocation_failure(allocation_failed);
                    } else {
                        output.extend(default);
                    }
                }
                // Skip [/slot] if not self-closing
                i += 1;
                continue;
            }

            // Close tag for [slot] — skip (already handled above)
            Token::CloseTag { name } if name == "slot" => {
                i += 1;
                continue;
            }

            // Text tokens — substitute $attr references
            Token::Text(text) => {
                let text = substitute_attrs(text, attrs, allocation_failed);
                try_push(&mut output, Token::Text(text), allocation_failed);
                i += 1;
            }

            // Open tags — substitute $attr in attribute values
            Token::OpenTag {
                name,
                attributes,
                self_closing,
            } => {
                let mut new_attrs = Vec::new();
                if new_attrs.try_reserve_exact(attributes.len()).is_err() {
                    mark_allocation_failure(allocation_failed);
                } else {
                    for attr in attributes {
                        new_attrs.push(Attribute {
                            name: try_copy_string(&attr.name, allocation_failed),
                            value: substitute_attr_value(&attr.value, attrs, allocation_failed),
                        });
                    }
                }

                let name = try_copy_string(name, allocation_failed);
                try_push(
                    &mut output,
                    Token::OpenTag {
                        name,
                        attributes: new_attrs,
                        self_closing: *self_closing,
                    },
                    allocation_failed,
                );
                i += 1;
            }

            other => {
                let token = try_clone_token(other, allocation_failed);
                try_push(&mut output, token, allocation_failed);
                i += 1;
            }
        }
    }

    output
}

/// Substitute $attr references in a text string.
/// `$$` produces a literal `$`.
fn substitute_attrs(
    text: &str,
    attrs: &std::collections::HashMap<String, String>,
    allocation_failed: &mut bool,
) -> String {
    let mut result = String::new();

    let mut cursor = 0usize;
    while let Some(relative) = text[cursor..].find('$') {
        let dollar = cursor + relative;
        if !try_append_substitution(&mut result, &text[cursor..dollar], allocation_failed) {
            return result;
        }
        let after = dollar + 1;
        if text[after..].starts_with('$') {
            if !try_append_substitution(&mut result, "$", allocation_failed) {
                return result;
            }
            cursor = after + 1;
            continue;
        }

        let mut end = after;
        for (offset, character) in text[after..].char_indices() {
            if character.is_alphanumeric() || character == '-' || character == '_' {
                end = after + offset + character.len_utf8();
            } else {
                break;
            }
        }
        let name = &text[after..end];
        if let Some(value) = attrs.get(name) {
            if !try_append_substitution(&mut result, value, allocation_failed) {
                return result;
            }
        } else {
            if !try_append_substitution(&mut result, "$", allocation_failed)
                || !try_append_substitution(&mut result, name, allocation_failed)
            {
                return result;
            }
        }
        cursor = end;
    }
    let _ = try_append_substitution(&mut result, &text[cursor..], allocation_failed);
    result
}

fn try_append_substitution(output: &mut String, value: &str, allocation_failed: &mut bool) -> bool {
    if value.is_empty() {
        return true;
    }
    if reject_allocation(ParserAllocationSite::Substitution)
        || output.try_reserve(value.len()).is_err()
    {
        mark_allocation_failure(allocation_failed);
        return false;
    }
    output.push_str(value);
    true
}

/// Substitute $attr in an attribute value.
fn substitute_attr_value(
    value: &AttributeValue,
    attrs: &std::collections::HashMap<String, String>,
    allocation_failed: &mut bool,
) -> AttributeValue {
    match value {
        AttributeValue::String(s) => {
            AttributeValue::String(substitute_attrs(s, attrs, allocation_failed))
        }
        AttributeValue::Ident(s) => {
            let substituted = substitute_attrs(s, attrs, allocation_failed);
            AttributeValue::Ident(substituted)
        }
        AttributeValue::Flag => AttributeValue::Flag,
    }
}

/// Parse the `attrs` string from [def]: "name1,name2=default,name3".
fn parse_def_attrs(attrs_str: &str, allocation_failed: &mut bool) -> Vec<(String, Option<String>)> {
    if attrs_str.is_empty() {
        return Vec::new();
    }

    let mut attrs = Vec::new();
    for attr in attrs_str
        .split(',')
        .map(str::trim)
        .filter(|attr| !attr.is_empty())
    {
        let parsed = if let Some((name, default)) = attr.split_once('=') {
            (
                try_copy_string(name.trim(), allocation_failed),
                Some(try_copy_string(default.trim(), allocation_failed)),
            )
        } else {
            (try_copy_string(attr, allocation_failed), None)
        };
        try_push(&mut attrs, parsed, allocation_failed);
    }
    attrs
}

fn attr_value_str(val: &AttributeValue) -> &str {
    match val {
        AttributeValue::String(s) | AttributeValue::Ident(s) => s,
        AttributeValue::Flag => "",
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::scanner::Scanner;

    fn scan(input: &str) -> Vec<Token> {
        Scanner::new(input.as_bytes()).unwrap().scan_all().unwrap()
    }

    fn expand(input: &str) -> (Vec<Token>, Vec<Diagnostic>) {
        let (tokens, diagnostics, resource_exhausted) = expand_components(scan(input));
        assert!(!resource_exhausted);
        (tokens, diagnostics)
    }

    fn expand_and_check(input: &str) -> Vec<Token> {
        let (tokens, diags) = expand(input);
        let errors: Vec<_> = diags
            .iter()
            .filter(|d| d.level == DiagnosticLevel::Error)
            .collect();
        assert!(errors.is_empty(), "unexpected errors: {errors:?}");
        tokens
    }

    fn has_error(diags: &[Diagnostic], code: &str) -> bool {
        diags
            .iter()
            .any(|d| d.code == code && d.level == DiagnosticLevel::Error)
    }

    fn tokens_contain_text(tokens: &[Token], needle: &str) -> bool {
        tokens.iter().any(|t| match t {
            Token::Text(s) => s.contains(needle),
            _ => false,
        })
    }

    fn tokens_contain_tag(tokens: &[Token], tag: &str) -> bool {
        tokens.iter().any(|t| match t {
            Token::OpenTag { name, .. } => name == tag,
            _ => false,
        })
    }

    // ─── Basic Expansion ─────────────────────────────────────

    #[test]
    fn basic_component() {
        let tokens = expand_and_check(
            r#"[def name="greeting" attrs="who"]
                [text]Hello $who[/text]
            [/def]
            [greeting who="world" /]"#,
        );
        assert!(tokens_contain_text(&tokens, "Hello world"));
        assert!(!tokens_contain_tag(&tokens, "def"));
        assert!(!tokens_contain_tag(&tokens, "greeting"));
    }

    #[test]
    fn component_with_defaults() {
        let tokens = expand_and_check(
            r#"[def name="badge" attrs="color=white,label"]
                [text fg=$color]$label[/text]
            [/def]
            [badge label="OK" /]"#,
        );
        // Should have fg=white (default) and text "OK"
        let has_white = tokens.iter().any(|t| match t {
            Token::OpenTag { attributes, .. } => attributes.iter().any(|a| {
                a.name == "fg" && matches!(&a.value, AttributeValue::Ident(s) if s == "white")
            }),
            _ => false,
        });
        assert!(has_white, "should use default color=white");
        assert!(tokens_contain_text(&tokens, "OK"));
    }

    #[test]
    fn component_override_default() {
        let tokens = expand_and_check(
            r#"[def name="badge" attrs="color=white"]
                [text fg=$color]X[/text]
            [/def]
            [badge color=red /]"#,
        );
        let has_red = tokens.iter().any(|t| match t {
            Token::OpenTag { attributes, .. } => attributes.iter().any(|a| {
                a.name == "fg" && matches!(&a.value, AttributeValue::Ident(s) if s == "red")
            }),
            _ => false,
        });
        assert!(has_red, "should override default with red");
    }

    #[test]
    fn dollar_escape() {
        let tokens = expand_and_check(
            r#"[def name="price" attrs="amount"]
                [text]$$USD $amount[/text]
            [/def]
            [price amount="49.99" /]"#,
        );
        assert!(tokens_contain_text(&tokens, "$USD 49.99"));
    }

    // ─── Slots ───────────────────────────────────────────────

    #[test]
    fn single_slot() {
        let tokens = expand_and_check(
            r#"[def name="wrapper" slots="content"]
                [box][slot name="content" /][/box]
            [/def]
            [wrapper][text]Inside[/text][/wrapper]"#,
        );
        assert!(tokens_contain_tag(&tokens, "box"));
        assert!(tokens_contain_text(&tokens, "Inside"));
    }

    #[test]
    fn named_slots() {
        let tokens = expand_and_check(
            r#"[def name="dialog" attrs="title" slots="body,actions"]
                [box title=$title]
                    [slot name="body" /]
                    [hr /]
                    [slot name="actions" /]
                [/box]
            [/def]
            [dialog title="Confirm"]
                [slot-content name="body"][text]Are you sure?[/text][/slot-content]
                [slot-content name="actions"][button action=submit]Yes[/button][/slot-content]
            [/dialog]"#,
        );
        assert!(tokens_contain_text(&tokens, "Are you sure?"));
        assert!(tokens_contain_tag(&tokens, "button"));
        assert!(tokens_contain_tag(&tokens, "hr"));
    }

    #[test]
    fn undeclared_slot_names_grow_only_through_fallible_reservation() {
        let tokens = expand_and_check(
            r#"[def name="card" slots="body"][slot name="body" /][/def]
            [card]
              [slot-content name="one"][text]1[/text][/slot-content]
              [slot-content name="two"][text]2[/text][/slot-content]
              [slot-content name="three"][text]3[/text][/slot-content]
              [slot-content name="body"][text]declared[/text][/slot-content]
            [/card]"#,
        );
        assert!(tokens_contain_text(&tokens, "declared"));
    }

    #[test]
    fn default_slot_content() {
        let tokens = expand_and_check(
            r#"[def name="section" slots="content,footer"]
                [slot name="content" /]
                [slot name="footer"][hr /][/slot]
            [/def]
            [section][text]Body here[/text][/section]"#,
        );
        assert!(tokens_contain_text(&tokens, "Body here"));
        // Footer should use default [hr]
        assert!(tokens_contain_tag(&tokens, "hr"));
    }

    // ─── Multiple Usages ─────────────────────────────────────

    #[test]
    fn multiple_usages() {
        let tokens = expand_and_check(
            r#"[def name="tag" attrs="label"]
                [text bold]$label[/text]
            [/def]
            [tag label="one" /]
            [tag label="two" /]
            [tag label="three" /]"#,
        );
        assert!(tokens_contain_text(&tokens, "one"));
        assert!(tokens_contain_text(&tokens, "two"));
        assert!(tokens_contain_text(&tokens, "three"));
    }

    // ─── Validation Errors ───────────────────────────────────

    #[test]
    fn recursive_component_e031() {
        let (_, diags) = expand(r#"[def name="loop"][loop /][/def]"#);
        assert!(has_error(&diags, "E031"));
    }

    #[test]
    fn component_references_another_e030() {
        let (_, diags) = expand(
            r#"[def name="inner"][text]x[/text][/def]
            [def name="outer"][inner /][/def]"#,
        );
        assert!(has_error(&diags, "E030"));
    }

    #[test]
    fn duplicate_name_e036() {
        let (_, diags) = expand(
            r#"[def name="x"][/def]
            [def name="x"][/def]"#,
        );
        assert!(has_error(&diags, "E036"));
    }

    #[test]
    fn builtin_conflict_e037() {
        let (_, diags) = expand(r#"[def name="text"][/def]"#);
        assert!(has_error(&diags, "E037"));
    }

    #[test]
    fn missing_required_attr_e033() {
        let (_, diags) = expand(
            r#"[def name="badge" attrs="color,label"][text]$label[/text][/def]
            [badge color=red /]"#,
        );
        assert!(has_error(&diags, "E033"));
    }

    #[test]
    fn unknown_attr_e034() {
        let (_, diags) = expand(
            r#"[def name="badge" attrs="color"][text]x[/text][/def]
            [badge color=red size=big /]"#,
        );
        assert!(has_error(&diags, "E034"));
    }

    // ─── No Components ───────────────────────────────────────

    #[test]
    fn no_defs_passthrough() {
        let input = "[page mode=document][text]hello[/text][/page]";
        let (tokens, diags) = expand(input);
        assert!(diags.is_empty());
        // Should be unchanged
        assert!(tokens_contain_text(&tokens, "hello"));
        assert!(tokens_contain_tag(&tokens, "page"));
    }

    // ─── Substitution Edge Cases ─────────────────────────────

    #[test]
    fn attr_in_tag_attribute() {
        let tokens = expand_and_check(
            r#"[def name="colorbox" attrs="color"]
                [box fg=$color][text]colored[/text][/box]
            [/def]
            [colorbox color=cyan /]"#,
        );
        let has_cyan = tokens.iter().any(|t| match t {
            Token::OpenTag {
                name, attributes, ..
            } => {
                name == "box"
                    && attributes.iter().any(|a| {
                        a.name == "fg"
                            && matches!(&a.value, AttributeValue::Ident(s) if s == "cyan")
                    })
            }
            _ => false,
        });
        assert!(has_cyan);
    }

    #[test]
    fn self_closing_component() {
        let tokens = expand_and_check(
            r#"[def name="dot" attrs="color"]
                [text fg=$color].[/text]
            [/def]
            [dot color=red /]"#,
        );
        assert!(tokens_contain_text(&tokens, "."));
    }

    #[test]
    fn component_with_panel() {
        let tokens = expand_and_check(
            r#"[def name="toggle" attrs="id,label" slots="content"]
                [panel id=$id state="off"]
                    [state name="off"][text]OFF[/text][/state]
                    [state name="on"][slot name="content" /][/state]
                [/panel]
            [/def]
            [toggle id="t1" label="Test"]
                [text]Enabled![/text]
            [/toggle]"#,
        );
        assert!(tokens_contain_tag(&tokens, "panel"));
        assert!(tokens_contain_tag(&tokens, "state"));
        assert!(tokens_contain_text(&tokens, "Enabled!"));
    }

    // ─── Expansion limits (DoS resistance) ───────────────────

    /// Exceeding a usage cap must *halt* expansion, not merely emit a
    /// diagnostic while continuing to grow the stream. Regression test for the
    /// entity-expansion ("billion laughs") class of DoS. Here every usage is
    /// the same component, so the per-component cap (E039) is what fires.
    #[test]
    fn excessive_usages_halts_expansion() {
        let mut input = String::from(r#"[def name="x"][text]hi[/text][/def]"#);
        for _ in 0..400 {
            input.push_str("[x /]");
        }
        let (tokens, diags) = expand(&input);

        assert!(
            has_error(&diags, "E039") || has_error(&diags, "E040"),
            "usage overflow must be reported"
        );
        // Because expansion stops at the cap rather than running all 400
        // usages, far fewer than 400 bodies reach the output.
        let expansions = tokens
            .iter()
            .filter(|t| matches!(t, Token::Text(s) if s.contains("hi")))
            .count();
        assert!(
            expansions <= MAX_USAGES_PER,
            "expansion should halt at the per-component cap, got {expansions} bodies"
        );
    }

    /// A component body may itself contain component usages, so expansion is
    /// potentially geometric across passes. The output-token ceiling must keep
    /// even a deliberately-multiplying document bounded.
    #[test]
    fn geometric_expansion_stays_bounded() {
        // Each level embeds many copies of the next, so naive expansion would
        // multiply without limit across passes.
        let input = r#"
            [def name="d"][text]x[/text][text]x[/text][text]x[/text][/def]
            [def name="c"][d /][d /][d /][d /][d /][d /][d /][d /][/def]
            [def name="b"][c /][c /][c /][c /][c /][c /][c /][c /][/def]
            [def name="a"][b /][b /][b /][b /][b /][b /][b /][b /][/def]
            [a /][a /][a /][a /][a /][a /][a /][a /]
        "#;
        let (tokens, _diags) = expand(input);
        assert!(
            tokens.len() <= MAX_EXPANDED_TOKENS + 64,
            "expansion must stay within the token ceiling, got {}",
            tokens.len()
        );
    }
}