rcal 1.0.0

OMS Critical Abstraction Layer (CAL) implementation for Rust
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
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
use std::collections::{HashMap, HashSet};
use std::fs;
use std::path::{Path, PathBuf};

use quick_xml::events::Event;
use quick_xml::reader::Reader;

fn main() {
    println!("cargo::rerun-if-env-changed=RCAL_XSD_PATH");
    println!("cargo::rerun-if-env-changed=RCAL_SCHEMA_VERSION");
    println!("cargo::rerun-if-env-changed=RCAL_OMS_COMPILER_VERSION");
    println!("cargo::rerun-if-env-changed=RCAL_CALCONFIG_PATH");
    println!("cargo::rerun-if-env-changed=RCAL_CALCONFIG_SERVICES");

    eprintln!("Starting generation step");
    if let Ok(compiler_version) = std::env::var("RCAL_OMS_COMPILER_VERSION") {
        println!("cargo::rustc-env=RCAL_OMS_COMPILER_VERSION={compiler_version}");
        eprintln!("OMS compiler version={compiler_version}");
    } else {
        let compiler_version = format!("{}/{}", env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION"));
        println!("cargo::rustc-env=RCAL_OMS_COMPILER_VERSION={compiler_version}");
        eprintln!("OMS compiler version={compiler_version}");
    }

    let out_dir = PathBuf::from(std::env::var("OUT_DIR").unwrap());
    let types_dir = out_dir.join("uci_types");
    fs::create_dir_all(&types_dir).unwrap();
    eprintln!("OUT_DIR={}", out_dir.display());

    let xsd_path = match std::env::var("RCAL_XSD_PATH").ok() {
        Some(p) => p,
        None => {
            use glob::glob;

            let pattern = "schema/UCI_MessageDefinitions_*.xsd";
            let mut files: Vec<String> = glob(pattern)
                .expect("invalid glob pattern")
                .filter_map(Result::ok)
                .map(|path| path.to_string_lossy().into_owned())
                .collect();
            files.sort_by(|a, b| b.cmp(a));
            let Some(v) = files.first() else {
                println!(
                    "cargo::error=Unable to find a valid schema files {:?}",
                    files
                );
                return;
            };
            v.to_string()
        }
    };
    eprintln!("XSD path={xsd_path}");

    let xsd_path = PathBuf::from(&xsd_path);
    let xsd_content = match fs::read_to_string(&xsd_path) {
        Ok(content) => content,
        Err(e) => {
            println!(
                "cargo::error=Cannot read RCAL_XSD_PATH={}: {e}",
                xsd_path.display()
            );
            return;
        }
    };

    println!("cargo::rerun-if-changed={}", xsd_path.display());

    let schema = parse_xsd_file(
        &xsd_path,
        &xsd_content,
        &mut std::collections::HashSet::new(),
    );

    if let Ok(schema_version) = std::env::var("RCAL_SCHEMA_VERSION") {
        println!("cargo::rustc-env=RCAL_SCHEMA_VERSION={schema_version}");
        eprintln!("Schema version={schema_version}");
    } else {
        let Some(ref v) = schema.version else {
            println!(
                "cargo::error=XSD has no version= attribute; set RCAL_SCHEMA_VERSION to override"
            );
            return;
        };
        let schema_version = format!("UCI_{v}");
        println!("cargo::rustc-env=RCAL_SCHEMA_VERSION={schema_version}");
        eprintln!("Schema version={schema_version}");
    }

    // Resolve optional calconfig-based message subset
    let subset = if let Ok(calconfig_path) = std::env::var("RCAL_CALCONFIG_PATH") {
        println!("cargo::rerun-if-changed={calconfig_path}");
        let service_filter: Option<HashSet<String>> =
            std::env::var("RCAL_CALCONFIG_SERVICES").ok().map(|s| {
                s.split(',')
                    .map(|v| v.trim().to_owned())
                    .filter(|v| !v.is_empty())
                    .collect()
            });
        match calconfig_topics(&calconfig_path, service_filter.as_ref()) {
            Ok(topics) => {
                eprintln!("Calconfig topics: {topics:?}");
                Some(compute_needed_names(&topics, &schema))
            }
            Err(e) => {
                println!("cargo::error=Failed to parse RCAL_CALCONFIG_PATH={calconfig_path}: {e}");
                return;
            }
        }
    } else {
        None
    };

    eprintln!("Generating");
    generate_types(&schema, &types_dir, subset.as_ref());
}

// ════════════════════════════════════════════════════════════════════════════
// Build-time namespace resolver
// ════════════════════════════════════════════════════════════════════════════

// Mirrors qname::NamespaceResolver — build scripts cannot use lib code.
#[derive(Debug, Clone)]
struct XsdResolver {
    default_ns: Option<String>,
    prefix_to_uri: HashMap<String, String>,
    uri_to_prefix: HashMap<String, String>,
}

impl Default for XsdResolver {
    fn default() -> Self {
        let mut r = Self {
            default_ns: None,
            prefix_to_uri: HashMap::new(),
            uri_to_prefix: HashMap::new(),
        };
        r.add_prefix("xs", "http://www.w3.org/2001/XMLSchema");
        r
    }
}

impl XsdResolver {
    fn add_prefix(&mut self, prefix: &str, uri: &str) {
        self.prefix_to_uri
            .insert(prefix.to_string(), uri.to_string());
        self.uri_to_prefix
            .insert(uri.to_string(), prefix.to_string());
    }

    fn resolve_pair(&self, name: &str) -> (Option<String>, String) {
        if let Some(colon) = name.find(':') {
            let prefix = &name[..colon];
            let local = name[colon + 1..].to_string();
            let ns = self.prefix_to_uri.get(prefix).cloned();
            (ns, local)
        } else {
            (self.default_ns.clone(), name.to_string())
        }
    }

    fn format_display(&self, ns: Option<&str>, local: &str) -> String {
        match ns {
            None => local.to_string(),
            Some(n) if self.default_ns.as_deref() == Some(n) => local.to_string(),
            Some(n) => match self.uri_to_prefix.get(n) {
                Some(p) => format!("{p}:{local}"),
                None => format!("{{{n}}}{local}"),
            },
        }
    }
}

// ════════════════════════════════════════════════════════════════════════════
// XSD data model
// ════════════════════════════════════════════════════════════════════════════

#[derive(Debug, Default)]
struct Schema {
    version: Option<String>,
    namespace: Option<String>,
    resolver: XsdResolver,
    simple_types: Vec<SimpleType>,
    complex_types: Vec<ComplexType>,
    elements: Vec<Element>,
}

#[derive(Debug)]
struct SimpleType {
    name: String,
    kind: SimpleTypeKind,
}

/// XSD restriction facets for string and numeric types.
#[derive(Debug, Default, Clone)]
struct Facets {
    length: Option<u32>,
    min_length: Option<u32>,
    max_length: Option<u32>,
    pattern: Option<String>,
    min_inclusive: Option<String>,
    max_inclusive: Option<String>,
}

impl Facets {
    fn is_empty(&self) -> bool {
        self.length.is_none()
            && self.min_length.is_none()
            && self.max_length.is_none()
            && self.pattern.is_none()
            && self.min_inclusive.is_none()
            && self.max_inclusive.is_none()
    }
}

#[derive(Debug)]
enum SimpleTypeKind {
    Enum(Vec<String>),
    Restriction { base: String, facets: Facets },
}

#[derive(Debug)]
struct ComplexType {
    name: String,
    abstract_: bool,
    extension_base: Option<String>,
    fields: Vec<Field>,
    is_choice: bool,
}

/// Maximum occurrences constraint on an XSD element.
#[derive(Debug, PartialEq, Clone)]
enum MaxOccurs {
    Bounded(u32),
    Unbounded,
}

#[derive(Debug)]
struct Field {
    name: String,
    type_: String,
    min_occurs: u32,
    max_occurs: MaxOccurs,
}

impl Field {
    /// True when the field is represented as `Option<T>` (minOccurs=0, maxOccurs=1).
    fn is_optional(&self) -> bool {
        self.min_occurs == 0 && self.max_occurs == MaxOccurs::Bounded(1)
    }

    /// True when the field is represented as `Vec<T>` (maxOccurs > 1 or unbounded).
    fn is_vec(&self) -> bool {
        matches!(self.max_occurs, MaxOccurs::Unbounded)
            || matches!(self.max_occurs, MaxOccurs::Bounded(n) if n > 1)
    }
}

#[derive(Debug)]
struct Element {
    name: String,
    type_: String,
}

// ════════════════════════════════════════════════════════════════════════════
// XSD parser
// ════════════════════════════════════════════════════════════════════════════

fn parse_xsd_file(
    xsd_path: &Path,
    content: &str,
    seen: &mut std::collections::HashSet<PathBuf>,
) -> Schema {
    let canonical = xsd_path
        .canonicalize()
        .unwrap_or_else(|_| xsd_path.to_path_buf());
    seen.insert(canonical);
    let base_dir = xsd_path.parent().unwrap_or(Path::new("."));

    let mut reader = Reader::from_str(content);
    reader.config_mut().trim_text(true);

    let mut schema = Schema::default();
    let mut current_simple: Option<SimpleType> = None;
    let mut current_complex: Option<ComplexType> = None;
    let mut in_restriction = false;
    let mut in_choice_depth: u32 = 0;
    let mut restriction_base: Option<String> = None;
    let mut current_facets = Facets::default();

    loop {
        match reader.read_event() {
            Ok(Event::Start(ref e) | Event::Empty(ref e)) => {
                let local = local_name(e.name().as_ref());
                match local.as_str() {
                    "schema" => {
                        schema.version = attr(e, "version");
                        schema.namespace = attr(e, "targetNamespace");
                        if let Some(ref ns) = schema.namespace {
                            schema.resolver.default_ns = Some(ns.clone());
                        }
                        for a in e.attributes().filter_map(|a| a.ok()) {
                            let key = std::str::from_utf8(a.key.as_ref()).unwrap_or("");
                            if let Some(prefix) = key.strip_prefix("xmlns:") {
                                let uri = std::str::from_utf8(a.value.as_ref())
                                    .unwrap_or("")
                                    .to_string();
                                schema.resolver.add_prefix(prefix, &uri);
                            }
                        }
                    }
                    "include" => {
                        if let Some(loc) = attr(e, "schemaLocation") {
                            let inc_path = base_dir.join(&loc);
                            let canonical_inc =
                                inc_path.canonicalize().unwrap_or_else(|_| inc_path.clone());
                            if seen.contains(&canonical_inc) {
                                continue;
                            }
                            let inc_content = fs::read_to_string(&inc_path).unwrap_or_else(|e| {
                                panic!(
                                    "xs:include '{}' not found (included from '{}'): {e}",
                                    inc_path.display(),
                                    xsd_path.display()
                                )
                            });
                            println!("cargo::rerun-if-changed={}", inc_path.display());
                            let inc_schema = parse_xsd_file(&inc_path, &inc_content, seen);
                            for (p, u) in &inc_schema.resolver.prefix_to_uri {
                                if !schema.resolver.prefix_to_uri.contains_key(p) {
                                    schema.resolver.add_prefix(p, u);
                                }
                            }
                            schema.simple_types.extend(inc_schema.simple_types);
                            schema.complex_types.extend(inc_schema.complex_types);
                            schema.elements.extend(inc_schema.elements);
                        }
                    }
                    "simpleType" => {
                        if let Some(name) = attr(e, "name") {
                            current_simple = Some(SimpleType {
                                name,
                                kind: SimpleTypeKind::Restriction {
                                    base: "xs:string".into(),
                                    facets: Facets::default(),
                                },
                            });
                        }
                    }
                    "complexType" => {
                        if let Some(name) = attr(e, "name") {
                            let abstract_ =
                                attr(e, "abstract").map(|v| v == "true").unwrap_or(false);
                            current_complex = Some(ComplexType {
                                name,
                                abstract_,
                                extension_base: None,
                                fields: vec![],
                                is_choice: false,
                            });
                        }
                    }
                    "extension" => {
                        if let Some(ct) = current_complex.as_mut() {
                            ct.extension_base = attr(e, "base");
                        }
                    }
                    "restriction" => {
                        in_restriction = true;
                        restriction_base = attr(e, "base");
                        current_facets = Facets::default();
                    }
                    "enumeration" => {
                        if let (Some(st), Some(val)) = (current_simple.as_mut(), attr(e, "value")) {
                            if let SimpleTypeKind::Enum(ref mut vals) = st.kind {
                                vals.push(val);
                            } else {
                                st.kind = SimpleTypeKind::Enum(vec![val]);
                            }
                        }
                    }
                    // XSD restriction facets
                    "length" => {
                        if in_restriction {
                            current_facets.length = attr(e, "value").and_then(|v| v.parse().ok());
                        }
                    }
                    "minLength" => {
                        if in_restriction {
                            current_facets.min_length =
                                attr(e, "value").and_then(|v| v.parse().ok());
                        }
                    }
                    "maxLength" => {
                        if in_restriction {
                            current_facets.max_length =
                                attr(e, "value").and_then(|v| v.parse().ok());
                        }
                    }
                    "pattern" => {
                        if in_restriction {
                            current_facets.pattern = attr(e, "value");
                        }
                    }
                    "minInclusive" => {
                        if in_restriction {
                            current_facets.min_inclusive = attr(e, "value");
                        }
                    }
                    "maxInclusive" => {
                        if in_restriction {
                            current_facets.max_inclusive = attr(e, "value");
                        }
                    }
                    "choice" => {
                        in_choice_depth += 1;
                        if let Some(ct) = current_complex.as_mut() {
                            ct.is_choice = true;
                        }
                    }
                    "element" => {
                        let min_occurs: u32 = attr(e, "minOccurs")
                            .and_then(|v| v.parse().ok())
                            .unwrap_or(1);
                        let max_occurs = match attr(e, "maxOccurs").as_deref() {
                            Some("unbounded") => MaxOccurs::Unbounded,
                            Some(n) => MaxOccurs::Bounded(n.parse().unwrap_or(1)),
                            None => MaxOccurs::Bounded(1),
                        };

                        if current_complex.is_none() && current_simple.is_none() {
                            if let (Some(name), Some(type_)) = (attr(e, "name"), attr(e, "type")) {
                                schema.elements.push(Element { name, type_ });
                            }
                        } else if let Some(ct) = current_complex.as_mut()
                            && let (Some(name), Some(type_)) = (attr(e, "name"), attr(e, "type"))
                        {
                            ct.fields.push(Field {
                                name,
                                type_,
                                min_occurs,
                                max_occurs,
                            });
                        }
                    }
                    _ => {}
                }
            }
            Ok(Event::End(ref e)) => {
                let local = local_name(e.name().as_ref());
                match local.as_str() {
                    "simpleType" => {
                        if let Some(mut st) = current_simple.take() {
                            if in_restriction
                                && let SimpleTypeKind::Restriction {
                                    ref mut base,
                                    ref mut facets,
                                } = st.kind
                            {
                                if let Some(b) = restriction_base.take() {
                                    *base = b;
                                }
                                *facets = current_facets.clone();
                            }
                            schema.simple_types.push(st);
                        }
                        in_restriction = false;
                        restriction_base = None;
                        current_facets = Facets::default();
                    }
                    "complexType" => {
                        if let Some(ct) = current_complex.take() {
                            schema.complex_types.push(ct);
                        }
                    }
                    "choice" if in_choice_depth > 0 => {
                        in_choice_depth = in_choice_depth.saturating_sub(1);
                    }
                    _ => {}
                }
            }
            Ok(Event::Eof) => break,
            Err(e) => panic!("XSD parse error in '{}': {e}", xsd_path.display()),
            _ => {}
        }
    }

    schema
}

fn local_name(name: &[u8]) -> String {
    let s = std::str::from_utf8(name).unwrap_or("");
    s.rfind(':').map(|i| &s[i + 1..]).unwrap_or(s).to_string()
}

fn attr(e: &quick_xml::events::BytesStart, key: &str) -> Option<String> {
    e.attributes()
        .filter_map(|a| a.ok())
        .find(|a| local_name(a.key.as_ref()) == key)
        // unescape_value decodes XML entity refs (e.g. &#x20; → ' ') so regex patterns are valid.
        .and_then(|a| a.unescape_value().ok().map(|s| s.into_owned()))
}

// ════════════════════════════════════════════════════════════════════════════
// Code generator
// ════════════════════════════════════════════════════════════════════════════

fn generate_types(schema: &Schema, out_dir: &Path, subset: Option<&HashSet<String>>) {
    let resolver = &schema.resolver;
    let mut mod_entries: Vec<String> = vec![];
    let mut written_files: HashSet<String> = HashSet::new();

    // Build a lookup: simple-type local name → fully-qualified Rust type path.
    let simple_type_map: HashMap<&str, String> = schema
        .simple_types
        .iter()
        .map(|st| {
            let rust_ty = match &st.kind {
                SimpleTypeKind::Enum(_) => {
                    format!("crate::uci::types::{}", pascal(&st.name))
                }
                SimpleTypeKind::Restriction { base, .. } => {
                    let (ns, local) = resolver.resolve_pair(base);
                    xsd_to_rust(ns.as_deref(), &local)
                }
            };
            let rust_ty = if st.name == "UniversallyUniqueIdentifierType" {
                "crate::uci::base::UUID".to_string()
            } else {
                rust_ty
            };
            (st.name.as_str(), rust_ty)
        })
        .collect();

    // Names of all enum simple types (for generating enum checks in is_valid).
    let enum_names: HashSet<&str> = schema
        .simple_types
        .iter()
        .filter(|st| matches!(st.kind, SimpleTypeKind::Enum(_)))
        .map(|st| st.name.as_str())
        .collect();

    // Map: type local name → facets (for string/double constraint checks).
    let facets_map: HashMap<&str, &Facets> = schema
        .simple_types
        .iter()
        .filter_map(|st| match &st.kind {
            SimpleTypeKind::Restriction { facets, .. } if !facets.is_empty() => {
                Some((st.name.as_str(), facets))
            }
            _ => None,
        })
        .collect();

    // Generate simple types
    let mut simple_count = 0;
    eprintln!("Generating simple types");
    for st in &schema.simple_types {
        // UniversallyUniqueIdentifierType maps directly to crate::uci::base::UUID — no alias needed.
        if st.name == "UniversallyUniqueIdentifierType" {
            continue;
        }
        if let Some(needed) = subset
            && !needed.contains(&st.name)
        {
            continue;
        }
        let file_name = format!("{}.rs", snake(&st.name));
        let code = match &st.kind {
            SimpleTypeKind::Enum(vals) => gen_enum(&st.name, vals),
            SimpleTypeKind::Restriction { base, facets: _ } => {
                gen_type_alias(&st.name, base, resolver)
            }
        };
        fs::write(out_dir.join(&file_name), code).unwrap();
        written_files.insert(file_name.clone());
        let mod_name = snake(&st.name);
        mod_entries.push(format!(
            "#[doc(hidden)]\npub mod {mod_name};\n#[doc(inline)]\npub use {mod_name}::*;"
        ));
        simple_count += 1;
    }

    // Invert: complex-type local name → element name
    let type_to_element: HashMap<&str, &str> = schema
        .elements
        .iter()
        .map(|el| {
            let colon = el.type_.find(':');
            let local = colon.map(|i| &el.type_[i + 1..]).unwrap_or(&el.type_);
            (local, el.name.as_str())
        })
        .collect();

    // Build complex-type lookup for inheritance delegation
    let complex_type_map: HashMap<&str, &ComplexType> = schema
        .complex_types
        .iter()
        .map(|ct| (ct.name.as_str(), ct))
        .collect();

    // Names of all xs:choice complex types — used to suppress trait generation and dyn dispatch.
    let choice_type_names: HashSet<&str> = schema
        .complex_types
        .iter()
        .filter(|ct| ct.is_choice)
        .map(|ct| ct.name.as_str())
        .collect();

    // Pure-extension complex types (no own fields) are emitted as type aliases.
    let mut simple_type_map = simple_type_map;
    for ct in &schema.complex_types {
        if ct.fields.is_empty() && ct.extension_base.is_some() {
            let pascal_name = pascal(&ct.name);
            simple_type_map
                .entry(ct.name.as_str())
                .or_insert_with(|| format!("crate::uci::types::{pascal_name}"));
        }
    }

    // Choice complex types: only those with validatable fields get the `_` suffix.
    // The suffix signals to is_validatable_type/dyn_type that is_valid_at is meaningful.
    for ct in &schema.complex_types {
        if ct.is_choice {
            let any_validatable = ct.fields.iter().any(|f| {
                let (type_ns, type_local) = resolver.resolve_pair(&f.type_);
                let rust_type = if let Some(resolved) = simple_type_map.get(type_local.as_str()) {
                    resolved.clone()
                } else {
                    xsd_to_rust_concrete(type_ns.as_deref(), &type_local)
                };
                is_validatable_type(&rust_type, &enum_names, &type_local)
            });
            let pascal_name = pascal(&ct.name);
            let suffix = if any_validatable { "_" } else { "" };
            simple_type_map.insert(
                ct.name.as_str(),
                format!("crate::uci::types::{pascal_name}{suffix}"),
            );
        }
    }

    // Generate complex types
    let mut complex_count = 0;
    eprintln!("Generating complex types");
    for ct in &schema.complex_types {
        if let Some(needed) = subset
            && !needed.contains(&ct.name)
        {
            continue;
        }
        let file_name = format!("{}.rs", snake(&ct.name));
        let code = gen_struct(
            ct,
            &simple_type_map,
            &type_to_element,
            resolver,
            &complex_type_map,
            &enum_names,
            &facets_map,
            &choice_type_names,
        );
        fs::write(out_dir.join(&file_name), code).unwrap();
        written_files.insert(file_name.clone());
        let mod_name = snake(&ct.name);
        mod_entries.push(format!(
            "#[doc(hidden)]\n#[allow(missing_docs)]\npub mod {mod_name};\n#[doc(inline)]\npub use {mod_name}::*;"
        ));
        complex_count += 1;
    }

    // Generate element newtype wrappers
    let mut element_count = 0;
    eprintln!("Generating elements");
    for el in &schema.elements {
        if let Some(needed) = subset
            && !needed.contains(&el.name)
        {
            continue;
        }
        let (type_ns, type_local) = resolver.resolve_pair(&el.type_);
        let type_pascal = pascal(&type_local);
        let el_module = snake(&el.name);
        let el_pascal = pascal(&el.name);
        let type_path_concrete = format!("crate::uci::types::{type_pascal}_");
        let display = resolver.format_display(type_ns.as_deref(), &type_local);
        let ns_arg = match &type_ns {
            Some(ns) => format!("Some(\"{ns}\")"),
            None => "None".to_string(),
        };
        // Build xmlns field declarations and value initializers for the __Ns<'a> serialize wrapper.
        let mut ns_struct_fields = String::new();
        let mut ns_struct_values = String::new();
        if let Some(ref default_ns) = resolver.default_ns {
            ns_struct_fields.push_str(
                "            #[serde(rename = \"@xmlns\")]\n            xmlns: &'static str,\n",
            );
            ns_struct_values.push_str(&format!("            xmlns: \"{default_ns}\",\n"));
        }
        ns_struct_fields.push_str(
            "            #[serde(rename = \"@xmlns:xsi\")]\n            xmlns_xsi: &'static str,\n",
        );
        ns_struct_values
            .push_str("            xmlns_xsi: \"http://www.w3.org/2001/XMLSchema-instance\",\n");
        let mut sorted_prefixes: Vec<_> = resolver.prefix_to_uri.iter().collect();
        sorted_prefixes.sort_by_key(|(k, _)| k.as_str());
        for (prefix, uri) in &sorted_prefixes {
            let field_name = format!("xmlns_{}", prefix.replace(['-', ':'], "_"));
            ns_struct_fields.push_str(&format!(
                "            #[serde(rename = \"@xmlns:{prefix}\")]\n            {field_name}: &'static str,\n"
            ));
            ns_struct_values.push_str(&format!("            {field_name}: \"{uri}\",\n"));
        }
        let code = format!(
            "// @generated — do not edit.\n#![allow(non_camel_case_types)]\n\n\
             /// XSD element `{el_name}`. Wraps [`{type_pascal}_`]({type_path_concrete}).\n\
             #[derive(Debug, Clone, serde::Deserialize)]\n\
             #[serde(transparent)]\n\
             pub struct {el_pascal}_(pub {type_path_concrete});\n\n\
             impl serde::Serialize for {el_pascal}_ {{\n\
             \x20   fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {{\n\
             \x20       #[derive(serde::Serialize)]\n\
             \x20       struct __Ns<'a> {{\n\
             {ns_struct_fields}\
             \x20           #[serde(flatten)]\n\
             \x20           inner: &'a {type_path_concrete},\n\
             \x20       }}\n\
             \x20       __Ns {{\n\
             {ns_struct_values}\
             \x20           inner: &self.0,\n\
             \x20       }}.serialize(serializer)\n\
             \x20   }}\n\
             }}\n\n\
             impl std::ops::Deref for {el_pascal}_ {{\n\
             \x20   type Target = {type_path_concrete};\n\
             \x20   fn deref(&self) -> &Self::Target {{ &self.0 }}\n\
             }}\n\n\
             impl std::ops::DerefMut for {el_pascal}_ {{\n\
             \x20   fn deref_mut(&mut self) -> &mut Self::Target {{ &mut self.0 }}\n\
             }}\n\n\
             impl crate::uci::CalMessage for {el_pascal}_ {{\n\
             \x20   fn message_type_name() -> crate::QName {{\n\
             \x20       crate::QName::with_display({ns_arg}, \"{type_local}\", \"{display}\")\n\
             \x20   }}\n\
             \x20   fn cal_create() -> Self {{ Self({type_path_concrete}::_cal_create()) }}\n\
             \x20   fn is_valid(&self) -> Result<(), crate::uci::ValidationError> {{\n\
             \x20       self.0.is_valid_at(\"{el_name}\")\n\
             \x20   }}\n\
             \x20   fn as_message_type_mut(&mut self) -> Option<&mut dyn crate::uci::types::MessageType> {{\n\
             \x20       Some(&mut self.0)\n\
             \x20   }}\n\
             }}\n",
            el_name = el.name,
            ns_struct_fields = ns_struct_fields,
            ns_struct_values = ns_struct_values,
        );
        let el_file = format!("{el_module}.rs");
        fs::write(out_dir.join(&el_file), code).unwrap();
        written_files.insert(el_file);
        mod_entries.push(format!(
            "#[doc(hidden)]\n#[allow(missing_docs)]\npub mod {el_module};\n#[doc(inline)]\npub use {el_module}::*;"
        ));
        element_count += 1;
    }

    // Remove stale .rs files from a previous broader generation
    if subset.is_some()
        && let Ok(entries) = fs::read_dir(out_dir)
    {
        for entry in entries.filter_map(|e| e.ok()) {
            let name = entry.file_name();
            let name_str = name.to_string_lossy();
            if name_str.ends_with(".rs")
                && name_str != "mod.rs"
                && !written_files.contains(name_str.as_ref())
            {
                let _ = fs::remove_file(entry.path());
            }
        }
    }

    // Write mod.rs
    let mod_content = format!(
        "// @generated — do not edit.\n\n{}\n",
        mod_entries.join("\n")
    );
    fs::write(out_dir.join("mod.rs"), mod_content).unwrap();
    eprintln!(
        "Finished generating types: {element_count} elements, {simple_count} simple types, and {complex_count} complex typtes"
    );
}

// ════════════════════════════════════════════════════════════════════════════
// Calconfig-based message subsetting
// ════════════════════════════════════════════════════════════════════════════

/// Parse a calconfig.toml and return the set of message type names referenced by topics.
///
/// If `service_filter` is Some, only topics from those service IDs are included.
fn calconfig_topics(
    path: &str,
    service_filter: Option<&HashSet<String>>,
) -> Result<HashSet<String>, String> {
    let content = fs::read_to_string(path).map_err(|e| format!("cannot read {path}: {e}"))?;
    let mut topics: HashSet<String> = HashSet::new();

    #[derive(Default)]
    enum Section {
        #[default]
        Other,
        Service,
        ServiceTopic,
    }

    let mut section = Section::default();
    let mut current_service_id = String::new();
    let mut current_topic_id = String::new();
    let mut current_topic_type: Option<String> = None;
    let mut in_selected_service = false;

    let flush_topic =
        |id: &str, type_: Option<&str>, in_svc: bool, topics: &mut HashSet<String>| {
            if in_svc && !id.is_empty() {
                topics.insert(type_.unwrap_or(id).to_owned());
            }
        };

    for line in content.lines() {
        let trimmed = line.trim();

        if trimmed == "[[service]]" {
            flush_topic(
                &current_topic_id,
                current_topic_type.as_deref(),
                in_selected_service,
                &mut topics,
            );
            current_topic_id.clear();
            current_topic_type = None;
            current_service_id.clear();
            in_selected_service = false;
            section = Section::Service;
            continue;
        }

        if trimmed == "[[service.topic]]" {
            flush_topic(
                &current_topic_id,
                current_topic_type.as_deref(),
                in_selected_service,
                &mut topics,
            );
            current_topic_id.clear();
            current_topic_type = None;
            section = Section::ServiceTopic;
            continue;
        }

        // Any other array-of-tables header resets section
        if trimmed.starts_with("[[") {
            flush_topic(
                &current_topic_id,
                current_topic_type.as_deref(),
                in_selected_service,
                &mut topics,
            );
            current_topic_id.clear();
            current_topic_type = None;
            section = Section::Other;
            continue;
        }

        match section {
            Section::Service => {
                if let Some(val) = parse_toml_string_value(trimmed, "id") {
                    current_service_id = val;
                    in_selected_service = service_filter
                        .map(|f| f.contains(&current_service_id))
                        .unwrap_or(true);
                }
            }
            Section::ServiceTopic => {
                if let Some(val) = parse_toml_string_value(trimmed, "id") {
                    current_topic_id = val;
                } else if let Some(val) = parse_toml_string_value(trimmed, "type") {
                    current_topic_type = Some(val);
                }
            }
            Section::Other => {}
        }
    }

    flush_topic(
        &current_topic_id,
        current_topic_type.as_deref(),
        in_selected_service,
        &mut topics,
    );

    Ok(topics)
}

/// Extract a `key = "value"` string from a TOML line.
fn parse_toml_string_value(line: &str, key: &str) -> Option<String> {
    let rest = line.strip_prefix(key)?.trim_start();
    let rest = rest.strip_prefix('=')?.trim_start();
    let rest = rest.strip_prefix('"')?;
    let end = rest.find('"')?;
    Some(rest[..end].to_owned())
}

/// Resolve all transitive XSD dependencies for the given message element names.
///
/// Returns a HashSet of all element and type names that must be generated.
fn compute_needed_names(message_types: &HashSet<String>, schema: &Schema) -> HashSet<String> {
    // Build a map: name -> set of referenced names
    let mut refs: HashMap<String, HashSet<String>> = HashMap::new();

    for el in &schema.elements {
        let local = el
            .type_
            .rfind(':')
            .map(|i| &el.type_[i + 1..])
            .unwrap_or(&el.type_);
        let mut dep_set = HashSet::new();
        if !el.type_.starts_with("xs:") {
            dep_set.insert(local.to_owned());
        }
        refs.insert(el.name.clone(), dep_set);
    }

    for ct in &schema.complex_types {
        let mut dep_set: HashSet<String> = HashSet::new();
        if let Some(base) = &ct.extension_base {
            let local = base.rfind(':').map(|i| &base[i + 1..]).unwrap_or(base);
            if !base.starts_with("xs:") {
                dep_set.insert(local.to_owned());
            }
        }
        for f in &ct.fields {
            let local = f
                .type_
                .rfind(':')
                .map(|i| &f.type_[i + 1..])
                .unwrap_or(&f.type_);
            if !f.type_.starts_with("xs:") {
                dep_set.insert(local.to_owned());
            }
        }
        refs.insert(ct.name.clone(), dep_set);
    }

    for st in &schema.simple_types {
        let dep_set = match &st.kind {
            SimpleTypeKind::Restriction { base, .. } if !base.starts_with("xs:") => {
                let local = base.rfind(':').map(|i| &base[i + 1..]).unwrap_or(base);
                let mut s = HashSet::new();
                s.insert(local.to_owned());
                s
            }
            _ => HashSet::new(),
        };
        refs.insert(st.name.clone(), dep_set);
    }

    // BFS from message_types
    let mut needed: HashSet<String> = HashSet::new();
    let mut queue: Vec<String> = message_types.iter().cloned().collect();
    while let Some(name) = queue.pop() {
        if needed.contains(&name) {
            continue;
        }
        needed.insert(name.clone());
        if let Some(deps) = refs.get(&name) {
            for dep in deps {
                if !needed.contains(dep) {
                    queue.push(dep.clone());
                }
            }
        }
    }
    needed
}

fn gen_enum(name: &str, vals: &[String]) -> String {
    let pascal_name = pascal(name);

    let mut seen: std::collections::HashMap<String, usize> = std::collections::HashMap::new();
    let variants: Vec<(String, &str)> = vals
        .iter()
        .map(|v| {
            let base = enum_variant(v);
            let count = seen.entry(base.clone()).or_insert(0);
            *count += 1;
            let variant = if *count == 1 {
                base
            } else {
                format!("{base}{}", *count)
            };
            (variant, v.as_str())
        })
        .collect();

    let mut match_arms: String =
        format!("                    \"enumNotSet\" => Ok({pascal_name}::EnumNotSet),\n");
    match_arms.push_str(
        &variants
            .iter()
            .map(|(variant, orig)| {
                format!("                    \"{orig}\" => Ok({pascal_name}::{variant}),\n")
            })
            .collect::<String>(),
    );
    let mut variant_names: Vec<String> = vec!["\"enumNotSet\"".to_string()];
    variant_names.extend(variants.iter().map(|(_, orig)| format!("\"{orig}\"")));
    let variant_names_str = variant_names.join(", ");

    let mut out = String::new();
    out.push_str("// @generated — do not edit.\n#![allow(non_camel_case_types, non_snake_case, clippy::approx_constant, clippy::excessive_precision, clippy::wrong_self_convention)]\n\n");
    out.push_str(&format!("/// XSD simpleType `{name}`.\n"));
    out.push_str("#[derive(Debug, Clone, Default, PartialEq, Eq, serde::Serialize)]\n");
    out.push_str(&format!("#[serde(rename = \"{pascal_name}\")]\n"));
    out.push_str(&format!("pub enum {pascal_name} {{\n"));
    out.push_str("    /// Unset/default sentinel.\n    #[default]\n    #[serde(rename = \"enumNotSet\")]\n    EnumNotSet,\n");
    for (variant, orig) in &variants {
        out.push_str(&format!(
            "    /// `{orig}` variant.\n    #[serde(rename = \"{orig}\")]\n    {variant},\n"
        ));
    }
    out.push_str("}\n\n");
    // Custom Deserialize impl
    out.push_str(&format!(
        "impl<'de> serde::Deserialize<'de> for {pascal_name} {{\n\
         \x20   fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {{\n\
         \x20       fn from_str<E: serde::de::Error>(v: &str) -> Result<{pascal_name}, E> {{\n\
         \x20           match v {{\n\
         {match_arms}\
         \x20               other => Err(E::unknown_variant(other, &[{variant_names_str}])),\n\
         \x20           }}\n\
         \x20       }}\n\
         \x20       struct Visitor_;\n\
         \x20       impl<'de> serde::de::Visitor<'de> for Visitor_ {{\n\
         \x20           type Value = {pascal_name};\n\
         \x20           fn expecting(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {{\n\
         \x20               write!(f, \"a {pascal_name} variant\")\n\
         \x20           }}\n\
         \x20           fn visit_str<E: serde::de::Error>(self, v: &str) -> Result<Self::Value, E> {{\n\
         \x20               from_str(v)\n\
         \x20           }}\n\
         \x20           fn visit_map<A: serde::de::MapAccess<'de>>(self, mut map: A) -> Result<Self::Value, A::Error> {{\n\
         \x20               let mut result = None;\n\
         \x20               while let Some(key) = map.next_key::<std::borrow::Cow<str>>()? {{\n\
         \x20                   if key == \"$text\" {{\n\
         \x20                       let val: std::borrow::Cow<str> = map.next_value()?;\n\
         \x20                       result = Some(from_str::<A::Error>(&val)?);\n\
         \x20                   }} else {{\n\
         \x20                       let _: serde::de::IgnoredAny = map.next_value()?;\n\
         \x20                   }}\n\
         \x20               }}\n\
         \x20               result.ok_or_else(|| serde::de::Error::missing_field(\"$text\"))\n\
         \x20           }}\n\
         \x20       }}\n\
         \x20       deserializer.deserialize_any(Visitor_)\n\
         \x20   }}\n\
         }}\n\n"
    ));
    // is_valid
    out.push_str(&format!(
        "impl {pascal_name} {{\n\
         \x20   /// Returns `Err` if this enum is still at the default `EnumNotSet` sentinel.\n\
         \x20   pub fn is_valid_at(&self, path: &str) -> Result<(), crate::uci::ValidationError> {{\n\
         \x20       if matches!(self, {pascal_name}::EnumNotSet) {{\n\
         \x20           return Err(crate::uci::ValidationError {{\n\
         \x20               path: path.to_owned(),\n\
         \x20               reason: \"enum not set\".to_owned(),\n\
         \x20           }});\n\
         \x20       }}\n\
         \x20       Ok(())\n\
         \x20   }}\n\
         }}\n"
    ));
    out
}

fn gen_type_alias(name: &str, base: &str, resolver: &XsdResolver) -> String {
    let pascal_name = pascal(name);
    let (ns, local) = resolver.resolve_pair(base);
    let rust_type = xsd_to_rust(ns.as_deref(), &local);
    format!(
        "// @generated — do not edit.\n#![allow(non_camel_case_types)]\n\n/// XSD simpleType `{name}`.\npub type {pascal_name} = {rust_type};\n"
    )
}

fn resolve_base_rust_type(
    base: &str,
    simple_map: &HashMap<&str, String>,
    resolver: &XsdResolver,
) -> String {
    let (ns, local) = resolver.resolve_pair(base);
    if let Some(resolved) = simple_map.get(local.as_str()) {
        resolved.clone()
    } else {
        xsd_to_rust_concrete(ns.as_deref(), &local)
    }
}

fn field_rust_type(
    f: &Field,
    simple_map: &HashMap<&str, String>,
    resolver: &XsdResolver,
) -> String {
    let (type_ns, type_local) = resolver.resolve_pair(&f.type_);
    let base = if let Some(resolved) = simple_map.get(type_local.as_str()) {
        resolved.clone()
    } else {
        xsd_to_rust_concrete(type_ns.as_deref(), &type_local)
    };
    if f.is_vec() {
        format!("crate::uci::base::BoundedList<{base}>")
    } else if f.is_optional() {
        format!("Option<{base}>")
    } else {
        base
    }
}

/// Returns true when a ComplexType is emitted as a type alias (not a trait).
///
/// gen_struct early-returns a `type Foo = ...` alias when the type has no
/// own fields and merely re-exports an extension base.  Such entries must be
/// excluded from ancestor delegation chains because type aliases cannot be
/// used as trait bounds.
fn is_type_alias(ct: &ComplexType) -> bool {
    ct.fields.is_empty() && ct.extension_base.is_some()
}

/// Collect the inheritance chain: [(local_name, &ComplexType)] from immediate base upward.
///
/// Ancestors that would be emitted as type aliases are skipped — they have no
/// corresponding trait definition and cannot appear in `impl Foo for Bar`.
fn base_chain<'a>(
    ct: &'a ComplexType,
    complex_map: &'a HashMap<&str, &'a ComplexType>,
) -> Vec<(&'a str, &'a ComplexType)> {
    let mut chain = Vec::new();
    let mut current = ct;
    while let Some(base_ref) = &current.extension_base {
        let local = base_ref
            .rfind(':')
            .map(|i| &base_ref[i + 1..])
            .unwrap_or(base_ref.as_str());
        match complex_map.get(local) {
            Some(base_ct) => {
                if !is_type_alias(base_ct) {
                    chain.push((local, *base_ct));
                }
                current = base_ct;
            }
            None => break,
        }
    }
    chain
}

/// Generate the is_valid() check code for a single field.
///
/// Returns a (possibly empty) block of Rust statements to be placed inside
/// the is_valid() body. Each statement returns early with Err on violation.
fn gen_field_validation(
    f: &Field,
    simple_map: &HashMap<&str, String>,
    resolver: &XsdResolver,
    enum_names: &HashSet<&str>,
    facets_map: &HashMap<&str, &Facets>,
) -> String {
    let field_name = snake(&f.name);
    let xsd_name = &f.name;
    let (type_ns, type_local) = resolver.resolve_pair(&f.type_);

    let rust_type = if let Some(resolved) = simple_map.get(type_local.as_str()) {
        resolved.clone()
    } else {
        xsd_to_rust_concrete(type_ns.as_deref(), &type_local)
    };

    // ── Vec fields ────────────────────────────────────────────────────────
    if f.is_vec() {
        let mut out = String::new();

        // Length check: skip only when min=0 AND unbounded (no constraint at all).
        let needs_len_check = f.min_occurs > 0 || matches!(f.max_occurs, MaxOccurs::Bounded(_));
        if needs_len_check {
            let min = f.min_occurs;
            // Use range-contains syntax to satisfy clippy::manual_range_contains.
            let (cond, max_desc) = match (min > 0, &f.max_occurs) {
                (true, MaxOccurs::Bounded(n)) => {
                    (format!("!({min}..={n}).contains(&_n)"), n.to_string())
                }
                (true, MaxOccurs::Unbounded) => (format!("_n < {min}"), "unbounded".to_string()),
                (false, MaxOccurs::Bounded(n)) => (format!("_n > {n}"), n.to_string()),
                (false, MaxOccurs::Unbounded) => {
                    unreachable!("needs_len_check requires min>0 or bounded max")
                }
            };
            out.push_str(&format!(
                "    {{\n\
                 \x20       let _n = self.{field_name}.len();\n\
                 \x20       if {cond} {{\n\
                 \x20           return Err(crate::uci::ValidationError {{\n\
                 \x20               path: format!(\"{{path}}.{xsd_name}\"),\n\
                 \x20               reason: format!(\"incorrect number of elements: got {{_n}}, expected {min}..={max_desc}\"),\n\
                 \x20           }});\n\
                 \x20       }}\n\
                 \x20   }}\n"
            ));
        }

        // Recurse into Vec elements if they have their own is_valid().
        if is_validatable_type(&rust_type, enum_names, &type_local) {
            out.push_str(&format!(
                "    for (_i, _item) in self.{field_name}.iter().enumerate() {{\n\
                 \x20       _item.is_valid_at(&format!(\"{{path}}.{xsd_name}[{{_i}}]\"))?;\n\
                 \x20   }}\n"
            ));
        }

        return out;
    }

    let is_opt = f.is_optional();

    // ── Enum fields ───────────────────────────────────────────────────────
    if enum_names.contains(type_local.as_str()) {
        return if is_opt {
            format!(
                "    if let Some(ref _v) = self.{field_name} {{\n\
                 \x20       _v.is_valid_at(&format!(\"{{path}}.{xsd_name}\"))?;\n\
                 \x20   }}\n"
            )
        } else {
            format!("    self.{field_name}.is_valid_at(&format!(\"{{path}}.{xsd_name}\"))?;\n")
        };
    }

    // ── Complex struct fields ─────────────────────────────────────────────
    if rust_type.starts_with("crate::uci::types::") && rust_type.ends_with('_') {
        return if is_opt {
            format!(
                "    if let Some(ref _v) = self.{field_name} {{\n\
                 \x20       _v.is_valid_at(&format!(\"{{path}}.{xsd_name}\"))?;\n\
                 \x20   }}\n"
            )
        } else {
            format!("    self.{field_name}.is_valid_at(&format!(\"{{path}}.{xsd_name}\"))?;\n")
        };
    }

    // ── xs:string with facets ─────────────────────────────────────────────
    if rust_type == "crate::xs::XsString"
        && let Some(facets) = facets_map.get(type_local.as_str())
    {
        let mut out = String::new();

        // String length check
        let eff_min = facets.length.or(facets.min_length).unwrap_or(0) as usize;
        let eff_max = facets.length.or(facets.max_length).map(|v| v as usize);
        let has_len_check = eff_min > 0 || eff_max.is_some();

        if has_len_check {
            // Use range-contains syntax to satisfy clippy::manual_range_contains.
            let cond = match (eff_min > 0, eff_max) {
                (true, Some(max)) => format!("!({eff_min}..={max}).contains(&_n)"),
                (true, None) => format!("_n < {eff_min}"),
                (false, Some(max)) => format!("_n > {max}"),
                (false, None) => unreachable!("has_len_check requires min>0 or max set"),
            };
            if is_opt {
                out.push_str(&format!(
                    "    if let Some(ref _v) = self.{field_name} {{\n\
                     \x20       let _n = _v.chars().count();\n\
                     \x20       if {cond} {{\n\
                     \x20           return Err(crate::uci::ValidationError {{\n\
                     \x20               path: format!(\"{{path}}.{xsd_name}\"),\n\
                     \x20               reason: \"string does not match constraints\".to_owned(),\n\
                     \x20           }});\n\
                     \x20       }}\n\
                     \x20   }}\n"
                ));
            } else {
                out.push_str(&format!(
                    "    {{\n\
                     \x20       let _n = self.{field_name}.chars().count();\n\
                     \x20       if {cond} {{\n\
                     \x20           return Err(crate::uci::ValidationError {{\n\
                     \x20               path: format!(\"{{path}}.{xsd_name}\"),\n\
                     \x20               reason: \"string does not match constraints\".to_owned(),\n\
                     \x20           }});\n\
                     \x20       }}\n\
                     \x20   }}\n"
                ));
            }
        }

        // Pattern check
        if let Some(pattern) = &facets.pattern {
            let static_name = format!("PATTERN_{}", snake(&f.name).to_uppercase());
            let pattern_escaped = pattern.replace('\\', "\\\\").replace('"', "\\\"");
            if is_opt {
                out.push_str(&format!(
                    "    if let Some(ref _v) = self.{field_name} {{\n\
                     \x20       static {static_name}: std::sync::OnceLock<regex::Regex> = std::sync::OnceLock::new();\n\
                     \x20       let _re = {static_name}.get_or_init(|| regex::Regex::new(\"{pattern_escaped}\").unwrap());\n\
                     \x20       if !_re.is_match(_v) {{\n\
                     \x20           return Err(crate::uci::ValidationError {{\n\
                     \x20               path: format!(\"{{path}}.{xsd_name}\"),\n\
                     \x20               reason: \"string does not match constraints\".to_owned(),\n\
                     \x20           }});\n\
                     \x20       }}\n\
                     \x20   }}\n"
                ));
            } else {
                out.push_str(&format!(
                    "    {{\n\
                     \x20       static {static_name}: std::sync::OnceLock<regex::Regex> = std::sync::OnceLock::new();\n\
                     \x20       let _re = {static_name}.get_or_init(|| regex::Regex::new(\"{pattern_escaped}\").unwrap());\n\
                     \x20       if !_re.is_match(&self.{field_name}) {{\n\
                     \x20           return Err(crate::uci::ValidationError {{\n\
                     \x20               path: format!(\"{{path}}.{xsd_name}\"),\n\
                     \x20               reason: \"string does not match constraints\".to_owned(),\n\
                     \x20           }});\n\
                     \x20       }}\n\
                     \x20   }}\n"
                ));
            }
        }

        if !out.is_empty() {
            return out;
        }
    }

    // ── xs:double / xs:float with range facets ────────────────────────────
    if (rust_type == "crate::xs::Double" || rust_type == "crate::xs::Float")
        && let Some(facets) = facets_map.get(type_local.as_str())
    {
        let float_suffix = if rust_type == "crate::xs::Double" {
            "f64"
        } else {
            "f32"
        };
        let has_min = facets.min_inclusive.is_some();
        let has_max = facets.max_inclusive.is_some();
        if has_min || has_max {
            let min_val = facets.min_inclusive.as_deref().unwrap_or("0");
            let max_val = facets.max_inclusive.as_deref().unwrap_or("0");
            // Use range-contains to satisfy clippy::manual_range_contains.
            // approx_constant / excessive_precision are suppressed at the file level.
            let cond = match (has_min, has_max) {
                (true, true) => {
                    format!("!({min_val}_{float_suffix}..={max_val}_{float_suffix}).contains(&_v)")
                }
                (true, false) => format!("_v < {min_val}_{float_suffix}"),
                (false, true) => format!("_v > {max_val}_{float_suffix}"),
                (false, false) => unreachable!(),
            };
            let range_str = match (has_min, has_max) {
                (true, true) => format!("[{min_val}, {max_val}]"),
                (true, false) => format!("[{min_val}, ∞)"),
                (false, true) => format!("(-∞, {max_val}]"),
                (false, false) => unreachable!(),
            };
            return if is_opt {
                // Combine let + range check to avoid collapsible_if.
                format!(
                    "    if let Some(_v) = self.{field_name}\n\
                     \x20       && {cond}\n\
                     \x20   {{\n\
                     \x20       return Err(crate::uci::ValidationError {{\n\
                     \x20           path: format!(\"{{path}}.{xsd_name}\"),\n\
                     \x20           reason: \"double is outside allowed range {range_str}\".to_owned(),\n\
                     \x20       }});\n\
                     \x20   }}\n"
                )
            } else {
                format!(
                    "    {{\n\
                     \x20       let _v = self.{field_name};\n\
                     \x20       if {cond} {{\n\
                     \x20           return Err(crate::uci::ValidationError {{\n\
                     \x20               path: format!(\"{{path}}.{xsd_name}\"),\n\
                     \x20               reason: \"double is outside allowed range {range_str}\".to_owned(),\n\
                     \x20           }});\n\
                     \x20       }}\n\
                     \x20   }}\n"
                )
            };
        }
    }

    String::new()
}

/// True if the given Rust type exposes an `is_valid(path: &str)` method.
fn is_validatable_type(rust_type: &str, enum_names: &HashSet<&str>, type_local: &str) -> bool {
    enum_names.contains(type_local)
        || (rust_type.starts_with("crate::uci::types::") && rust_type.ends_with('_'))
}

/// Converts a concrete complex-type path (`crate::uci::types::FooType_`) to its
/// `dyn Trait` form (`dyn crate::uci::types::FooType`) for use in trait signatures.
/// Non-complex types are returned unchanged.
fn dyn_type(rust_type: &str) -> String {
    if rust_type.starts_with("crate::uci::types::") && rust_type.ends_with('_') {
        format!("dyn {}", &rust_type[..rust_type.len() - 1])
    } else {
        rust_type.to_string()
    }
}

fn gen_choice_enum(
    ct: &ComplexType,
    simple_map: &HashMap<&str, String>,
    resolver: &XsdResolver,
    enum_names: &HashSet<&str>,
) -> String {
    let pascal_name = pascal(&ct.name);
    let mut out = String::new();

    // Derive enum name from the pre-populated simple_map entry (set in generate_types
    // before any gen_struct calls). The `_` suffix signals is_valid_at is meaningful.
    let enum_type_name = simple_map
        .get(ct.name.as_str())
        .and_then(|path| path.rsplit("::").next())
        .unwrap_or(&pascal_name)
        .to_string();

    out.push_str("// @generated — do not edit.\n#![allow(non_camel_case_types, non_snake_case, clippy::approx_constant, clippy::excessive_precision, clippy::wrong_self_convention, clippy::large_enum_variant)]\n\n");

    out.push_str(&format!("/// XSD complexType `{}` (xs:choice).\n", ct.name));
    out.push_str(
        "#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]\n\
         #[serde(untagged)]\n",
    );
    out.push_str(&format!("pub enum {enum_type_name} {{\n"));

    let mut first_variant_name = String::new();
    let mut first_payload_type = String::new();

    for (i, f) in ct.fields.iter().enumerate() {
        let variant_name = pascal(&f.name);
        let (type_ns, type_local) = resolver.resolve_pair(&f.type_);
        let payload_type = if let Some(resolved) = simple_map.get(type_local.as_str()) {
            resolved.clone()
        } else {
            xsd_to_rust_concrete(type_ns.as_deref(), &type_local)
        };
        if i == 0 {
            first_variant_name = variant_name.clone();
            first_payload_type = payload_type.clone();
        }
        let doc = format!("    /// XSD element `{}`.\n", f.name);
        out.push_str(&format!(
            "{doc}    {variant_name} {{\n\
             \x20       #[serde(rename = \"{xsd_name}\")]\n\
             \x20       inner: {payload_type},\n\
             \x20   }},\n",
            xsd_name = f.name,
        ));
    }
    out.push_str("}\n\n");

    // Manual Default impl — derive(Default) requires #[default] which only works on unit variants.
    if !first_variant_name.is_empty() {
        out.push_str(&format!(
            "impl Default for {enum_type_name} {{\n\
             \x20   fn default() -> Self {{\n\
             \x20       {enum_type_name}::{first_variant_name} {{ inner: <{first_payload_type}>::default() }}\n\
             \x20   }}\n\
             }}\n\n"
        ));
    }

    // is_valid_at: match on active variant only.
    // Pre-compute all match arms; derive path_param from whether any arm uses is_valid_at.
    let arms: Vec<(String, String, String)> = ct
        .fields
        .iter()
        .map(|f| {
            let variant_name = pascal(&f.name);
            let (type_ns, type_local) = resolver.resolve_pair(&f.type_);
            let rust_type = if let Some(resolved) = simple_map.get(type_local.as_str()) {
                resolved.clone()
            } else {
                xsd_to_rust_concrete(type_ns.as_deref(), &type_local)
            };
            let (binding, validation) = if is_validatable_type(&rust_type, enum_names, &type_local)
            {
                (
                    "inner".to_string(),
                    format!("inner.is_valid_at(&format!(\"{{path}}.{}\"))", f.name),
                )
            } else {
                ("inner: _".to_string(), "Ok(())".to_string())
            };
            (variant_name, binding, validation)
        })
        .collect();
    let path_param = if arms.iter().any(|(_, _, v)| v.contains("is_valid_at")) {
        "path"
    } else {
        "_path"
    };
    out.push_str(&format!("impl {enum_type_name} {{\n"));
    out.push_str(&format!(
        "    pub fn is_valid_at(&self, {path_param}: &str) -> Result<(), crate::uci::ValidationError> {{\n\
         \x20       match self {{\n"
    ));
    for (variant_name, binding, validation) in &arms {
        out.push_str(&format!(
            "            {enum_type_name}::{variant_name} {{ {binding} }} => {validation},\n"
        ));
    }
    out.push_str("        }\n    }\n}\n\n");

    out.push_str(&format!(
        "impl crate::uci::CalSubMessage for {enum_type_name} {{}}\n"
    ));

    out
}

#[allow(clippy::too_many_arguments)]
fn gen_struct(
    ct: &ComplexType,
    simple_map: &HashMap<&str, String>,
    type_to_element: &HashMap<&str, &str>,
    resolver: &XsdResolver,
    complex_map: &HashMap<&str, &ComplexType>,
    enum_names: &HashSet<&str>,
    facets_map: &HashMap<&str, &Facets>,
    choice_type_names: &HashSet<&str>,
) -> String {
    let pascal_name = pascal(&ct.name);

    // xs:choice types are emitted as enums, not structs.
    if ct.is_choice {
        return gen_choice_enum(ct, simple_map, resolver, enum_names);
    }

    // Extension with no additional fields → type alias; no trait needed.
    if ct.fields.is_empty()
        && let Some(base) = &ct.extension_base
    {
        let rust_type = resolve_base_rust_type(base, simple_map, resolver);
        return format!(
            "// @generated — do not edit.\n#![allow(non_camel_case_types)]\n\n\
             /// XSD complexType `{}` (extension of `{}`).\n\
             pub type {pascal_name} = {rust_type};\n",
            ct.name, base,
        );
    }

    let chain = base_chain(ct, complex_map);
    let immediate_base_local = chain.first().map(|(n, _)| *n);

    // Supertrait clause for the generated trait.
    let supertrait = match immediate_base_local {
        Some(base_local) => {
            let base_pascal = pascal(base_local);
            format!(": crate::uci::types::{base_pascal} ")
        }
        None => String::new(),
    };

    // --- Trait methods ---
    let mut trait_methods = String::new();
    for f in &ct.fields {
        let field_name = snake(&f.name);
        let (type_ns, type_local) = resolver.resolve_pair(&f.type_);
        let rust_type = if let Some(resolved) = simple_map.get(type_local.as_str()) {
            resolved.clone()
        } else {
            xsd_to_rust_concrete(type_ns.as_deref(), &type_local)
        };
        let dyn_rt = if choice_type_names.contains(type_local.as_str()) {
            rust_type.clone()
        } else {
            dyn_type(&rust_type)
        };
        if f.is_vec() {
            trait_methods.push_str(&format!(
                "    /// Returns the XSD element sequence `{elem}`.\n\
                 \x20   fn {field_name}(&self) -> &[{rust_type}];\n\
                 \x20   /// Returns a mutable reference to the XSD element sequence `{elem}`.\n\
                 \x20   fn {field_name}_mut(&mut self) -> &mut crate::uci::base::BoundedList<{rust_type}>;\n",
                elem = f.name,
            ));
        } else if f.is_optional() {
            trait_methods.push_str(&format!(
                "    /// Returns the optional XSD element `{elem}`.\n\
                 \x20   fn {field_name}(&self) -> Option<&{dyn_rt}>;\n\
                 \x20   /// Returns a mutable reference to the optional XSD element `{elem}`.\n\
                 \x20   fn {field_name}_mut(&mut self) -> Option<&mut {dyn_rt}>;\n",
                elem = f.name,
            ));
        } else {
            trait_methods.push_str(&format!(
                "    /// Returns the XSD element `{elem}`.\n\
                 \x20   fn {field_name}(&self) -> &{dyn_rt};\n\
                 \x20   /// Returns a mutable reference to the XSD element `{elem}`.\n\
                 \x20   fn {field_name}_mut(&mut self) -> &mut {dyn_rt};\n",
                elem = f.name,
            ));
        }
    }

    // --- Struct fields ---
    let inherited_fields_str: String = chain
        .iter()
        .rev()
        .flat_map(|(_, ancestor_ct)| &ancestor_ct.fields)
        .map(|f| {
            let field_name = snake(&f.name);
            let full_type = field_rust_type(f, simple_map, resolver);
            let tag = if f.is_vec() {
                " (sequence, inherited)"
            } else if f.is_optional() {
                " (optional, inherited)"
            } else {
                " (inherited)"
            };
            let doc = format!("    /// XSD element `{}`{tag}.\n", f.name);
            let serde_rename = format!("    #[serde(rename = \"{}\")]\n", f.name);
            let maybe_skip = if f.is_optional() {
                "    #[serde(skip_serializing_if = \"Option::is_none\")]\n"
            } else if f.is_vec() {
                "    #[serde(default, skip_serializing_if = \"crate::uci::base::BoundedList::is_empty\")]\n"
            } else {
                ""
            };
            format!("{doc}{serde_rename}{maybe_skip}    {field_name}: {full_type},\n")
        })
        .collect();

    let inherited_defaults_str: String = chain
        .iter()
        .rev()
        .flat_map(|(_, ancestor_ct)| &ancestor_ct.fields)
        .map(|f| {
            let field_name = snake(&f.name);
            let default_val = if f.is_optional() {
                "None".to_string()
            } else {
                "Default::default()".to_string()
            };
            format!("            {field_name}: {default_val},\n")
        })
        .collect();

    let struct_fields: String = ct
        .fields
        .iter()
        .map(|f| {
            let field_name = snake(&f.name);
            let full_type = field_rust_type(f, simple_map, resolver);
            let tag = if f.is_vec() {
                " (sequence)"
            } else if f.is_optional() {
                " (optional)"
            } else {
                ""
            };
            let doc = format!("    /// XSD element `{}`{tag}.\n", f.name);
            let serde_rename = format!("    #[serde(rename = \"{}\")]\n", f.name);
            let maybe_skip = if f.is_optional() {
                "    #[serde(skip_serializing_if = \"Option::is_none\")]\n"
            } else if f.is_vec() {
                "    #[serde(default, skip_serializing_if = \"crate::uci::base::BoundedList::is_empty\")]\n"
            } else {
                ""
            };
            format!("{doc}{serde_rename}{maybe_skip}    {field_name}: {full_type},\n")
        })
        .collect();

    let field_defaults: String = ct
        .fields
        .iter()
        .map(|f| {
            let field_name = snake(&f.name);
            let default_val = if f.is_optional() {
                "None".to_string()
            } else {
                "Default::default()".to_string()
            };
            format!("            {field_name}: {default_val},\n")
        })
        .collect();

    // --- Own trait impl bodies ---
    let own_trait_impl: String = ct
        .fields
        .iter()
        .map(|f| {
            let field_name = snake(&f.name);
            let (type_ns, type_local) = resolver.resolve_pair(&f.type_);
            let rust_type = if let Some(resolved) = simple_map.get(type_local.as_str()) {
                resolved.clone()
            } else {
                xsd_to_rust_concrete(type_ns.as_deref(), &type_local)
            };
            let is_choice_field = choice_type_names.contains(type_local.as_str());
            let dyn_rt = if is_choice_field {
                rust_type.clone()
            } else {
                dyn_type(&rust_type)
            };
            if f.is_vec() {
                format!(
                    "    fn {field_name}(&self) -> &[{rust_type}] {{ &self.{field_name} }}\n\
                     fn {field_name}_mut(&mut self) -> &mut crate::uci::base::BoundedList<{rust_type}> {{ &mut self.{field_name} }}\n"
                )
            } else if f.is_optional() {
                if is_choice_field {
                    format!(
                        "    fn {field_name}(&self) -> Option<&{dyn_rt}> {{ self.{field_name}.as_ref() }}\n\
                         fn {field_name}_mut(&mut self) -> Option<&mut {dyn_rt}> {{ self.{field_name}.as_mut() }}\n"
                    )
                } else {
                    format!(
                        "    fn {field_name}(&self) -> Option<&{dyn_rt}> {{ self.{field_name}.as_ref().map(|v| v as &{dyn_rt}) }}\n\
                         fn {field_name}_mut(&mut self) -> Option<&mut {dyn_rt}> {{ self.{field_name}.as_mut().map(|v| v as &mut {dyn_rt}) }}\n"
                    )
                }
            } else {
                format!(
                    "    fn {field_name}(&self) -> &{dyn_rt} {{ &self.{field_name} }}\n\
                     fn {field_name}_mut(&mut self) -> &mut {dyn_rt} {{ &mut self.{field_name} }}\n"
                )
            }
        })
        .collect();

    // --- Ancestor delegation impls ---
    let ancestor_impls: String = chain
        .iter()
        .map(|(ancestor_local, ancestor_ct)| {
            let ancestor_pascal = pascal(ancestor_local);
            let methods: String = ancestor_ct
                .fields
                .iter()
                .map(|f| {
                    let field_name = snake(&f.name);
                    let (type_ns, type_local) = resolver.resolve_pair(&f.type_);
                    let rust_type = if let Some(resolved) = simple_map.get(type_local.as_str()) {
                        resolved.clone()
                    } else {
                        xsd_to_rust_concrete(type_ns.as_deref(), &type_local)
                    };
                    let is_choice_field = choice_type_names.contains(type_local.as_str());
                    let dyn_rt = if is_choice_field {
                        rust_type.clone()
                    } else {
                        dyn_type(&rust_type)
                    };
                    if f.is_vec() {
                        format!(
                            "    fn {field_name}(&self) -> &[{rust_type}] {{ &self.{field_name} }}\n\
                             fn {field_name}_mut(&mut self) -> &mut crate::uci::base::BoundedList<{rust_type}> {{ &mut self.{field_name} }}\n"
                        )
                    } else if f.is_optional() {
                        if is_choice_field {
                            format!(
                                "    fn {field_name}(&self) -> Option<&{dyn_rt}> {{ self.{field_name}.as_ref() }}\n\
                                 fn {field_name}_mut(&mut self) -> Option<&mut {dyn_rt}> {{ self.{field_name}.as_mut() }}\n"
                            )
                        } else {
                            format!(
                                "    fn {field_name}(&self) -> Option<&{dyn_rt}> {{ self.{field_name}.as_ref().map(|v| v as &{dyn_rt}) }}\n\
                                 fn {field_name}_mut(&mut self) -> Option<&mut {dyn_rt}> {{ self.{field_name}.as_mut().map(|v| v as &mut {dyn_rt}) }}\n"
                            )
                        }
                    } else {
                        format!(
                            "    fn {field_name}(&self) -> &{dyn_rt} {{ &self.{field_name} }}\n\
                             fn {field_name}_mut(&mut self) -> &mut {dyn_rt} {{ &mut self.{field_name} }}\n"
                        )
                    }
                })
                .collect();
            format!("impl crate::uci::types::{ancestor_pascal} for {pascal_name}_ {{\n{methods}}}\n\n")
        })
        .collect();

    let is_element_backed = type_to_element.contains_key(ct.name.as_str());

    // --- is_valid() body ---
    // Collect checks for all fields (inherited first, then own).
    let all_fields: Vec<&Field> = chain
        .iter()
        .rev()
        .flat_map(|(_, ancestor_ct)| ancestor_ct.fields.iter())
        .chain(ct.fields.iter())
        .collect();

    let validation_checks: String = all_fields
        .iter()
        .map(|f| gen_field_validation(f, simple_map, resolver, enum_names, facets_map))
        .collect();

    let mut out = String::new();
    out.push_str("// @generated — do not edit.\n#![allow(non_camel_case_types, non_snake_case, clippy::approx_constant, clippy::excessive_precision, clippy::wrong_self_convention)]\n\n");

    // Trait
    out.push_str(&format!(
        "/// Accessor trait for XSD complexType `{}`.\n",
        ct.name
    ));
    out.push_str(&format!("pub trait {pascal_name} {supertrait}{{\n"));
    out.push_str(&trait_methods);
    out.push_str("}\n\n");

    // Struct
    out.push_str(&format!("/// XSD complexType `{}`.\n", ct.name));
    let derives = if is_element_backed {
        "#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]\n"
    } else {
        "#[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize)]\n"
    };
    out.push_str(derives);
    out.push_str(&format!("#[serde(rename = \"{pascal_name}\")]\n"));
    out.push_str(&format!("pub struct {pascal_name}_ {{\n"));
    out.push_str(&inherited_fields_str);
    out.push_str(&struct_fields);
    if is_element_backed {
        out.push_str("    #[serde(skip)]\n");
        out.push_str("    _priv: crate::uci::sealed::Token,\n");
    }
    out.push_str("}\n\n");

    // _cal_create for element-backed types
    if is_element_backed {
        out.push_str(&format!("impl {pascal_name}_ {{\n"));
        out.push_str("    pub(crate) fn _cal_create() -> Self {\n");
        out.push_str("        Self {\n");
        out.push_str(&inherited_defaults_str);
        out.push_str(&field_defaults);
        out.push_str("            _priv: crate::uci::sealed::Token(()),\n");
        out.push_str("        }\n");
        out.push_str("    }\n");
        out.push_str("}\n\n");
    }

    // is_valid()
    let path_param = if validation_checks.is_empty() {
        "_path"
    } else {
        "path"
    };
    out.push_str(&format!("impl {pascal_name}_ {{\n"));
    out.push_str("    /// Validates all fields against their XSD schema constraints.\n");
    out.push_str("    ///\n");
    out.push_str(
        "    /// `path` is the dot-separated path to this element, used in error messages.\n",
    );
    out.push_str(&format!("    pub fn is_valid_at(&self, {path_param}: &str) -> Result<(), crate::uci::ValidationError> {{\n"));
    out.push_str(&validation_checks);
    out.push_str("        Ok(())\n");
    out.push_str("    }\n");
    out.push_str("}\n\n");

    // Own trait impl
    out.push_str(&format!("impl {pascal_name} for {pascal_name}_ {{\n"));
    out.push_str(&own_trait_impl);
    out.push_str("}\n\n");

    // Ancestor delegation impls
    out.push_str(&ancestor_impls);

    // CalSubMessage marker
    if ct.abstract_ || !is_element_backed {
        out.push_str(&format!(
            "impl crate::uci::CalSubMessage for {pascal_name}_ {{}}\n"
        ));
    }

    out
}

// ════════════════════════════════════════════════════════════════════════════
// Name helpers
// ════════════════════════════════════════════════════════════════════════════

fn pascal(s: &str) -> String {
    let local = s.rfind(':').map(|i| &s[i + 1..]).unwrap_or(s);
    local.to_string()
}

const RUST_KEYWORDS: &[&str] = &[
    "as", "break", "const", "continue", "crate", "else", "enum", "extern", "false", "fn", "for",
    "if", "impl", "in", "let", "loop", "match", "mod", "move", "mut", "pub", "ref", "return",
    "self", "Self", "static", "struct", "super", "trait", "true", "type", "unsafe", "use", "where",
    "while", "async", "await", "dyn", "abstract", "become", "box", "do", "final", "macro",
    "override", "priv", "typeof", "unsized", "virtual", "yield", "try",
];

fn snake(s: &str) -> String {
    let local = s.rfind(':').map(|i| &s[i + 1..]).unwrap_or(s);
    let mut out = String::new();
    let mut prev_upper = false;
    let mut prev_under = false;
    for (i, c) in local.chars().enumerate() {
        if c.is_uppercase() {
            if i > 0 && !prev_upper && !prev_under {
                out.push('_');
            }
            out.push(c.to_lowercase().next().unwrap());
            prev_upper = true;
            prev_under = false;
        } else if c == '_' {
            prev_under = true;
        } else {
            out.push(c);
            prev_upper = false;
            prev_under = false;
        }
    }
    if RUST_KEYWORDS.contains(&out.as_str()) {
        out.push('_');
    }
    out
}

fn enum_variant(s: &str) -> String {
    let mut chars = s.chars();
    match chars.next() {
        None => String::new(),
        Some(first) if first.is_ascii_digit() => {
            format!("V{}{}", first, chars.as_str().to_lowercase())
        }
        Some(first) => first.to_uppercase().collect::<String>() + &chars.as_str().to_lowercase(),
    }
}

fn xsd_to_rust(ns: Option<&str>, local: &str) -> String {
    const XS: &str = "http://www.w3.org/2001/XMLSchema";
    if ns == Some(XS) {
        match local {
            "boolean" => return "crate::xs::Boolean".to_string(),
            "long" => return "crate::xs::Long".to_string(),
            "int" => return "crate::xs::Int".to_string(),
            "short" => return "crate::xs::Short".to_string(),
            "byte" => return "crate::xs::Byte".to_string(),
            "unsignedLong" => return "crate::xs::UnsignedLong".to_string(),
            "unsignedInt" => return "crate::xs::UnsignedInt".to_string(),
            "unsignedShort" => return "crate::xs::UnsignedShort".to_string(),
            "unsignedByte" => return "crate::xs::UnsignedByte".to_string(),
            "double" => return "crate::xs::Double".to_string(),
            "float" => return "crate::xs::Float".to_string(),
            "integer" => return "crate::xs::Integer".to_string(),
            "duration" => return "crate::xs::Duration".to_string(),
            "dateTime" => return "crate::xs::DateTime".to_string(),
            "time" => return "crate::xs::Time".to_string(),
            "string" => return "crate::xs::XsString".to_string(),
            "hexBinary" => return "crate::xs::HexBinary".to_string(),
            _ => {}
        }
    }
    format!("crate::uci::types::{}", pascal(local))
}

fn xsd_to_rust_concrete(ns: Option<&str>, local: &str) -> String {
    const XS: &str = "http://www.w3.org/2001/XMLSchema";
    if ns == Some(XS) {
        xsd_to_rust(ns, local)
    } else {
        format!("crate::uci::types::{}_", pascal(local))
    }
}