zerodds-lang-rules 1.0.0-rc.7.1

Maps the common AST onto the language CSTs through rules that are data, not code: seesaw v2 rule specifications, comparable and checkable.
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
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
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
// SPDX-License-Identifier: Apache-2.0
// Copyright 2026 ZeroDDS Contributors
//! Encoder and decoder bodies, built from the layout plan. Spec §5, §6.2.
//!
//! The body of an `encode` is a language CST — statements, calls, casts —
//! not a template with holes for names. A template would put the
//! per-construct decisions back where no diff of two languages can see
//! them, one hole at a time, which is what this rebuild took them out
//! of.
//!
//! Every framing decision is read from `zerodds-ast-layout`, never
//! re-derived here. The plan is where the length-code policy and the
//! XCDR2 alignment rule live; a second derivation would be a second
//! place to get them wrong.

use zerodds_ast::{Ast, CharWidth, PrimitiveKind, TryConstruct, TypeKind, TypeNode};
use zerodds_ast_core::{Id, NodeId};
use zerodds_ast_layout::key::{
    ElementPadding, FixedDigitCount, LayoutKey, PlCdr1HeaderPolicy, Purpose, WcharPosition,
    WirePosition, WstringCodeUnits, WstringLengthCounts, WstringLengthPolicy, WstringTerminator,
    WstringUnitOrder, WstringUnitWidth,
};
use zerodds_ast_layout::plan::{
    Extent, LayoutError, MemberHeader, MemberPlan, ScalarWrite, TypeLayout,
};
use zerodds_ast_layout::xcdr::DepCollector;
use zerodds_lang_cst::common::{Doc, Ident, Literal, Path, Visibility};
use zerodds_lang_cst::rust;

/// Why an encoder could not be built.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum EncodeError {
    /// The layout could not be planned, with the plan's own reason.
    NoLayout {
        /// What the layout crate said.
        detail: String,
    },
    /// The specification itself does not admit the type, in the
    /// planner's own words and by its clause.
    ///
    /// Kept apart from [`Self::NoLayout`] because the two answer
    /// different questions and `docs/specs/zerodds-verdicts-1.0.md`
    /// gives them different exit codes: a gap is "a tool of **ours**
    /// has not written it" (exit 6, `harness-gap`), and this is "it was
    /// asked and said no" (exit 1, `refused`). Reporting a clause of
    /// IDL 4.2 as a gap of this tree states something about this tree
    /// that is not true.
    ///
    /// The distinction was a string until 2026-08-26, and twelve
    /// generators each classified the same refusal on their own:
    /// measured over `recursive-array`, ada and d answered exit 3, zig
    /// answered 1, and twelve answered 6.
    SpecRefuses {
        /// The clause and what it refuses, from the planner.
        detail: String,
    },
    /// A member's type has no writer.
    ///
    /// Reported rather than skipped: a member left out of an encoder is
    /// a wire format that silently disagrees with its own plan.
    NoWriter {
        /// The member.
        member: String,
        /// What kind of type it is.
        kind: &'static str,
    },
    /// An enumerator's wire value could not be computed.
    ///
    /// Reported rather than skipped, for the same reason as
    /// [`Self::NoWriter`] and with a sharper edge. The C encoder's
    /// enumeration chain initialises its holder to the **declared**
    /// value and lets each arm overwrite it, so an enumerator that
    /// reaches no arm is not absent from the stream — it is written
    /// with the declared integer. Under
    /// `zerodds_ast_layout::EnumeratorValue::Ordinal` that is a
    /// different integer from the one every other column writes, and
    /// nothing at run time would say so.
    NoEnumeratorValue {
        /// The enumeration the enumerator belongs to.
        enumeration: String,
        /// The enumerator.
        enumerator: String,
        /// Why its wire value could not be computed.
        detail: &'static str,
    },
    /// A member's PL_CDR1 header form is not one the sample's flag can
    /// express.
    ///
    /// Under a [per-sample](`PlCdr1HeaderPolicy::decided_per_sample`)
    /// policy the header is chosen at run time, and the choice is read
    /// by planning the type under the two policies the flag stands
    /// between — [`PlCdr1HeaderPolicy::Specification`] and
    /// [`PlCdr1HeaderPolicy::ExtendedForConstructed`]. A member whose
    /// two plans differ in `extended` alone is one the flag decides; a
    /// member whose two plans differ in anything else is a case where
    /// `zd_extended` is not the whole of the answer, and picking one of
    /// the two plans would write bytes nothing measured.
    AmbiguousHeader {
        /// The member the two plans disagree about.
        member: String,
        /// What each plan said, so the difference can be read.
        detail: String,
    },
}

impl EncodeError {
    /// Whether the specification refused the type, rather than this tree
    /// not having written a writer for it.
    ///
    /// The one place a generator asks the question, so that sixteen of
    /// them do not each answer it from the error's text.
    #[must_use]
    pub const fn is_spec_refusal(&self) -> bool {
        matches!(self, Self::SpecRefuses { .. })
    }
}

/// Refuses a layout key whose axes the **non-Rust** generated codecs do
/// not carry.
///
/// [`zerodds_ast_layout::Dialect::Hdds`] selects three writer axes the
/// planner and `zerodds-wire-codec` carry: collection elements packed
/// without alignment, a length in front of each element of a sequence of a
/// non-`@final` type, and a `wstring` in UTF-8. The Rust codec carries all
/// three — see [`axes_the_rust_codec_carries`], which [`rust_codec`] and
/// [`rust_codec_pair`] call instead. A generated codec of another column
/// asks [`axes_the_codecs_carry_for`], which passes for a target whose
/// runtime carries them and refuses the rest: generating their default
/// form under them would write the specification's octets where the key
/// asked for another implementation's. The refusal is a gap of this tree
/// (`NoLayout`, exit 6), named axis by axis; this function is it for a
/// caller that names no target.
///
/// # Errors
/// [`EncodeError::NoLayout`] naming every such axis the key selects.
pub fn axes_the_codecs_carry(key: LayoutKey) -> Result<(), EncodeError> {
    axes_the_codecs_carry_named(key, false)
}

/// [`axes_the_codecs_carry`] for a generated codec, which carries the
/// axes its runtime does.
///
/// A target whose runtime has the packed-element origin, the element
/// length in DHEADER form and a UTF-8 `wstring` — the three axes
/// [`zerodds_ast_layout::Dialect::Hdds`] selects — answers
/// [`crate::emit::CodecTarget::carries_hdds_axes`] `true` and passes;
/// one that has not been given them is refused with the axes named.
///
/// # Errors
/// [`EncodeError::NoLayout`] naming every axis the key selects that the
/// target does not carry.
pub fn axes_the_codecs_carry_for<T: crate::emit::CodecTarget>(
    key: LayoutKey,
) -> Result<(), EncodeError> {
    axes_the_codecs_carry_named(key, T::carries_hdds_axes())
}

fn axes_the_codecs_carry_named(key: LayoutKey, carries: bool) -> Result<(), EncodeError> {
    if carries {
        return Ok(());
    }
    let mut missing: Vec<&str> = Vec::new();
    if !key.element_padding.is_default() {
        missing.push(key.element_padding.as_str());
    }
    if !key.sequence_element_delimiter.is_default() {
        missing.push(key.sequence_element_delimiter.as_str());
    }
    if !key.wstring_code_units.is_default() {
        missing.push(key.wstring_code_units.as_str());
    }
    if missing.is_empty() {
        Ok(())
    } else {
        Err(EncodeError::NoLayout {
            detail: format!(
                "the generated codecs do not carry {} yet",
                missing.join(", ")
            ),
        })
    }
}

/// The same check for the Rust target, which carries every HDDS axis.
///
/// [`ElementPadding::Packed`],
/// [`zerodds_ast_layout::SequenceElementDelimiter::BeforeEachNonFinalElement`]
/// and [`WstringCodeUnits::Utf8`] are emitted by [`rust_codec`] and
/// [`rust_codec_pair`] exactly as `zerodds-wire-codec` writes them, so
/// this target has no axis left to refuse. The function exists so that the
/// two targets ask one question through one name rather than the Rust
/// path skipping the check by having no call at all.
///
/// # Errors
/// Nothing today; the signature matches [`axes_the_codecs_carry_for`],
/// the check of the other columns.
pub fn axes_the_rust_codec_carries(_key: LayoutKey) -> Result<(), EncodeError> {
    Ok(())
}

impl core::fmt::Display for EncodeError {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match self {
            Self::NoLayout { detail } => write!(f, "the layout could not be planned: {detail}"),
            Self::SpecRefuses { detail } => write!(f, "{detail}"),
            Self::NoWriter { member, kind } => {
                write!(f, "`{member}` is a {kind}, which has no writer yet")
            }
            Self::NoEnumeratorValue {
                enumeration,
                enumerator,
                detail,
            } => write!(
                f,
                "`{enumeration}::{enumerator}` has no wire value: {detail}"
            ),
            Self::AmbiguousHeader { member, detail } => write!(
                f,
                "`{member}` has no single PL_CDR1 header form to choose between: {detail}"
            ),
        }
    }
}

impl core::error::Error for EncodeError {}

/// The name the **second** codec pair on a type carries.
///
/// A Rust type may hold only one inherent function per name, so a type
/// that carries both of its XCDR streams needs two names. The XCDR
/// version is what earns one: XTypes 1.3 §7.4 frames the two versions
/// differently — no DHEADER under version 1, PID headers instead of
/// EMHEADERs for `@mutable`, `MAXALIGN` eight instead of four — so the
/// two bodies are different code.
///
/// The byte order does not earn one. Nothing in this emitter reads
/// [`LayoutKey::endianness`]: the order is the `Writer`'s and the
/// `Reader`'s, so one body serves both and a suffix per order would
/// duplicate identical code.
///
/// [`rust_codec`] keeps the bare names whatever encoding it is given, so
/// every consumer that generates one codec per type is unaffected by
/// this existing; only [`rust_codec_pair`], which exists to put a second
/// one beside it, spells them out.
pub const SECONDARY_SUFFIX: &str = "_xcdr1";

/// The parameter that carries a sample's RTI PL_CDR1 step decision down
/// to a nested type's encoder.
///
/// RTI frames a sample under XCDR1 with the short parameter header until
/// its type's maximum serialized size passes a step, and from there
/// gives `PID_EXTENDED` to every member whose type is not a primitive or
/// an enumeration (XTypes 1.3 §7.4.1.2, in RTI's measured reading of it).
/// The step is a property of the **sample's** type and not of the type
/// that declares a member, so a generated type cannot bake the answer in:
/// the same nested `@mutable` type is framed short inside a small sample
/// and extended inside a large one. The flag is threaded at run time; the
/// sample's root supplies it through [`sample_past_step`], which asks the
/// same crossing the reflective encoder asks.
const EXTENDED_PARAM: &str = "zd_extended";

/// The suffix the encoder **body** takes under a [per-sample](`per_sample`)
/// plan.
///
/// A Rust type may hold only one inherent function per name, so a type
/// that needs both a body taking the flag and a public entry does so the
/// same way [`SECONDARY_SUFFIX`] splits the two encodings.
const EXTENDED_SUFFIX: &str = "_zd_extended";

/// Whether the plan's PL_CDR1 header form is decided per sample, so every
/// generated encoder takes [`EXTENDED_PARAM`] and threads it to the types
/// it calls.
fn per_sample(key: LayoutKey) -> bool {
    key.pl_cdr1_headers.decided_per_sample() && key.encoding.is_v1()
}

/// Whether a sample of `root` passes RTI's short-header step, the
/// generation-time half of the per-sample decision.
///
/// The same count [`LayoutKey::for_sample`] makes, asked through it
/// rather than through `exceeds_short_length` so that the generated
/// entry and the reflective path read one answer.
fn sample_past_step(ast: &Ast, root: Id<TypeNode>, key: LayoutKey) -> Result<bool, EncodeError> {
    let resolved = key.for_sample(ast, root).map_err(plan_refusal)?;
    Ok(resolved.pl_cdr1_headers == PlCdr1HeaderPolicy::ExtendedForConstructed)
}

/// The name of the encoder **body** for a codec suffix: the method that
/// takes [`EXTENDED_PARAM`], or the public entry itself when the plan's
/// header form is fixed.
///
/// Under [`per_sample`] one type needs two functions. The body carries
/// the sample's decision as a parameter, so one type's codec can frame
/// its constructed members short in a small sample and extended in a
/// large one; the public entry bakes the decision in for the sample whose
/// root this type is. Without the split, the nested `Inner` of
/// `tools/idlc/tests/dialect_byte_equality.rs`'s `rti_step_cases` could
/// not serve both its samples.
fn body_name(codec: &str, key: LayoutKey) -> String {
    if per_sample(key) {
        framed_body(codec)
    } else {
        format!("encode{codec}")
    }
}

/// The name of an encoder body that takes the sample's decision, for a
/// nested call that carries `extended`.
fn nested_method(codec: &str, extended: Extended) -> String {
    match extended {
        Extended::Fixed => format!("encode{codec}"),
        Extended::Inherited | Extended::Constant(_) => framed_body(codec),
    }
}

/// [`nested_method`]'s name on the other side of the split.
fn framed_body(codec: &str) -> String {
    format!("encode{codec}{EXTENDED_SUFFIX}")
}

/// The sample's RTI PL_CDR1 step decision, as a nested type's call
/// carries it.
///
/// A data sample's body passes its own flag down, because the sample's
/// root resolved it; a key holder passes [`Self::Constant`] `false`. A
/// key holder keeps the short form whatever the sample: the choice of
/// `PID_EXTENDED` for a constructed member is a data-sample decision —
/// [`LayoutKey::for_sample`] is asked for one, and
/// `zerodds_ast_layout::xcdr::extended_as_constructed` refuses the key
/// purpose for the same reason — and the reflective planner keeps a key
/// holder short. The data path resolves the choice once at the root and
/// hands it down; the holder has no root decision to hand down and states
/// the constant.
///
/// `pub(crate)`: it is a field of [`MemberWrite::Nested`], which is.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum Extended {
    /// The plan fixes the header form: the call names the public entry,
    /// which takes no flag. Every plan outside [`per_sample`].
    Fixed,
    /// Hand the enclosing body's flag to the nested call.
    Inherited,
    /// Pass this constant instead.
    Constant(bool),
}

impl Extended {
    /// The answer for a codec written under `key`.
    ///
    /// The key holder's constant is what `crate::emit::encode`'s
    /// `nested_call_with` passes for the same reason.
    #[must_use]
    fn of(key: LayoutKey) -> Self {
        if !per_sample(key) {
            Self::Fixed
        } else if key.purpose == Purpose::Data {
            Self::Inherited
        } else {
            Self::Constant(false)
        }
    }

    /// The flag argument the nested call passes, if it passes one.
    #[must_use]
    fn argument(self) -> Option<rust::Expr> {
        match self {
            Self::Fixed => None,
            Self::Inherited => Some(rust::Expr::Path(Path::single(EXTENDED_PARAM))),
            Self::Constant(value) => Some(rust::Expr::Literal(Literal::Bool(value))),
        }
    }
}

/// Which of one type's members take the sample's step decision at run
/// time.
///
/// A member is one exactly when the two policies a per-sample plan is
/// between disagree about its `extended` bit:
/// [`PlCdr1HeaderPolicy::Specification`] frames a sample that stays
/// within the short form's length,
/// [`PlCdr1HeaderPolicy::ExtendedForConstructed`] one that can exceed it.
/// Keyed by member identity and not by position, because a union finds
/// its case's plan in the layout by identity.
struct StepHeaders {
    /// Whether the plan decides the form per sample at all, and the
    /// entries below mean anything.
    per_sample: bool,
    /// One entry per member of the layout: does the flag choose its
    /// header form?
    members: Vec<(NodeId, bool)>,
    /// The same for a `@mutable` union's discriminator, which rules (27)
    /// and (28) frame as an `MMEMBER` at member id 0 and which is
    /// therefore a header the flag can choose too.
    discriminator: bool,
}

impl StepHeaders {
    /// No member takes the flag, which is what every plan outside
    /// [`per_sample`] plans.
    fn none() -> Self {
        Self {
            per_sample: false,
            members: Vec::new(),
            discriminator: false,
        }
    }

    /// Whether the flag chooses `plan`'s member's header form.
    ///
    /// # Errors
    /// [`EncodeError::NoLayout`] for a member this set does not name —
    /// a plan that lost one would leave a member whose header nothing
    /// wrote.
    fn member(&self, plan: &MemberPlan, name: &str) -> Result<bool, EncodeError> {
        if !self.per_sample {
            return Ok(false);
        }
        self.members
            .iter()
            .find(|(id, _)| *id == plan.member)
            .map(|(_, chosen)| *chosen)
            .ok_or_else(|| EncodeError::NoLayout {
                detail: format!(
                    "the PL_CDR1 plans for a sample past RTI's step name no member `{name}`, \
                     and its header form would be left unwritten"
                ),
            })
    }
}

/// The plan `type_` takes under one PL_CDR1 header policy.
///
/// # Errors
/// [`EncodeError`] when the type cannot be planned under that policy.
fn plan_under(
    ast: &Ast,
    type_: Id<TypeNode>,
    key: LayoutKey,
    policy: PlCdr1HeaderPolicy,
) -> Result<TypeLayout, EncodeError> {
    zerodds_ast_layout::xcdr::plan(
        ast,
        type_,
        key.with_pl_cdr1_headers(policy),
        &mut DepCollector::default(),
    )
    .map_err(plan_refusal)
}

/// Whether the flag chooses this member's header form, from the two plans
/// a per-sample decision stands between.
///
/// `framed` is the plan the codec is written from — the unresolved key's,
/// which frames short because the choice belongs to the sample and not to
/// the type — `below` the one a sample under the step gets and `past` the
/// one a sample over it gets. Where `below` and `past` agree, the form is
/// fixed and nothing is threaded; where they differ in `extended` alone,
/// the flag picks; anything else is a case `zd_extended` cannot express
/// and is refused rather than answered with one of the two plans.
///
/// `framed` is checked against `below` too. The unresolved policy is
/// neither of the two, so a member it framed differently from a small
/// sample would mean the two are not the ends of one decision.
///
/// # Errors
/// [`EncodeError::AmbiguousHeader`] naming the member when two of the
/// three plans disagree about more than `extended`.
fn header_is_chosen(
    member: &str,
    framed: MemberHeader,
    below: MemberHeader,
    past: MemberHeader,
) -> Result<bool, EncodeError> {
    let refuse = || ambiguous(member, framed, below, past);
    if below == past {
        if framed == below {
            return Ok(false);
        }
        return Err(refuse());
    }
    // `MemberHeader`'s own equality has already settled that the two
    // differ in nothing but `extended`, so what is left is which way and
    // which header: both forms of a PL_CDR1 parameter header take the
    // same branch, `write_pid` below the step and `write_pid_extended`
    // past it, and nothing else does.
    match (below, past) {
        (
            MemberHeader::PidCdr1 {
                extended: false, ..
            },
            MemberHeader::PidCdr1 { extended: true, .. },
        )
        | (
            MemberHeader::OptionalCdr1 {
                extended: false, ..
            },
            MemberHeader::OptionalCdr1 { extended: true, .. },
        ) if framed == below => Ok(true),
        _ => Err(refuse()),
    }
}

/// The refusal [`header_is_chosen`] makes, naming all three plans.
fn ambiguous(
    member: &str,
    framed: MemberHeader,
    below: MemberHeader,
    past: MemberHeader,
) -> EncodeError {
    EncodeError::AmbiguousHeader {
        member: member.to_string(),
        detail: format!(
            "the codec's own plan frames it {framed:?}, a sample within RTI's short-header step \
             {below:?} and a sample past it {past:?}, and one flag cannot choose between them"
        ),
    }
}

/// Every member of `type_` whose PL_CDR1 header form the sample decides,
/// under `framed`.
///
/// [`StepHeaders::none`] for every plan outside [`per_sample`], so a
/// dialect whose header form is fixed never pays for two more plans and
/// generates exactly the code it generated before.
///
/// # Errors
/// [`EncodeError`] when one of the two plans cannot be made, when they do
/// not name the same members as `framed`, or when they differ in anything
/// but `extended`.
fn step_headers(
    ast: &Ast,
    type_: Id<TypeNode>,
    key: LayoutKey,
    framed: &TypeLayout,
) -> Result<StepHeaders, EncodeError> {
    if !per_sample(key) {
        return Ok(StepHeaders::none());
    }
    let name = ast
        .type_(type_)
        .map_or_else(|_| "<unresolved>".to_string(), |node| node.name.clone());
    let below = plan_under(ast, type_, key, PlCdr1HeaderPolicy::Specification)?;
    let past = plan_under(ast, type_, key, PlCdr1HeaderPolicy::ExtendedForConstructed)?;
    if below.members.len() != framed.members.len() || past.members.len() != framed.members.len() {
        return Err(EncodeError::NoLayout {
            detail: format!(
                "`{name}`: the PL_CDR1 plans for the two sides of RTI's step name {} and {} \
                 members where the codec's own plan names {}",
                below.members.len(),
                past.members.len(),
                framed.members.len()
            ),
        });
    }
    let mut members = Vec::with_capacity(framed.members.len());
    for ((own, below), past) in framed.members.iter().zip(&below.members).zip(&past.members) {
        let member = member_name(ast, type_, own.member);
        members.push((
            own.member,
            header_is_chosen(&member, own.header, below.header, past.header)?,
        ));
    }
    let discriminator = match (
        framed.discriminator,
        below.discriminator,
        past.discriminator,
    ) {
        (None, None, None) => false,
        (Some(framed), Some(below), Some(past)) => {
            header_is_chosen(&format!("{name}::discriminator"), framed, below, past)?
        }
        _ => {
            return Err(EncodeError::NoLayout {
                detail: format!(
                    "`{name}`: the PL_CDR1 plans for the two sides of RTI's step disagree about \
                     whether its discriminator carries a header at all"
                ),
            });
        }
    };
    Ok(StepHeaders {
        per_sample: true,
        members,
        discriminator,
    })
}

/// The name a member node has inside `type_`, for an error that names it.
fn member_name(ast: &Ast, type_: Id<TypeNode>, member: NodeId) -> String {
    ast.all_members_of(type_)
        .into_iter()
        .find(|id| ast.member(*id).is_ok_and(|node| node.node_id == member))
        .and_then(|id| ast.member(id).ok())
        .map_or_else(|| alloc_format(&member), |node| node.name.clone())
}

/// Build the `encode` and `decode` functions for one type, with the
/// `Default` impl and — for a keyed type — the key-holder pair.
///
/// This column writes a member's collection where the member is, in the
/// parent's own body and through a closure, and never hoists it into a
/// routine named after its element type. So the bound-in-the-name
/// defect the other columns carry — `sequence<long>` and
/// `sequence<long, 4>` sharing one hoisted routine, the first declared
/// deciding whether the check exists — cannot reach here: every bound
/// travels as an argument at the member's own call site
/// (`write_string_bounded`, `write_sequence_bounded`, and the `fit_…`
/// pair on the read side). Measured on
/// `@final struct S { sequence<long> b; sequence<long, 4> a; };` in
/// both declaration orders: both members keep exactly the bound their
/// declaration gives them. `crates/lang-rules/src/emit/zig.rs`'s
/// `bound_tag` is the fix in the columns that do hoist.
///
/// # Errors
/// [`EncodeError`] when the layout cannot be planned or a member has no
/// writer — never a body that leaves a member out.
pub fn rust_codec(
    ast: &Ast,
    type_: Id<TypeNode>,
    key: LayoutKey,
) -> Result<Vec<rust::Item>, EncodeError> {
    codec_items(ast, type_, key, Companions::Emit)
}

/// Build only the `encode` and `decode` pair for one type.
///
/// For a **second** plan over a type that already has one: the `Default`
/// impl and the key holder do not belong to an encoding and would be
/// emitted twice under the same name, which Rust refuses. The key holder
/// in particular is not a per-encoding thing at all — XTypes 1.3 §7.6.8
/// fixes it as PLAIN_CDR2 big-endian whatever the sample's encoding is —
/// so a second copy of it would be the same bytes under a name that
/// suggests otherwise.
///
/// # Errors
/// As [`rust_codec`].
pub fn rust_codec_pair(
    ast: &Ast,
    type_: Id<TypeNode>,
    key: LayoutKey,
) -> Result<Vec<rust::Item>, EncodeError> {
    codec_items(ast, type_, key, Companions::Omit)
}

/// Whether [`rust_codec`] gives a type of this kind a codec of its own:
/// an aggregate (structure, union, valuetype), an enumeration, a bitmask
/// or a bitset. A bitmask's answer is empty, since its Rust form is an
/// alias of a primitive and its member site writes the holder. Every
/// other kind — a primitive, a string, a collection, an alias — is
/// written where it is used and has no `impl` to hang a pair on.
#[must_use]
pub const fn has_codec(kind: &TypeKind) -> bool {
    matches!(
        kind,
        TypeKind::Structure { .. }
            | TypeKind::Union { .. }
            | TypeKind::Valuetype { .. }
            | TypeKind::Enumeration { .. }
            | TypeKind::Bitmask { .. }
            | TypeKind::Bitset { .. }
    )
}

/// Whether a codec run also emits what does not belong to an encoding.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Companions {
    /// The `Default` impl and the key holder come with the codec.
    Emit,
    /// Only the codec pair.
    Omit,
}

fn codec_items(
    ast: &Ast,
    type_: Id<TypeNode>,
    key: LayoutKey,
    companions: Companions,
) -> Result<Vec<rust::Item>, EncodeError> {
    axes_the_rust_codec_carries(key)?;
    // Which set of names this run writes, and — through
    // `MemberWrite::Nested` — which set it calls on a member. The two
    // have to be the same set: a nested call that named the XCDR2
    // encoder from inside the XCDR1 body would frame the member under
    // version 2 in a version 1 stream, and the sample would decode into
    // wrong values with nothing reporting it.
    let codec = match companions {
        Companions::Emit => "",
        Companions::Omit => SECONDARY_SUFFIX,
    };
    // Asked for a kind with no codec of its own, the generic path below
    // emitted `impl long { … }` or `impl Count { … }` for a
    // `typedef long Count;` — not Rust (E0390), and an `encode` that wrote
    // nothing. Every caller filtered the kinds it asked for; the refusal
    // is here so none has to.
    let node = ast.type_(type_).map_err(|error| EncodeError::NoLayout {
        detail: alloc_format(&error),
    })?;
    if !has_codec(&node.kind) {
        return Err(EncodeError::NoWriter {
            member: node.name.clone(),
            kind: "a type with no codec of its own; it is written where it is used",
        });
    }
    let mut deps = DepCollector::default();
    let layout =
        zerodds_ast_layout::xcdr::plan(ast, type_, key, &mut deps).map_err(plan_refusal)?;
    // Which of this type's members the sample's PL_CDR1 step decides. One
    // set per codec, shared by every encoder below, because they are all
    // the same type under the same plan.
    let steps = step_headers(ast, type_, key, &layout)?;

    // An enumeration has no layout **members**, and the generic path
    // below walks exactly those. It therefore produced
    //
    //     pub fn encode(&self, w: &mut Writer) -> Result<(), WireError> { Ok(()) }
    //     pub fn decode(r: &mut Reader) -> Result<Self, WireError> { Ok(Self { }) }
    //
    // — an encoder that puts zero octets on the wire for a value that
    // has a wire form, and a decoder that is not Rust at all: `Self { }`
    // is a struct literal and the type is an enum. It was invisible
    // because an enumeration is normally reached as a *member*, where
    // `MemberWrite::Enumeration` writes its holder, and nothing in the
    // tree asked an enumeration for its own codec until a CORBA
    // operation took one as a parameter — an argument has no enclosing
    // aggregate to be a member of.
    if let TypeKind::Enumeration { enumerators } = &node.kind {
        let mut items = enumeration_encode_fns(ast, type_, enumerators, key, codec)?;
        items.push(enumeration_decode_fn(ast, type_, enumerators, key, codec)?);
        return Ok(vec![rust::Item::Impl(rust::Impl {
            doc: Doc::none(),
            attrs: Vec::new(),
            generics: Vec::new(),
            trait_: None,
            self_type: rust::Type::named(Path::single(&node.name)),
            items,
        })]);
    }

    // A bitmask has **no** codec of its own, and cannot have one.
    // `crate::adapter::cst` materialises it as `pub type Flags = u16;`,
    // and `impl Flags` on a type alias to a primitive is E0390 —
    // "cannot define inherent `impl` for primitive types". The generic
    // path below emitted exactly that, with an `encode` that put zero
    // octets on the wire and a `decode` returning `Ok(Self { })`: a
    // file that did not compile, so a model with a `bitmask` in it had
    // no Rust output at all. `MemberWrite::Bitmask` writes the holder
    // at the member site instead, which is why nothing has to be
    // emitted here and why an empty answer is not a member left out.
    //
    // The name stays in `generate::rust_codec_pairs`' list because the
    // other twenty columns do give a bitmask a codec function; this is
    // the one column whose language mapping has nowhere to put it.
    if matches!(node.kind, TypeKind::Bitmask { .. }) {
        return Ok(Vec::new());
    }

    // A bitset has a codec and had an empty one. It materialises as a
    // struct with one field per named bitfield, so there is an `impl`
    // to hang the pair on — but a bitset's layout has no **members**,
    // and the generic path walks exactly those. What came out was an
    // `encode` that wrote nothing at all for a type with a wire form
    // and a `decode` returning `Ok(Self { })` for a struct with two
    // fields. The same hole as the enumeration's above, and the same
    // fix: the construct's own pair.
    if matches!(node.kind, TypeKind::Bitset { .. }) {
        let mut items = bitset_encode_fns(ast, type_, &layout, key, codec)?;
        items.push(bitset_decode_fn(ast, type_, &layout, codec)?);
        let mut out = vec![rust::Item::Impl(rust::Impl {
            doc: Doc::none(),
            attrs: Vec::new(),
            generics: Vec::new(),
            trait_: None,
            self_type: rust::Type::named(Path::single(&node.name)),
            items,
        })];
        if companions == Companions::Emit
            && let Some(default) = default_impl(ast, type_)?
        {
            out.push(default);
        }
        return Ok(out);
    }

    // A union has the same shape of hole as an enumeration, one level
    // up: `crate::adapter::cst` materialises it as a Rust enum with one
    // payload variant per case, and the generic path below builds a
    // struct literal over the layout's members. It produced an `encode`
    // that wrote nothing and a `decode` that returned `Ok(Self { })`.
    if let TypeKind::Union {
        discriminator,
        cases,
        ..
    } = &node.kind
    {
        let items = union_codec(
            ast,
            type_,
            &layout,
            &steps,
            discriminator,
            cases,
            key,
            codec,
        )?;
        let mut out = vec![rust::Item::Impl(rust::Impl {
            doc: Doc::none(),
            attrs: Vec::new(),
            generics: Vec::new(),
            trait_: None,
            self_type: rust::Type::named(Path::single(&node.name)),
            items,
        })];
        // A union returns from here and not through the tail, so its
        // `impl Default` has to be added on this path too. It was not,
        // and the omission was silent: the type simply had no `Default`,
        // which shows up wherever a caller writes `Default::default()`
        // for a member of one.
        if companions == Companions::Emit
            && let Some(default) = default_impl(ast, type_)?
        {
            out.push(default);
        }
        return Ok(out);
    }

    let mut items = encode_fns(ast, type_, &layout, &steps, key, codec)?;
    items.push(decode_fn(ast, type_, &layout, key, codec)?);
    if companions == Companions::Emit {
        items.append(&mut key_holder_fns(ast, type_, key)?);
    }

    let mut out = vec![rust::Item::Impl(rust::Impl {
        doc: Doc::none(),
        attrs: Vec::new(),
        generics: Vec::new(),
        trait_: None,
        self_type: rust::Type::named(Path::single(&node.name)),
        items,
    })];
    // The specification's default values, as an `impl Default`. A member
    // that has none — a type this column cannot give a default for —
    // leaves the impl out rather than substituting one.
    if companions == Companions::Emit
        && let Some(default) = default_impl(ast, type_)?
    {
        out.push(default);
    }
    Ok(out)
}

/// `encode` and `decode` for a union. §7.4.3.5 rule (10): the
/// discriminator, then the member the discriminator selects — and around
/// them the framing the plan gives the union's extensibility: rule (11)'s
/// DHEADER for an `@appendable` one, rules (27) and (28)'s member headers
/// for a `@mutable` one, the discriminator under member id 0. This column
/// wrote rule (10) for every union, so an `@appendable` union went out
/// without the DHEADER the C, Java and C# columns and every vendor write
/// (measured: `idl-conformance/_interop` `mapenum`, a `sequence<Sel>` of
/// an `@appendable` union — 44 octets here, 48 in C).
///
/// The Rust form is `crate::adapter::cst`'s — an enum with one payload
/// variant per case — so `encode` matches the variant and writes the
/// label that selects it, and `decode` reads the discriminator and
/// matches its value back.
///
/// A discriminator value that selects no case and no `default:` leaves
/// the member absent (§7.4.3.5); it is read into the variant
/// `crate::rust_union::NO_MEMBER`, which holds the discriminator alone.
/// A variant that carries its discriminator is checked before it is
/// written: a pairing the type does not allow is refused.
#[allow(clippy::too_many_arguments)]
fn union_codec(
    ast: &Ast,
    type_: Id<TypeNode>,
    layout: &TypeLayout,
    steps: &StepHeaders,
    discriminator: &zerodds_ast_core::Ref<TypeNode>,
    cases: &[Id<zerodds_ast::CaseNode>],
    key: LayoutKey,
    codec: &'static str,
) -> Result<Vec<rust::Item>, EncodeError> {
    let node = ast.type_(type_).map_err(|error| EncodeError::NoLayout {
        detail: alloc_format(&error),
    })?;
    let name = node.name.clone();
    let discriminator = discriminator.resolved().ok_or(EncodeError::NoWriter {
        member: name.clone(),
        kind: "a union with an unresolved discriminator",
    })?;
    // The discriminator is written as its *value* and not through the
    // enumeration's own codec: what a union writes here is the label the
    // selected branch carries, which the emitter already has as an
    // integer. A member of an enumeration type is the other case and
    // goes through `<path>::encode` — see the arm in `write_of`.
    let discriminator_write = match ast.type_(discriminator).map(|d| &d.kind) {
        Ok(TypeKind::Enumeration { .. }) => MemberWrite::Enumeration {
            bits: zerodds_ast_layout::enumeration_bit_bound(ast, discriminator, key.enum_holder),
        },
        _ => write_of(
            ast,
            node.module,
            discriminator,
            key,
            codec,
            Position {
                behaviour: zerodds_ast::TryConstruct::Discard,
                written: None,
            },
        ),
    };

    // The union as this column holds it: which variant carries its
    // discriminator, whether one holds a discriminator alone, and how a
    // label is spelled. `crate::rust_union`, shared with the declaration
    // and the default.
    let shape = crate::rust_union::shape(ast, type_).map_err(|kind| EncodeError::NoWriter {
        member: name.clone(),
        kind,
    })?;
    let refuse_label = |_| EncodeError::NoWriter {
        member: name.clone(),
        kind: "a union label with no source form in this column",
    };
    // A label as the discriminator's *write* takes it: the integer an
    // enumeration's holder carries, the literal otherwise.
    let written_label = |value: i128| -> Result<rust::Expr, EncodeError> {
        Ok(match shape.kind {
            crate::rust_union::DiscriminatorKind::Enumeration(_) => {
                rust::Expr::Literal(Literal::int(value))
            }
            kind => crate::rust_union::label_literal(ast, node.module, kind, value)
                .map_err(refuse_label)?
                .expr(),
        })
    };
    // A label as a pattern over the discriminator a variant carries, and
    // over the one `decode` reads: an enumeration's is converted to the
    // enumeration first.
    // An enumeration discriminator's own name, for a refusal that names it.
    let enumeration_name = |enumeration: Id<TypeNode>| {
        ast.type_(enumeration)
            .map_or_else(|_| name.clone(), |node| node.name.clone())
    };
    let label_pattern = |value: i128| -> Result<rust::Pattern, EncodeError> {
        Ok(
            crate::rust_union::label_literal(ast, node.module, shape.kind, value)
                .map_err(refuse_label)?
                .pattern(),
        )
    };
    let refused =
        |what: String, sink: &str| rust::Expr::Return(Some(Box::new(refused_err(sink, &what))));
    // A carried discriminator as a match scrutinee: the binding, which a
    // `match` on `&self` makes a reference, dereferenced for the copyable
    // primitives; an enumeration is matched through the reference.
    let carried_scrutinee = || match shape.kind {
        crate::rust_union::DiscriminatorKind::Enumeration(_) => {
            rust::Expr::Path(Path::single("discriminator"))
        }
        _ => rust::Expr::Deref(Box::new(rust::Expr::Path(Path::single("discriminator")))),
    };
    // A carried discriminator written: an enumeration through its holder
    // integer, every other kind as itself.
    let write_carried = || -> Result<rust::Expr, EncodeError> {
        match shape.kind {
            crate::rust_union::DiscriminatorKind::Enumeration(enumeration) => {
                let mut arms = Vec::new();
                for enumerator in ast.enumerators_of(enumeration) {
                    // The enumerator by name, and the integer the wire
                    // carries for it — which the setting decides.
                    let (_, value) = enumerator_variant_and_value(
                        ast,
                        &enumeration_name(enumeration),
                        *enumerator,
                        key,
                    )?;
                    arms.push(rust::MatchArm {
                        pattern: rust::Pattern::Path(
                            crate::rust_union::enumerator_path_of(
                                ast,
                                node.module,
                                enumeration,
                                *enumerator,
                            )
                            .map_err(refuse_label)?,
                        ),
                        guard: None,
                        body: rust::Expr::Literal(Literal::int(value)),
                    });
                }
                write_expr_for(
                    &discriminator_write,
                    rust::Expr::Match {
                        scrutinee: Box::new(rust::Expr::Path(Path::single("discriminator"))),
                        arms,
                    },
                    &name,
                    false,
                )
            }
            _ => write_expr_for(
                &discriminator_write,
                rust::Expr::Path(Path::single("discriminator")),
                &name,
                true,
            ),
        }
    };
    // The check ahead of a carried discriminator's write: one of `own`
    // where the variant is a case with several labels; none of every
    // case's labels where it is the `default:` case or the variant that
    // holds no member. A pairing the type does not allow is refused, not
    // written: a peer would read another branch than the one the value
    // names.
    let check_carried =
        |own: Option<&[i128]>, what: String| -> Result<rust::Statement, EncodeError> {
            let mut arms = Vec::new();
            match own {
                Some(labels) => {
                    for value in labels {
                        arms.push(rust::MatchArm {
                            pattern: label_pattern(*value)?,
                            guard: None,
                            body: rust::Expr::Block(rust::Block {
                                statements: Vec::new(),
                            }),
                        });
                    }
                    arms.push(rust::MatchArm {
                        pattern: rust::Pattern::Wildcard,
                        guard: None,
                        body: refused(what, "w"),
                    });
                }
                None => {
                    for value in shape.all_labels() {
                        arms.push(rust::MatchArm {
                            pattern: label_pattern(value)?,
                            guard: None,
                            body: refused(what.clone(), "w"),
                        });
                    }
                    arms.push(rust::MatchArm {
                        pattern: rust::Pattern::Wildcard,
                        guard: None,
                        body: rust::Expr::Block(rust::Block {
                            statements: Vec::new(),
                        }),
                    });
                }
            }
            Ok(rust::Statement::Semi(rust::Expr::Match {
                scrutinee: Box::new(carried_scrutinee()),
                arms,
            }))
        };

    // The discriminator's header, as a plan the header helpers take: only
    // a `@mutable` union has one, under member id 0.
    let discriminator_plan = layout.discriminator.map(|header| MemberPlan {
        member: layout.type_,
        member_id: 0,
        header,
        offset: zerodds_ast_layout::plan::Offset::Dynamic,
        align: 1,
    });

    let mut encode_arms = Vec::new();
    let mut decode_arms = Vec::new();
    let mut default_arm = None;

    for case in cases {
        let case_node = ast.case(*case).map_err(|error| EncodeError::NoLayout {
            detail: alloc_format(&error),
        })?;
        let member = ast
            .member(case_node.member)
            .map_err(|error| EncodeError::NoLayout {
                detail: alloc_format(&error),
            })?;
        let rust_case = shape
            .cases
            .iter()
            .find(|c| c.member.node_id == member.node_id)
            .ok_or(EncodeError::NoWriter {
                member: member.name.clone(),
                kind: "a union case the shape does not hold",
            })?;
        let variant = rust_case.variant.clone();
        let carries = rust_case.carries_discriminator();
        let member_type = member.type_.resolved().ok_or(EncodeError::NoWriter {
            member: member.name.clone(),
            kind: "a union case with an unresolved type",
        })?;
        let member_write = write_of(
            ast,
            node.module,
            member_type,
            key,
            codec,
            Position {
                behaviour: ast.effective_try_construct(None, Some(member_type)),
                written: None,
            },
        );
        let member_write = external_write(ast, member, member_write);
        let label_values = rust_case.labels.clone();
        let is_default = rust_case.is_default;

        // The case's own header, which only a `@mutable` union's plan has.
        let case_plan = layout
            .members
            .iter()
            .find(|plan| plan.member == member.node_id)
            .copied();

        // `Self::Variant(value)`, or `Self::Variant(discriminator, value)`
        // where the variant carries what `decode` read.
        let construct = |value: rust::Expr| {
            let mut args = Vec::new();
            if carries {
                args.push(rust::Expr::Path(Path::single("discriminator")));
            }
            args.push(value);
            rust::Expr::Call {
                callee: Box::new(rust::Expr::Path(Path::single("Ok"))),
                args: vec![rust::Expr::Call {
                    callee: Box::new(rust::Expr::Path(Path::new(&["Self", &variant]))),
                    args,
                }],
            }
        };
        let read_member = read_for(&member.name, &member_write)?;
        let build = match case_plan {
            None => construct(rust::Expr::Try(Box::new(read_member))),
            Some(plan) => {
                let mut statements = read_header(&plan);
                statements.push(let_statement(
                    "value",
                    rust::Expr::Try(Box::new(read_member)),
                ));
                statements.extend(finish_read_header(&plan));
                statements.push(rust::Statement::Tail(construct(rust::Expr::Path(
                    Path::single("value"),
                ))));
                rust::Expr::Block(rust::Block { statements })
            }
        };

        if is_default {
            default_arm = Some(build.clone());
        } else {
            for value in &label_values {
                decode_arms.push(rust::MatchArm {
                    pattern: label_pattern(*value)?,
                    guard: None,
                    body: build.clone(),
                });
            }
        }

        // `Self::Variant(value) => { <discriminator write>; <member write>; }`
        // with the discriminator the case fixes, or
        // `Self::Variant(discriminator, value) => { <check>; <discriminator
        // write>; <member write>; }` with the one the variant carries.
        let mut statements = Vec::new();
        if carries {
            let own = (!is_default).then_some(label_values.as_slice());
            let what = if is_default {
                format!("a discriminator no label of `{name}` takes, for its `default:` member")
            } else {
                format!("a label of the member `{}` of `{name}`", member.name)
            };
            statements.push(check_carried(own, what)?);
        }
        if let Some(plan) = &discriminator_plan {
            statements.extend(open_header(plan, steps.discriminator));
        }
        let discriminator_write_expr = if carries {
            write_carried()?
        } else {
            // A case that does not carry its discriminator has exactly one
            // label: `carries_discriminator` is true for every other.
            let label = label_values.first().copied().ok_or(EncodeError::NoWriter {
                member: member.name.clone(),
                kind: "a union case carrying no case label",
            })?;
            write_expr_for(&discriminator_write, written_label(label)?, &name, false)?
        };
        statements.push(rust::Statement::Semi(discriminator_write_expr));
        if let Some(plan) = &discriminator_plan {
            statements.extend(close_header(plan));
        }
        if let Some(plan) = &case_plan {
            statements.extend(open_header(plan, steps.member(plan, &member.name)?));
        }
        // `value` is the variant's binding, which a `match` on `&self`
        // makes a reference.
        statements.push(rust::Statement::Semi(write_expr_for(
            &member_write,
            rust::Expr::Path(Path::single("value")),
            &member.name,
            true,
        )?));
        if let Some(plan) = &case_plan {
            statements.extend(close_header(plan));
        }
        let mut elements = Vec::new();
        if carries {
            elements.push(rust::Pattern::Binding(Ident::new("discriminator")));
        }
        elements.push(rust::Pattern::Binding(Ident::new("value")));
        encode_arms.push(rust::MatchArm {
            pattern: rust::Pattern::TupleStruct {
                path: Path::new(&["Self", &variant]),
                elements,
            },
            guard: None,
            body: rust::Expr::Block(rust::Block { statements }),
        });
    }

    // The variant that holds a discriminator selecting no member
    // (§7.4.3.5: the member is absent): the discriminator and nothing
    // after it, under the same check as the `default:` case's.
    if shape.no_member {
        let mut statements = vec![check_carried(
            None,
            format!("a discriminator no label of `{name}` takes, for no member"),
        )?];
        if let Some(plan) = &discriminator_plan {
            statements.extend(open_header(plan, steps.discriminator));
        }
        statements.push(rust::Statement::Semi(write_carried()?));
        if let Some(plan) = &discriminator_plan {
            statements.extend(close_header(plan));
        }
        encode_arms.push(rust::MatchArm {
            pattern: rust::Pattern::TupleStruct {
                path: Path::new(&["Self", crate::rust_union::NO_MEMBER]),
                elements: vec![rust::Pattern::Binding(Ident::new("discriminator"))],
            },
            guard: None,
            body: rust::Expr::Block(rust::Block { statements }),
        });
    }

    // Past the labels: the `default:` case, or the variant that holds no
    // member. A Rust `char` has more values than an IDL `char` or `wchar`
    // labels can cover, so a character discriminator always has the arm;
    // an integer, `bool` or enumeration discriminator whose labels cover
    // its type has no value left for it.
    let rest = default_arm.or_else(|| {
        shape.no_member.then(|| rust::Expr::Call {
            callee: Box::new(rust::Expr::Path(Path::single("Ok"))),
            args: vec![rust::Expr::Call {
                callee: Box::new(rust::Expr::Path(Path::new(&[
                    "Self",
                    crate::rust_union::NO_MEMBER,
                ]))),
                args: vec![rust::Expr::Path(Path::single("discriminator"))],
            }],
        })
    });
    match rest {
        Some(body) => decode_arms.push(rust::MatchArm {
            pattern: rust::Pattern::Wildcard,
            guard: None,
            body,
        }),
        None if shape.kind == crate::rust_union::DiscriminatorKind::Character => {
            decode_arms.push(refusal_arm(&format!(
                "a discriminator selecting a case of `{name}`"
            )));
        }
        None => {}
    }

    // An enumeration discriminator is read as its holder integer and
    // turned into the enumerator here, so the arms above match — and the
    // variants carry — the enumeration and not its holder. A holder no
    // enumerator has is refused, as the enumeration's own `decode` does.
    let to_enumeration = match shape.kind {
        crate::rust_union::DiscriminatorKind::Enumeration(enumeration) => {
            let mut arms = Vec::new();
            for enumerator in ast.enumerators_of(enumeration) {
                let (_, value) = enumerator_variant_and_value(
                    ast,
                    &enumeration_name(enumeration),
                    *enumerator,
                    key,
                )?;
                arms.push(rust::MatchArm {
                    pattern: rust::Pattern::Literal(Literal::int(value)),
                    guard: None,
                    body: rust::Expr::Path(
                        crate::rust_union::enumerator_path_of(
                            ast,
                            node.module,
                            enumeration,
                            *enumerator,
                        )
                        .map_err(refuse_label)?,
                    ),
                });
            }
            arms.push(rust::MatchArm {
                pattern: rust::Pattern::Wildcard,
                guard: None,
                body: refused(
                    format!("an enumerator of the discriminator of `{name}`"),
                    "r",
                ),
            });
            Some(rust::Statement::Let {
                name: Ident::new("discriminator"),
                type_: None,
                value: Some(rust::Expr::Match {
                    scrutinee: Box::new(rust::Expr::Path(Path::single("discriminator"))),
                    arms,
                }),
                mutable: false,
            })
        }
        _ => None,
    };

    let mut encode_statements = Vec::new();
    if per_sample(key) {
        // As in `encode_fns`: the flag is read only by a case member or
        // by the discriminator whose header form the sample decides.
        encode_statements.push(rust::Statement::Let {
            name: Ident::new("_"),
            type_: None,
            value: Some(rust::Expr::Path(Path::single(EXTENDED_PARAM))),
            mutable: false,
        });
    }
    if layout.needs_dheader {
        encode_statements.push(let_statement("dheader", {
            rust::Expr::MethodCall {
                receiver: Box::new(rust::Expr::Path(Path::single("w"))),
                method: Ident::new("write_dheader"),
                args: Vec::new(),
            }
        }));
    }
    encode_statements.push(rust::Statement::Semi(rust::Expr::Match {
        scrutinee: Box::new(rust::Expr::Path(Path::single("self"))),
        arms: encode_arms,
    }));
    if layout.needs_sentinel {
        encode_statements.push(call_statement("write_sentinel", Vec::new()));
    }
    if layout.needs_dheader {
        encode_statements.push(call_statement(
            "finish_dheader",
            vec![rust::Expr::Path(Path::single("dheader"))],
        ));
    }
    encode_statements.push(rust::Statement::Tail(rust::Expr::Call {
        callee: Box::new(rust::Expr::Path(Path::single("Ok"))),
        args: vec![rust::Expr::Tuple(Vec::new())],
    }));
    // As in `encode_fns`: under `ElementPadding::Packed` a union value is
    // its own alignment block. See `zerodds-wire-codec`'s `write_union`.
    let encode_statements = wrap_packed_body(
        encode_statements,
        "w",
        key.element_padding == ElementPadding::Packed,
    );
    let encode = encode_function(&body_name(codec, key), &encode_statements, per_sample(key));

    let decode = rust::Item::Function(rust::Function {
        doc: Doc::none(),
        attrs: Vec::new(),
        visibility: Visibility::Public,
        name: Ident::new(&format!("decode{codec}")),
        generics: Vec::new(),
        params: vec![rust::Param {
            name: Some(Ident::new("r")),
            type_: rust::Type::Ref {
                target: Box::new(rust::Type::named(Path::single("Reader"))),
                mutable: true,
                lifetime: None,
            },
            receiver: None,
        }],
        returns: Some(rust::Type::generic(
            Path::single("Result"),
            vec![
                rust::Type::named(Path::single("Self")),
                rust::Type::named(Path::single("WireError")),
            ],
        )),
        body: Some(rust::Block {
            statements: wrap_packed_body(
                union_decode_body(
                    layout,
                    discriminator_plan,
                    &discriminator_write,
                    to_enumeration,
                    decode_arms,
                )?,
                "r",
                key.element_padding == ElementPadding::Packed,
            ),
        }),
    });

    let mut out = with_entry(encode, encode_entry(ast, type_, key, codec)?);
    out.push(decode);
    Ok(out)
}

/// The body of a union's `decode`: the framing the plan gives it around
/// the discriminator's read and the match that reads the branch.
///
/// A `@final` union keeps the shape it always had — the discriminator,
/// then the match as the tail — so its generated code does not change.
fn union_decode_body(
    layout: &TypeLayout,
    discriminator_plan: Option<MemberPlan>,
    discriminator_write: &MemberWrite,
    to_enumeration: Option<rust::Statement>,
    decode_arms: Vec<rust::MatchArm>,
) -> Result<Vec<rust::Statement>, EncodeError> {
    let read_discriminator = rust::Statement::Let {
        name: Ident::new("discriminator"),
        type_: None,
        value: Some(rust::Expr::Try(Box::new(read_for(
            "discriminator",
            discriminator_write,
        )?))),
        mutable: false,
    };
    let branch = rust::Expr::Match {
        scrutinee: Box::new(rust::Expr::Path(Path::single("discriminator"))),
        arms: decode_arms,
    };
    if !layout.needs_dheader && !layout.needs_sentinel && discriminator_plan.is_none() {
        let mut statements = vec![read_discriminator];
        statements.extend(to_enumeration);
        statements.push(rust::Statement::Tail(branch));
        return Ok(statements);
    }

    let mut statements = Vec::new();
    if layout.needs_dheader {
        // Where the writer's union ends: a newer writer's branch may
        // carry more than this reader's type knows, and the reader steps
        // over it rather than reading the next value from inside it.
        statements.push(let_statement(
            "end",
            rust::Expr::Binary {
                op: rust::BinaryOp::Add,
                lhs: Box::new(rust::Expr::Try(Box::new(call("read_dheader", Vec::new())))),
                rhs: Box::new(call("position", Vec::new())),
            },
        ));
    }
    if let Some(plan) = &discriminator_plan {
        statements.extend(read_header(plan));
    }
    statements.push(read_discriminator);
    if let Some(plan) = &discriminator_plan {
        statements.extend(finish_read_header(plan));
    }
    statements.extend(to_enumeration);
    statements.push(let_statement("value", rust::Expr::Try(Box::new(branch))));
    if layout.needs_sentinel {
        statements.extend(read_sentinel());
    }
    if layout.needs_dheader {
        statements.push(rust::Statement::Semi(rust::Expr::Try(Box::new(call(
            "skip_to",
            vec![rust::Expr::Path(Path::single("end"))],
        )))));
    }
    statements.push(rust::Statement::Tail(rust::Expr::Call {
        callee: Box::new(rust::Expr::Path(Path::single("Ok"))),
        args: vec![rust::Expr::Path(Path::single("value"))],
    }));
    Ok(statements)
}

/// Rule (23)'s `PID_SENTINEL` at the end of a PL_CDR1 parameter list,
/// read and checked: a parameter list that ends anywhere else is refused.
fn read_sentinel() -> Vec<rust::Statement> {
    vec![
        let_statement(
            "sentinel",
            rust::Expr::Try(Box::new(call("read_pid", Vec::new()))),
        ),
        rust::Statement::Semi(rust::Expr::If {
            condition: Box::new(rust::Expr::Not(Box::new(rust::Expr::Field {
                receiver: Box::new(rust::Expr::Path(Path::single("sentinel"))),
                name: Ident::new("end"),
            }))),
            then: rust::Block {
                statements: vec![rust::Statement::Semi(rust::Expr::Return(Some(Box::new(
                    rust::Expr::Call {
                        callee: Box::new(rust::Expr::Path(Path::single("Err"))),
                        args: vec![rust::Expr::Call {
                            callee: Box::new(rust::Expr::Path(Path::new(&[
                                "WireError",
                                "refused",
                            ]))),
                            args: vec![
                                call("position", Vec::new()),
                                rust::Expr::Literal(Literal::string(
                                    "PID_SENTINEL closing the parameter list, per rule (23)",
                                )),
                            ],
                        }],
                    },
                ))))],
            },
            otherwise: None,
        }),
    ]
}

/// A member header's read, before the value, for a member read where
/// the stream puts it — the decoder's half of [`open_header`].
fn read_header(plan: &MemberPlan) -> Vec<rust::Statement> {
    match plan.header {
        MemberHeader::NoHeader | MemberHeader::OptionalFlag => Vec::new(),
        MemberHeader::PidCdr1 { .. } | MemberHeader::OptionalCdr1 { .. } => vec![let_statement(
            "pid",
            rust::Expr::Try(Box::new(call("read_pid", Vec::new()))),
        )],
        MemberHeader::EmHeader { .. } => vec![let_statement(
            "_emheader",
            rust::Expr::Try(Box::new(call("read_emheader", Vec::new()))),
        )],
    }
}

/// What closes a header after its value on the read side — the
/// decoder's half of [`close_header`].
fn finish_read_header(plan: &MemberPlan) -> Vec<rust::Statement> {
    match plan.header {
        MemberHeader::PidCdr1 { .. } | MemberHeader::OptionalCdr1 { .. } => {
            vec![rust::Statement::Semi(rust::Expr::Try(Box::new(call(
                "finish_pid",
                vec![rust::Expr::Path(Path::single("pid"))],
            ))))]
        }
        MemberHeader::NoHeader | MemberHeader::OptionalFlag | MemberHeader::EmHeader { .. } => {
            Vec::new()
        }
    }
}

/// The write of one value, as an expression, with its `?` where the
/// write can refuse.
fn write_expr_for(
    kind: &MemberWrite,
    value: rust::Expr,
    at: &str,
    borrowed: bool,
) -> Result<rust::Expr, EncodeError> {
    let (expr, fallible) = write_call("w", SinkShape::Reference, at, kind, value, borrowed)?;
    Ok(if fallible {
        rust::Expr::Try(Box::new(expr))
    } else {
        expr
    })
}

/// The unsigned Rust type an enumeration's holder takes, and the
/// `Writer`/`Reader` method for it.
///
/// `zerodds_ast_layout::enumeration_bit_bound` decides the width, the
/// same crossing function the member position asks — a second table
/// here would be a second place for a holder to be the wrong size.
const fn enum_holder(bits: u16) -> (rust::Primitive, &'static str, &'static str) {
    match bits {
        0..=8 => (rust::Primitive::U8, "write_u8", "read_u8"),
        9..=16 => (rust::Primitive::U16, "write_u16", "read_u16"),
        17..=32 => (rust::Primitive::U32, "write_u32", "read_u32"),
        _ => (rust::Primitive::U64, "write_u64", "read_u64"),
    }
}

/// The encoder body for an enumeration: its holder, at the width its bit
/// bound gives, and — under [`per_sample`] — the public entry beside it.
///
/// An enumeration's body reads no [`EXTENDED_PARAM`] and never writes a
/// parameter header: `keeps_short_header` counts an enumeration among the
/// types that keep the short form in a sample past RTI's step. The body
/// takes the flag anyway because a `MemberWrite::Nested` call on an
/// enumeration names it, and the same name under every plan is what keeps
/// a call from framing an enumeration member with the wrong encoder.
///
/// A `match` and not `*self as uN`. The cast is a move, and
/// `crate::adapter::cst` derives nothing on what it declares — not
/// `Copy` either — so the cast form did not compile. The match is also
/// the honest one: it names each enumerator's wire value where a
/// reader can compare it with `decode`'s.
fn enumeration_encode_fns(
    ast: &Ast,
    type_: Id<TypeNode>,
    enumerators: &[Id<zerodds_ast::EnumeratorNode>],
    key: LayoutKey,
    codec: &'static str,
) -> Result<Vec<rust::Item>, EncodeError> {
    let node = ast.type_(type_).map_err(|error| EncodeError::NoLayout {
        detail: alloc_format(&error),
    })?;
    let bits = zerodds_ast_layout::enumeration_bit_bound(ast, type_, key.enum_holder);
    let (primitive, writer, _) = enum_holder(bits);

    let mut arms = Vec::new();
    for id in enumerators {
        let (name, value) = enumerator_variant_and_value(ast, &node.name, *id, key)?;
        arms.push(rust::MatchArm {
            pattern: rust::Pattern::Path(Path::new(&["Self", &name])),
            guard: None,
            body: rust::Expr::Literal(Literal::int(value)),
        });
    }

    let mut statements = Vec::new();
    if per_sample(key) {
        statements.push(rust::Statement::Let {
            name: Ident::new("_"),
            type_: None,
            value: Some(rust::Expr::Path(Path::single(EXTENDED_PARAM))),
            mutable: false,
        });
    }
    statements.extend([
        rust::Statement::Let {
            name: Ident::new("value"),
            type_: Some(rust::Type::Primitive(primitive)),
            value: Some(rust::Expr::Match {
                scrutinee: Box::new(rust::Expr::Path(Path::single("self"))),
                arms,
            }),
            mutable: false,
        },
        rust::Statement::Semi(rust::Expr::MethodCall {
            receiver: Box::new(rust::Expr::Path(Path::single("w"))),
            method: Ident::new(writer),
            args: vec![rust::Expr::Path(Path::single("value"))],
        }),
        rust::Statement::Tail(rust::Expr::Call {
            callee: Box::new(rust::Expr::Path(Path::single("Ok"))),
            args: vec![rust::Expr::Tuple(Vec::new())],
        }),
    ]);
    Ok(with_entry(
        encode_function(&body_name(codec, key), &statements, per_sample(key)),
        encode_entry(ast, type_, key, codec)?,
    ))
}

/// One enumerator's Rust variant name and the integer it serialises as.
///
/// The value goes through
/// [`zerodds_ast_layout::enumerator_wire_value`] and not through
/// `Ast::effective_enumerator_value`. The two answer differently under
/// [`zerodds_ast_layout::EnumeratorValue::Ordinal`], and this integer is
/// the one that leaves the process: `enumeration_encode_fn` puts it in
/// the holder and `enumeration_decode_fn` matches an incoming holder
/// against it. Reading the declared value here wrote `@value(200)`
/// where a front end that drops the annotation writes `1`, in both
/// directions, with nothing at run time saying so — the Rust column
/// carries no corpus row that reaches this code, so only
/// `crates/wire-probe/tests/enumerator_value.rs::the_sites_go_through_the_crossing`
/// stands between it and a silent bypass.
///
/// The Rust *discriminant* is a different question and stays declared:
/// `crate::adapter::cst` writes it, and it is the language mapping.
fn enumerator_variant_and_value(
    ast: &Ast,
    enumeration: &str,
    id: Id<zerodds_ast::EnumeratorNode>,
    key: LayoutKey,
) -> Result<(String, i128), EncodeError> {
    let enumerator = ast.enumerator(id).map_err(|error| EncodeError::NoLayout {
        detail: alloc_format(&error),
    })?;
    let value = zerodds_ast_layout::enumerator_wire_value(ast, id, key.enumerator_value)
        .ok_or_else(|| EncodeError::NoEnumeratorValue {
            enumeration: enumeration.to_string(),
            enumerator: enumerator.name.clone(),
            detail: "the enumerator has no computable wire value",
        })?;
    // `escape_keyword` and not `variant_name`: the declaration is
    // `crate::adapter::cst`'s and it writes the enumerator's own name.
    // `variant_name` capitalises, which is what a *union* case takes —
    // it is built from a member name — and using it here produced
    // `Self::Missing_node` for an enumerator declared `missing_node`.
    Ok((rust::escape_keyword(&enumerator.name), value))
}

/// `fn decode(r: &mut Reader) -> Result<Self, WireError>` for an
/// enumeration.
///
/// A value that names no enumerator is **refused** and not mapped onto
/// the first variant. §7.2.2.4.1.2 gives an enumeration a fixed set of
/// literals; a decoder that answered `RED` for a value nobody wrote
/// would hand the caller a sample that never existed, which is the
/// silent half of the drop class.
fn enumeration_decode_fn(
    ast: &Ast,
    type_: Id<TypeNode>,
    enumerators: &[Id<zerodds_ast::EnumeratorNode>],
    key: LayoutKey,
    codec: &'static str,
) -> Result<rust::Item, EncodeError> {
    let node = ast.type_(type_).map_err(|error| EncodeError::NoLayout {
        detail: alloc_format(&error),
    })?;
    let bits = zerodds_ast_layout::enumeration_bit_bound(ast, type_, key.enum_holder);
    let (_, _, reader) = enum_holder(bits);

    let mut arms = Vec::new();
    for id in enumerators {
        let (name, value) = enumerator_variant_and_value(ast, &node.name, *id, key)?;
        arms.push(rust::MatchArm {
            pattern: rust::Pattern::Literal(Literal::int(value)),
            guard: None,
            body: rust::Expr::Call {
                callee: Box::new(rust::Expr::Path(Path::single("Ok"))),
                args: vec![rust::Expr::Path(Path::new(&["Self", &name]))],
            },
        });
    }
    arms.push(refusal_arm(&format!("an enumerator of `{}`", node.name)));

    let statements = vec![
        rust::Statement::Let {
            name: Ident::new("value"),
            type_: None,
            value: Some(rust::Expr::Try(Box::new(rust::Expr::MethodCall {
                receiver: Box::new(rust::Expr::Path(Path::single("r"))),
                method: Ident::new(reader),
                args: Vec::new(),
            }))),
            mutable: false,
        },
        rust::Statement::Tail(rust::Expr::Match {
            scrutinee: Box::new(rust::Expr::Path(Path::single("value"))),
            arms,
        }),
    ];

    Ok(rust::Item::Function(rust::Function {
        doc: Doc::none(),
        attrs: Vec::new(),
        visibility: Visibility::Public,
        name: Ident::new(&format!("decode{codec}")),
        generics: Vec::new(),
        params: vec![rust::Param {
            name: Some(Ident::new("r")),
            type_: rust::Type::Ref {
                target: Box::new(rust::Type::named(Path::single("Reader"))),
                mutable: true,
                lifetime: None,
            },
            receiver: None,
        }],
        returns: Some(rust::Type::generic(
            Path::single("Result"),
            vec![
                rust::Type::named(Path::single("Self")),
                rust::Type::named(Path::single("WireError")),
            ],
        )),
        body: Some(rust::Block { statements }),
    }))
}

/// The anchor a bitset packs into, in octets.
///
/// The **plan's** answer and not a sum of the widths: an anchor is one,
/// two, four or eight octets whatever the bitfields add up to, and
/// `zerodds_ast_layout::xcdr::plan` is the crossing the other twenty
/// columns ask through `Extent::Sized`.
fn bitset_anchor(layout: &TypeLayout, name: &str) -> Result<u32, EncodeError> {
    match layout.extent {
        Extent::Sized(anchor) => Ok(anchor),
        Extent::Unbounded => Err(EncodeError::NoWriter {
            member: name.to_string(),
            kind: "bitset whose anchor the planner does not size",
        }),
    }
}

/// Each named bitfield with the width and the position it takes in the
/// anchor.
///
/// Positions run cumulatively from zero in declaration order, **padding
/// included**: an anonymous `bitfield<4>` carries no value and still
/// moves the next field along. The whole inheritance chain, base first,
/// because that is what the planner sizes the anchor over —
/// `crates/lang-rules/src/emit/encode.rs` reads the same list the same
/// way.
///
/// # Errors
/// [`EncodeError::NoWriter`] for a chain that runs past the sixty-four
/// bits an anchor holds — never a field silently dropped off the end.
fn bitset_fields(
    ast: &Ast,
    type_: Id<TypeNode>,
    name: &str,
) -> Result<Vec<(String, u16, u32)>, EncodeError> {
    let mut out = Vec::new();
    let mut position: u32 = 0;
    for field in &ast.all_bitfields_of(type_) {
        // Refused and not skipped: a skipped field takes its width out
        // of the running position, so every field after it would be
        // packed at an offset the peer does not read it from — a sample
        // that encodes, reports success and means something else.
        let Ok(field_node) = ast.bitfield(*field) else {
            return Err(EncodeError::NoWriter {
                member: name.to_string(),
                kind: "bitset with a stale bitfield handle",
            });
        };
        let width = u32::from(field_node.bits);
        if position.saturating_add(width) > 64 {
            return Err(EncodeError::NoWriter {
                member: name.to_string(),
                kind: "bitset wider than the sixty-four bits an anchor holds",
            });
        }
        if let Some(field_name) = field_node.name.as_ref() {
            out.push((
                rust::escape_keyword(field_name).to_string(),
                field_node.bits,
                position,
            ));
        }
        position = position.saturating_add(width);
    }
    Ok(out)
}

/// A bitset's encoder body — each field shifted into the anchor, then the
/// anchor — and, under [`per_sample`], the public entry beside it.
///
/// As `enumeration_encode_fns`'s: the body reads no [`EXTENDED_PARAM`]
/// and takes it anyway, because a `MemberWrite::Nested` call on a bitset
/// names it.
///
/// `Writer::bitfield` does the shift and refuses a value the field's
/// width cannot hold. The generated field is as wide as its **holder**
/// — `bitfield<3> a` is a `u8` — so a value that does not fit is a
/// program error the writer reports rather than an octet it masks off.
fn bitset_encode_fns(
    ast: &Ast,
    type_: Id<TypeNode>,
    layout: &TypeLayout,
    key: LayoutKey,
    codec: &'static str,
) -> Result<Vec<rust::Item>, EncodeError> {
    let node = ast.type_(type_).map_err(|error| EncodeError::NoLayout {
        detail: alloc_format(&error),
    })?;
    let anchor = bitset_anchor(layout, &node.name)?;
    let mut statements = Vec::new();
    if per_sample(key) {
        statements.push(rust::Statement::Let {
            name: Ident::new("_"),
            type_: None,
            value: Some(rust::Expr::Path(Path::single(EXTENDED_PARAM))),
            mutable: false,
        });
    }
    statements.push(rust::Statement::Let {
        name: Ident::new("bits"),
        type_: Some(rust::Type::Primitive(rust::Primitive::U64)),
        value: Some(rust::Expr::Literal(Literal::int(0))),
        mutable: true,
    });
    for (field, bits, position) in bitset_fields(ast, type_, &node.name)? {
        let value = rust::Expr::Field {
            receiver: Box::new(rust::Expr::Path(Path::single("self"))),
            name: Ident::new(&field),
        };
        statements.push(rust::Statement::Semi(rust::Expr::Assign {
            target: Box::new(rust::Expr::Path(Path::single("bits"))),
            value: Box::new(rust::Expr::Binary {
                op: rust::BinaryOp::BitOr,
                lhs: Box::new(rust::Expr::Path(Path::single("bits"))),
                rhs: Box::new(rust::Expr::Try(Box::new(rust::Expr::MethodCall {
                    receiver: Box::new(rust::Expr::Path(Path::single("w"))),
                    method: Ident::new("bitfield"),
                    args: vec![
                        widen_to_u64(value, bits),
                        rust::Expr::Literal(Literal::int(i128::from(bits))),
                        rust::Expr::Literal(Literal::int(i128::from(position))),
                    ],
                }))),
            }),
        }));
    }
    statements.push(rust::Statement::Semi(rust::Expr::Try(Box::new(
        rust::Expr::MethodCall {
            receiver: Box::new(rust::Expr::Path(Path::single("w"))),
            method: Ident::new("write_bitset"),
            args: vec![
                rust::Expr::Literal(Literal::int(i128::from(anchor))),
                rust::Expr::Path(Path::single("bits")),
            ],
        },
    ))));
    statements.push(rust::Statement::Tail(rust::Expr::Call {
        callee: Box::new(rust::Expr::Path(Path::single("Ok"))),
        args: vec![rust::Expr::Tuple(Vec::new())],
    }));
    Ok(with_entry(
        encode_function(&body_name(codec, key), &statements, per_sample(key)),
        encode_entry(ast, type_, key, codec)?,
    ))
}

/// A bitset's `decode`: the anchor, then each field shifted and masked
/// back out of it.
///
/// The mask is written out rather than left to the narrowing cast: the
/// anchor is sixty-four bits and the field is as wide as its holder, so
/// `bitfield<3> a` in a `u8` would otherwise take five bits that belong
/// to the field beside it.
fn bitset_decode_fn(
    ast: &Ast,
    type_: Id<TypeNode>,
    layout: &TypeLayout,
    codec: &'static str,
) -> Result<rust::Item, EncodeError> {
    let node = ast.type_(type_).map_err(|error| EncodeError::NoLayout {
        detail: alloc_format(&error),
    })?;
    let anchor = bitset_anchor(layout, &node.name)?;
    let mut statements = vec![rust::Statement::Let {
        name: Ident::new("bits"),
        type_: None,
        value: Some(rust::Expr::Try(Box::new(rust::Expr::MethodCall {
            receiver: Box::new(rust::Expr::Path(Path::single("r"))),
            method: Ident::new("read_bitset"),
            args: vec![rust::Expr::Literal(Literal::int(i128::from(anchor)))],
        }))),
        mutable: false,
    }];
    let mut fields = Vec::new();
    for (field, bits, position) in bitset_fields(ast, type_, &node.name)? {
        let mask: u64 = if bits >= 64 {
            u64::MAX
        } else {
            (1u64 << bits) - 1
        };
        let masked = rust::Expr::Binary {
            op: rust::BinaryOp::BitAnd,
            lhs: Box::new(rust::Expr::Binary {
                op: rust::BinaryOp::Shr,
                lhs: Box::new(rust::Expr::Path(Path::single("bits"))),
                rhs: Box::new(rust::Expr::Literal(Literal::int(i128::from(position)))),
            }),
            rhs: Box::new(rust::Expr::Literal(Literal::int(i128::from(mask)))),
        };
        let holder = crate::adapter::cst::bitmask_primitive(bits);
        let value = if holder == rust::Primitive::U64 {
            masked
        } else {
            rust::Expr::Cast {
                value: Box::new(masked),
                type_: Box::new(rust::Type::Primitive(holder)),
            }
        };
        fields.push((Ident::new(&field), value));
    }
    statements.push(rust::Statement::Tail(rust::Expr::Call {
        callee: Box::new(rust::Expr::Path(Path::single("Ok"))),
        args: vec![rust::Expr::StructLit {
            path: Path::single("Self"),
            fields,
            base: None,
        }],
    }));
    Ok(rust::Item::Function(rust::Function {
        doc: Doc::none(),
        attrs: Vec::new(),
        visibility: Visibility::Public,
        name: Ident::new(&format!("decode{codec}")),
        generics: Vec::new(),
        params: vec![rust::Param {
            name: Some(Ident::new("r")),
            type_: rust::Type::Ref {
                target: Box::new(rust::Type::named(Path::single("Reader"))),
                mutable: true,
                lifetime: None,
            },
            receiver: None,
        }],
        returns: Some(rust::Type::generic(
            Path::single("Result"),
            vec![
                rust::Type::named(Path::single("Self")),
                rust::Type::named(Path::single("WireError")),
            ],
        )),
        body: Some(rust::Block { statements }),
    }))
}

/// The encoder **body** for a structure or a valuetype, and — under
/// [`per_sample`] — the public entry beside it.
///
/// The statements follow the plan's member order, which is wire order
/// and not necessarily declaration order.
///
/// Fallible even for a type with nothing fallible in it, so the
/// signature is the type's and not its members'. `char` and `wchar`
/// refuse a code point they cannot carry, and an `encode` whose
/// signature changed with the member list would make that a source
/// break for every caller the day someone added one.
///
/// `steps` names the members whose PL_CDR1 header form the sample
/// decides, and the body reads [`EXTENDED_PARAM`] for each of them.
fn encode_fns(
    ast: &Ast,
    type_: Id<TypeNode>,
    layout: &TypeLayout,
    steps: &StepHeaders,
    key: LayoutKey,
    codec: &'static str,
) -> Result<Vec<rust::Item>, EncodeError> {
    let mut statements = Vec::new();
    // The flag is read by the members whose header form the sample
    // decides, and by nothing else: a `@final` aggregate of primitives
    // takes it and never looks at it, which is `unused_variables` in
    // every crate that includes the generated text. `let _ =` is valid
    // whether or not the body goes on to use it, so it is emitted
    // unconditionally rather than from a guess about which bodies read it
    // — the same shape `crate::emit::encode`'s `with_extended_used` uses
    // for the twenty other languages.
    if per_sample(key) {
        statements.push(rust::Statement::Let {
            name: Ident::new("_"),
            type_: None,
            value: Some(rust::Expr::Path(Path::single(EXTENDED_PARAM))),
            mutable: false,
        });
    }

    // The DHEADER the plan asks for, and nothing more: whether an
    // appendable type carries one is the plan's decision, made once.
    if layout.needs_dheader {
        // The length is not known until the members are written, so the
        // header is a placeholder the runtime patches. A generator that
        // wrote a length it computed itself would be computing the
        // layout a second time.
        statements.push(rust::Statement::Let {
            name: Ident::new("dheader"),
            type_: None,
            value: Some(rust::Expr::MethodCall {
                receiver: Box::new(rust::Expr::Path(Path::single("w"))),
                method: Ident::new("write_dheader"),
                args: Vec::new(),
            }),
            mutable: false,
        });
    }

    for plan in &layout.members {
        let Some(member) = member_of(ast, type_, plan.member, key, codec) else {
            continue;
        };
        let name = member.0;
        let kind = member.1;
        let optional = member_node_of(ast, type_, plan.member)
            .is_some_and(|member| member.facets.is_optional());

        let receiver = holder_of(ast, type_, plan.member);
        if optional {
            statements.extend(write_optional_member(
                receiver,
                plan,
                &name,
                &kind,
                steps.member(plan, &name)?,
            )?);
            continue;
        }

        // The header the plan chose, including the length code — this
        // is where `LengthCodePolicy` becomes visible in the output. A
        // member the sample's step decides gets the run-time branch
        // instead, because the plan named the short side of a decision
        // the sample makes.
        statements.extend(open_header(plan, steps.member(plan, &name)?));
        statements.push(write_for(receiver, &name, &kind)?);
        // The header's length is patched after the value, because that
        // is when it is known.
        statements.extend(close_header(plan));
    }

    // The sentinel a PL_CDR1 parameter list ends with. A reader that
    // stops at the last member it recognises instead of at the sentinel
    // misreads whatever follows.
    if layout.needs_sentinel {
        statements.push(call_statement("write_sentinel", Vec::new()));
    }

    if layout.needs_dheader {
        statements.push(call_statement(
            "finish_dheader",
            vec![rust::Expr::Path(Path::single("dheader"))],
        ));
    }

    statements.push(rust::Statement::Tail(rust::Expr::Call {
        callee: Box::new(rust::Expr::Path(Path::single("Ok"))),
        args: vec![rust::Expr::Tuple(Vec::new())],
    }));

    // An aggregate value is its own alignment block under
    // `ElementPadding::Packed`; see `zerodds-wire-codec`'s
    // `write_aggregate`.
    let statements = wrap_packed_body(
        statements,
        "w",
        key.element_padding == ElementPadding::Packed,
    );

    Ok(with_entry(
        encode_function(&body_name(codec, key), &statements, per_sample(key)),
        encode_entry(ast, type_, key, codec)?,
    ))
}

/// A generated `encode`, under either name the per-sample split gives it.
///
/// `flagged` is the body, which takes [`EXTENDED_PARAM`] after the writer;
/// the public entry takes no flag, because the sample whose root this type
/// is resolves the decision itself and passes the constant it resolved.
fn encode_function(name: &str, statements: &[rust::Statement], flagged: bool) -> rust::Item {
    let mut params = vec![
        rust::Param {
            name: None,
            type_: rust::Type::unit(),
            receiver: Some(rust::Receiver::Ref),
        },
        rust::Param {
            name: Some(Ident::new("w")),
            type_: rust::Type::Ref {
                target: Box::new(rust::Type::named(Path::single("Writer"))),
                mutable: true,
                lifetime: None,
            },
            receiver: None,
        },
    ];
    // After the writer, so the two the entry passes read in that order:
    // the sink, then the sample's decision.
    if flagged {
        params.push(rust::Param {
            name: Some(Ident::new(EXTENDED_PARAM)),
            type_: rust::Type::Primitive(rust::Primitive::Bool),
            receiver: None,
        });
    }
    rust::Item::Function(rust::Function {
        doc: Doc::none(),
        attrs: Vec::new(),
        visibility: Visibility::Public,
        name: Ident::new(name),
        generics: Vec::new(),
        params,
        returns: Some(rust::Type::generic(
            Path::single("Result"),
            vec![
                rust::Type::unit(),
                rust::Type::named(Path::single("WireError")),
            ],
        )),
        body: Some(rust::Block {
            statements: statements.to_vec(),
        }),
    })
}

/// The body, and the public entry that bakes this type's own step
/// decision in beside it — or the body alone, for every plan whose header
/// form is fixed.
fn with_entry(body: rust::Item, entry: Option<rust::Item>) -> Vec<rust::Item> {
    match entry {
        Some(entry) => vec![body, entry],
        None => vec![body],
    }
}

/// The public `encode(&self, w)` of a type under [`per_sample`], and
/// `None` without it.
///
/// RTI decides the PL_CDR1 header form for the **sample's** type, so the
/// sample whose root this type is resolves the decision once, here, and
/// hands it to the body — which threads it to every type the body calls.
/// The same nested `@mutable` type is then framed short inside a small
/// sample and extended inside a large one, which a single `encode` per
/// type cannot be.
///
/// The decision itself is [`sample_past_step`]'s, which asks
/// [`LayoutKey::for_sample`] — the crossing the reflective encoder makes
/// for the same sample, and the one that keeps
/// `crates/wire-codec`'s encoder and this codec writing the same octets.
///
/// # Errors
/// [`EncodeError`] when the sample's type cannot be sized, which is what
/// [`LayoutKey::for_sample`] reports.
fn encode_entry(
    ast: &Ast,
    type_: Id<TypeNode>,
    key: LayoutKey,
    codec: &'static str,
) -> Result<Option<rust::Item>, EncodeError> {
    if !per_sample(key) {
        return Ok(None);
    }
    let past = sample_past_step(ast, type_, key)?;
    let statements = vec![rust::Statement::Tail(rust::Expr::MethodCall {
        receiver: Box::new(rust::Expr::Path(Path::single("self"))),
        method: Ident::new(&body_name(codec, key)),
        args: vec![
            rust::Expr::Path(Path::single("w")),
            rust::Expr::Literal(Literal::Bool(past)),
        ],
    })];
    Ok(Some(encode_function(
        &format!("encode{codec}"),
        &statements,
        false,
    )))
}

/// The statements that open one member's framing, before its value.
///
/// Split out of the loop because an `@optional` member writes some of
/// them inside a `match` arm and some outside it, and a second copy of
/// the header shapes would be a second place for a length code to be
/// wrong.
///
/// `chosen` says that the sample, not the plan, picks the PL_CDR1 header
/// form, and the statements are then a run-time branch on
/// [`EXTENDED_PARAM`] between `write_pid` and `write_pid_extended`. It is
/// what [`StepHeaders`] records for the member and what the enclosing
/// body threads down from the sample's root.
fn open_header(plan: &zerodds_ast_layout::plan::MemberPlan, chosen: bool) -> Vec<rust::Statement> {
    match plan.header {
        MemberHeader::NoHeader => Vec::new(),
        MemberHeader::PidCdr1 {
            pid,
            extended,
            must_understand,
        } => vec![bind_pid_header(
            chosen,
            (pid, plan.member_id),
            extended,
            "Member",
            must_understand,
        )],
        // Rule (19) and §7.4.1.2. The `Optional` role is what makes
        // the declared length follow the optional convention rather
        // than the `@mutable` one; the two differ in Cyclone.
        MemberHeader::OptionalCdr1 {
            pid,
            extended,
            must_understand,
        } => vec![bind_pid_header(
            chosen,
            (pid, plan.member_id),
            extended,
            "Optional",
            must_understand,
        )],
        // Rule (20), §7.4.3.5.2: a BOOLEAN `<is_present>`. Written by
        // `write_optional_member`, which is the only caller that reaches
        // this arm and knows whether the member is there.
        MemberHeader::OptionalFlag => Vec::new(),
        MemberHeader::EmHeader {
            lc,
            must_understand,
        } => {
            // The member id and the length code both come from the
            // plan: the id is the completion pass's, the code is the
            // policy's, and neither is decided again here.
            vec![bind_header(
                "emheader",
                "write_emheader",
                vec![
                    rust::Expr::Literal(Literal::int(i128::from(plan.member_id))),
                    rust::Expr::Literal(Literal::int(i128::from(lc as u8))),
                    rust::Expr::Literal(Literal::Bool(must_understand)),
                ],
            )]
        }
    }
}

/// The `let pid = …` that opens a PL_CDR1 parameter header, in the form
/// `plan` names or in the one the sample's flag chooses.
///
/// Both forms call the same two runtime methods, so the branch is over
/// the *call* and not over anything else: `finish_pid` patches whichever
/// header was opened and reads its form back off the stream
/// (§7.4.1.2.3), and the id, the role and the flag are the plan's in
/// either arm. `chosen` with a plan that already names the extended form
/// cannot happen: [`StepHeaders`] records a member as chosen only where
/// the two sides of the step disagree, and the plan the codec is written
/// from is the short side.
fn bind_pid_header(
    chosen: bool,
    (pid, member_id): (u16, u32),
    extended: bool,
    role: &str,
    must_understand: bool,
) -> rust::Statement {
    // The short header carries the 16-bit parameter id; the extended one
    // the member id in 28 bits (XTypes 1.3 §7.4.1.2.2). The plan's `pid`
    // saturates at `u16::MAX` for a member id past 16 bits — the extended
    // form is then mandatory and takes `member_id`, or a `@hashid` member
    // goes out as member 65535.
    let writer = |method: &str| {
        let id = if method == "write_pid_extended" {
            i128::from(member_id)
        } else {
            i128::from(pid)
        };
        rust::Expr::Try(Box::new(rust::Expr::MethodCall {
            receiver: Box::new(rust::Expr::Path(Path::single("w"))),
            method: Ident::new(method),
            args: vec![
                rust::Expr::Literal(Literal::int(id)),
                parameter_role(role),
                rust::Expr::Literal(Literal::Bool(must_understand)),
            ],
        }))
    };
    let value = if !chosen {
        writer(if extended {
            "write_pid_extended"
        } else {
            "write_pid"
        })
    } else {
        // `zd_extended` is the sample's answer, resolved once by
        // [`sample_past_step`] in the entry that called this body and
        // threaded down to here.
        rust::Expr::If {
            condition: Box::new(rust::Expr::Path(Path::single(EXTENDED_PARAM))),
            then: rust::Block {
                statements: vec![rust::Statement::Tail(writer("write_pid_extended"))],
            },
            otherwise: Some(rust::Block {
                statements: vec![rust::Statement::Tail(writer("write_pid"))],
            }),
        }
    };
    rust::Statement::Let {
        name: Ident::new("pid"),
        type_: None,
        value: Some(value),
        mutable: false,
    }
}

/// The statements that close one member's framing, after its value.
fn close_header(plan: &zerodds_ast_layout::plan::MemberPlan) -> Vec<rust::Statement> {
    match plan.header {
        MemberHeader::NoHeader | MemberHeader::OptionalFlag => Vec::new(),
        MemberHeader::PidCdr1 { .. } | MemberHeader::OptionalCdr1 { .. } => vec![call_statement(
            "finish_pid",
            vec![rust::Expr::Path(Path::single("pid"))],
        )],
        // `?`: under a length code of 0 to 3 the code is the member's
        // length, and `finish_emheader` refuses a value of another size
        // rather than let the header misdeclare it (§7.4.3.4.2).
        MemberHeader::EmHeader { .. } => vec![try_call_statement(
            "finish_emheader",
            vec![rust::Expr::Path(Path::single("emheader"))],
        )],
    }
}

/// One `@optional` member's write, over the `Option<T>` field.
///
/// Three shapes, one per way the specification marks the absence, and
/// the *field* is what says which one this sample wants:
///
/// - rule (20), XCDR2 outside a `@mutable` type: a BOOLEAN
///   `<is_present>` either way, and the value only when it is `true`;
/// - rule (19), XCDR1 outside a `@mutable` type: the parameter header
///   either way, with a declared length of zero for the absence
///   (§7.4.1.2), which is what `finish_pid` computes from the bytes
///   that follow;
/// - inside a `@mutable` type: no parameter at all. §7.4.1.2.3 —
///   a member the stream does not carry is one the reader defaults, and
///   for an `@optional` member the default is the absence itself.
///
/// The match is over `&self.<name>`, so the binding is a borrow and the
/// value goes to the runtime the same way a borrowed field does.
///
/// `chosen` is [`open_header`]'s, passed on: an `@optional` member of a
/// `@mutable` type is framed as an `MMEMBER` and takes the same run-time
/// branch as every other member of the type.
fn write_optional_member(
    receiver: rust::Expr,
    plan: &zerodds_ast_layout::plan::MemberPlan,
    name: &str,
    kind: &MemberWrite,
    chosen: bool,
) -> Result<Vec<rust::Statement>, EncodeError> {
    let bound = rust::escape_keyword(name);
    let value = rust::Expr::Path(Path::single(&bound));

    let (mut present, absent) = match plan.header {
        MemberHeader::OptionalFlag => (
            vec![call_statement(
                "write_bool",
                vec![rust::Expr::Literal(Literal::Bool(true))],
            )],
            vec![call_statement(
                "write_bool",
                vec![rust::Expr::Literal(Literal::Bool(false))],
            )],
        ),
        // The header is outside the match: it is written whether or not
        // the value is, and its declared length is the marker.
        MemberHeader::OptionalCdr1 { .. } => (Vec::new(), Vec::new()),
        // A parameter list simply omits the parameter, so the header
        // goes inside the arm with the value.
        _ => (open_header(plan, chosen), Vec::new()),
    };
    present.push(write_value_to("w", name, kind, value, true)?);
    if !matches!(plan.header, MemberHeader::OptionalCdr1 { .. }) {
        present.extend(close_header(plan));
    }

    let matched = rust::Statement::Semi(rust::Expr::Match {
        scrutinee: Box::new(rust::Expr::Ref {
            target: Box::new(rust::Expr::Field {
                receiver: Box::new(receiver),
                name: Ident::new(&bound),
            }),
            mutable: false,
        }),
        arms: vec![
            rust::MatchArm {
                pattern: rust::Pattern::TupleStruct {
                    path: Path::single("Some"),
                    elements: vec![rust::Pattern::Binding(Ident::new(&bound))],
                },
                guard: None,
                body: rust::Expr::Block(rust::Block {
                    statements: present,
                }),
            },
            rust::MatchArm {
                pattern: rust::Pattern::Path(Path::single("None")),
                guard: None,
                body: rust::Expr::Block(rust::Block { statements: absent }),
            },
        ],
    });

    if matches!(plan.header, MemberHeader::OptionalCdr1 { .. }) {
        let mut all = open_header(plan, chosen);
        all.push(matched);
        all.extend(close_header(plan));
        return Ok(all);
    }
    Ok(vec![matched])
}

/// `fn key_holder_be(&self) -> Result<Vec<u8>, WireError>`,
/// `fn key_from_holder_be(r: &mut Reader) -> Result<Self, WireError>` and
/// `fn key_hash(&self) -> Result<[u8; 16], WireError>`, for a keyed type.
///
/// The two are one function apart — §7.6.8 steps 1 to 4 build the
/// holder, step 5 turns it into the hash — and they are emitted together
/// so that no caller can reach the hash through a holder written by
/// anything but this plan.
///
/// Empty for a type with no `@key` member: a KeyHash over nothing is
/// not a KeyHash, and emitting one would give every instance of that
/// type the same handle. Settled in favour of this position after
/// `wireprobe-zerodds --emit keyhash` was found answering a keyless type
/// with sixteen zero bytes, or with the MD5 of nothing when the type was
/// `@mutable`. Cyclone, RTI and Fast DDS all decide on "no keys" before
/// any hash is computed and produce none;
/// `crates/wire-probe/src/encode.rs::key_hash` carries the measurement
/// and now refuses as well.
///
/// The holder is always big-endian — DDSI-RTPS 2.5 §9.6.3.8 — and that
/// fact comes from `key_holder_plan_with`, not from a second reading of
/// the model here.
///
/// What the holder *contains* and in what order does take the dialect.
/// This column used to plan the holder under `LayoutKey::key`, whose
/// comment said "§7.6.8 leaves no room for a dialect"; three of the
/// holder's rules are measured divergences and the other three columns
/// have selected them for a while — `KeyHolderExtensibility` (RTI keeps
/// the type's own kind under XCDR1), `KeyHolderNesting` (RTI stops
/// filtering below a keyless aggregate under XCDR1) and
/// `KeyHolderOrder` (RTI under XCDR1 keeps declaration order).
/// A column that ignored all three answered `--dialect rti` with
/// Cyclone's instance handle.
fn key_holder_fns(
    ast: &Ast,
    type_: Id<TypeNode>,
    data_key: LayoutKey,
) -> Result<Vec<rust::Item>, EncodeError> {
    // The holder's own key, not the data key: big-endian and the key
    // purpose, both of which §7.6.8 fixes, over the dialect the data
    // plan was built with.
    let key = LayoutKey {
        purpose: zerodds_ast_layout::Purpose::Key,
        endianness: zerodds_ast_layout::Endianness::Big,
        ..data_key
    };
    let keys = zerodds_ast_layout::keyhash::key_members_with(
        ast,
        type_,
        key.key_holder_nesting,
        key.key_holder_order,
    )
    .map_err(|error| EncodeError::NoLayout {
        detail: alloc_format(&error),
    })?;
    if keys.is_empty() {
        return Ok(Vec::new());
    }

    let form =
        zerodds_ast_layout::keyhash::key_hash_form_for(ast, type_, key).map_err(|error| {
            EncodeError::NoLayout {
                detail: alloc_format(&error),
            }
        })?;

    let mut statements = vec![rust::Statement::Let {
        name: Ident::new("holder"),
        type_: None,
        // An associated function, not a method: the constructor takes
        // both the order and the alignment cap, and `Writer.with_…()` is
        // not Rust.
        //
        // The cap is four and not the `Writer`'s own default of eight.
        // XTypes 1.3 §7.6.8 step 4 serialises the holder as PLAIN_CDR2,
        // whose `MAXALIGN` §7.4.1 gives as four, whatever version the
        // sample itself goes out in. `Writer::big_endian()` stood here
        // and left the cap at eight, so a holder whose plan put a
        // `@key double` at offset four had it written at offset eight —
        // a KeyHash that disagrees with the plan that sized it, and with
        // every peer, for every keyed type with an eight-octet key
        // member behind a four-octet one. Nothing reported it: a
        // KeyHash is sixteen opaque octets and a wrong one only shows up
        // as an instance that never matches.
        //
        // The order is written as an absolute path rather than through
        // an import, because the generated file's import list is built
        // by each consumer of this emitter and a new name in it would
        // have to be added in each one.
        value: Some(rust::Expr::Call {
            callee: Box::new(rust::Expr::Path(Path::new(&["Writer", "with_max_align"]))),
            args: vec![
                rust::Expr::Path(Path::new(&["zerodds_wire", "Endianness", "Big"])),
                // PLAIN_CDR2's four, unless the dialect aligns the holder
                // to its data encoding or packs it (`KeyHolderAlignment`).
                rust::Expr::Literal(Literal::int(i128::from(
                    key.key_holder_alignment.max_alignment(key.encoding),
                ))),
            ],
        }),
        mutable: true,
    }];

    // A nested key member is written through the **bare** codec, not
    // through a versioned one: XTypes 1.3 §7.6.8 step 4 serialises the
    // holder as PLAIN_CDR2 whatever version the sample goes out in, so
    // there is no XCDR1 key holder for an XCDR1 codec to call.
    let codec = "";

    // Only the key members, in the order `key_members_with` gives —
    // which is the dialect's holder order, and the same order
    // `plan_aggregate` laid the holder out in.
    for node in &keys {
        let Some((name, kind)) = member_of(ast, type_, *node, key, codec) else {
            continue;
        };
        statements.push(write_value_to_sink(
            "holder",
            SinkShape::Owned,
            &name,
            &kind,
            rust::Expr::Field {
                receiver: Box::new(holder_of(ast, type_, *node)),
                name: Ident::new(&rust::escape_keyword(&name)),
            },
            // A key member is never `@optional` — §7.2.2.4.4.4.8, and
            // `idl-lower::validate::member_rules` refuses the pair — so
            // the field here is the member's own type and not an
            // `Option` of it.
            false,
        )?);
    }

    // The octets of the holder itself, before step 5 turns them into a
    // hash. A `DdsType` impl needs exactly these: the runtime's
    // instance path writes the holder into a `PlainCdr2BeKeyHolder` and
    // decides the branch of step 5 from the type's declared maximum, so
    // handing it a finished hash would be handing it the wrong thing.
    let mut holder_statements = statements.clone();
    holder_statements.push(rust::Statement::Tail(rust::Expr::Call {
        callee: Box::new(rust::Expr::Path(Path::single("Ok"))),
        args: vec![rust::Expr::MethodCall {
            receiver: Box::new(rust::Expr::Path(Path::single("holder"))),
            method: Ident::new("finish"),
            args: Vec::new(),
        }],
    }));
    let key_holder_be = rust::Item::Function(rust::Function {
        doc: Doc::none(),
        attrs: Vec::new(),
        visibility: Visibility::Public,
        name: Ident::new("key_holder_be"),
        generics: Vec::new(),
        params: vec![rust::Param {
            name: None,
            type_: rust::Type::unit(),
            receiver: Some(rust::Receiver::Ref),
        }],
        // Fallible for the same reason `key_hash` is.
        returns: Some(rust::Type::generic(
            Path::single("Result"),
            vec![
                rust::Type::generic(
                    Path::single("Vec"),
                    vec![rust::Type::Primitive(rust::Primitive::U8)],
                ),
                rust::Type::named(Path::single("WireError")),
            ],
        )),
        body: Some(rust::Block {
            statements: holder_statements,
        }),
    });

    // The inverse of `key_holder_be`: the same key members, read in the
    // same order the same plan wrote them.
    //
    // XTypes 1.3 §7.6.8 builds a holder that is not the type's own
    // stream — big-endian, `MAXALIGN` four, only the key members, no
    // DHEADER and no member headers — so `decode` cannot read one, and a
    // type that only had `encode_key_holder_be` could serialise an
    // instance's key and never read it back. DDS 1.4 §2.2.2.5.1.13 needs
    // exactly that read: on a dispose/unregister sample
    // `SampleInfo.valid_data` is `false` and "only the fields that form
    // the key" are meaningful, and those fields have to come from
    // somewhere.
    //
    // The members outside the holder are not in the stream at all and
    // take their §7.3.1.2.1.10 defaults through `Default::default()` —
    // the same values `default_impl` emits, so the two cannot drift.
    let mut from_holder = Vec::new();
    let mut key_fields = Vec::new();
    // A key member this emitter cannot describe is not skipped here. The
    // writer above skips it — a judged silent drop, allowlisted in
    // `tools/silent-drop-guard.sh` — but a *reader* that skipped one would
    // read the following member off the octets of the one it left out and
    // hand back a key that is wrong rather than absent. The whole reader
    // refuses instead, and says which member it could not read.
    let mut undescribed = None;
    for node in &keys {
        match member_of(ast, type_, *node, key, codec) {
            Some((name, kind)) => {
                let field = rust::escape_keyword(&name);
                from_holder.push(let_statement(
                    &field,
                    rust::Expr::Try(Box::new(read_for(&name, &kind)?)),
                ));
                key_fields.push((
                    *node,
                    (Ident::new(&field), rust::Expr::Path(Path::single(&field))),
                ));
            }
            None => undescribed = Some(*node),
        }
    }
    if let Some(node) = undescribed {
        from_holder = vec![rust::Statement::Tail(refused_err(
            "r",
            &format!("a key holder with a member this emitter cannot read ({node:?})"),
        ))];
    } else {
        from_holder.push(rust::Statement::Tail(rust::Expr::Call {
            callee: Box::new(rust::Expr::Path(Path::single("Ok"))),
            args: vec![nested_struct_literal(ast, type_, key_fields, &|| {
                Some(Box::new(rust::Expr::Call {
                    callee: Box::new(rust::Expr::Path(Path::new(&["Default", "default"]))),
                    args: Vec::new(),
                }))
            })],
        }));
    }
    let key_from_holder_be = rust::Item::Function(rust::Function {
        doc: Doc::none(),
        attrs: Vec::new(),
        visibility: Visibility::Public,
        name: Ident::new("key_from_holder_be"),
        generics: Vec::new(),
        params: vec![rust::Param {
            name: Some(Ident::new("r")),
            type_: rust::Type::Ref {
                target: Box::new(rust::Type::named(Path::single("Reader"))),
                mutable: true,
                lifetime: None,
            },
            receiver: None,
        }],
        returns: Some(rust::Type::generic(
            Path::single("Result"),
            vec![
                rust::Type::named(Path::single("Self")),
                rust::Type::named(Path::single("WireError")),
            ],
        )),
        body: Some(rust::Block {
            statements: from_holder,
        }),
    });

    // A declared maximum of sixteen bytes or fewer: zero-pad the holder.
    // A larger or unbounded maximum: MD5. Variable length alone does not
    // select MD5; the shared maximum-size calculation decides.
    let finish = match form {
        zerodds_ast_layout::keyhash::KeyHashForm::ZeroPadded => "zero_padded",
        zerodds_ast_layout::keyhash::KeyHashForm::Md5 => "md5",
    };
    statements.push(rust::Statement::Tail(rust::Expr::Call {
        callee: Box::new(rust::Expr::Path(Path::single("Ok"))),
        args: vec![rust::Expr::MethodCall {
            receiver: Box::new(rust::Expr::Path(Path::single("holder"))),
            method: Ident::new(finish),
            args: Vec::new(),
        }],
    }));

    Ok(vec![
        key_holder_be,
        key_from_holder_be,
        rust::Item::Function(rust::Function {
            doc: Doc::none(),
            attrs: Vec::new(),
            visibility: Visibility::Public,
            name: Ident::new("key_hash"),
            generics: Vec::new(),
            params: vec![rust::Param {
                name: None,
                type_: rust::Type::unit(),
                receiver: Some(rust::Receiver::Ref),
            }],
            // Fallible for the reason `encode` is: a `@key char` refuses
            // a code point above U+00FF, and a KeyHash that swallowed
            // the refusal would hash a character nobody wrote.
            returns: Some(rust::Type::generic(
                Path::single("Result"),
                vec![
                    rust::Type::Array {
                        element: Box::new(rust::Type::Primitive(rust::Primitive::U8)),
                        length: Box::new(rust::Expr::Literal(Literal::int(16))),
                    },
                    rust::Type::named(Path::single("WireError")),
                ],
            )),
            body: Some(rust::Block { statements }),
        }),
    ])
}

/// `fn decode(r: &mut Reader) -> Self`.
///
/// The mirror of the encoder, member for member and header for header,
/// built from the same plan — which is what makes the two agree by
/// construction rather than by review.
fn decode_fn(
    ast: &Ast,
    type_: Id<TypeNode>,
    layout: &TypeLayout,
    key: LayoutKey,
    codec: &'static str,
) -> Result<rust::Item, EncodeError> {
    let statements = if ast.effective_extensibility(type_) == zerodds_ast::Extensibility::Mutable {
        decode_by_member_id(ast, type_, layout, key, codec)?
    } else {
        decode_in_plan_order(ast, type_, layout, key, codec)?
    };
    // The read side of `encode_fns`'s block: an aggregate value's members
    // align from its own first octet under `ElementPadding::Packed`.
    let statements = wrap_packed_body(
        statements,
        "r",
        key.element_padding == ElementPadding::Packed,
    );

    Ok(rust::Item::Function(rust::Function {
        doc: Doc::none(),
        attrs: Vec::new(),
        visibility: Visibility::Public,
        name: Ident::new(&format!("decode{codec}")),
        generics: Vec::new(),
        params: vec![rust::Param {
            name: Some(Ident::new("r")),
            type_: rust::Type::Ref {
                target: Box::new(rust::Type::named(Path::single("Reader"))),
                mutable: true,
                lifetime: None,
            },
            receiver: None,
        }],
        returns: Some(rust::Type::generic(
            Path::single("Result"),
            vec![
                rust::Type::named(Path::single("Self")),
                rust::Type::named(Path::single("WireError")),
            ],
        )),
        body: Some(rust::Block { statements }),
    }))
}

/// A `@final` or `@appendable` aggregate: the members where the plan
/// says, because that is where the writer put them.
///
/// §7.4.1.2.3 makes only a `@mutable` list unordered. A positional
/// aggregate that read by id would be inventing a framing its own
/// encoder does not write.
fn decode_in_plan_order(
    ast: &Ast,
    type_: Id<TypeNode>,
    layout: &TypeLayout,
    key: LayoutKey,
    codec: &'static str,
) -> Result<Vec<rust::Statement>, EncodeError> {
    // A writer with fewer members than this reader stops early, and
    // §7.2.4.4.8, Table 19, says "each non-optional member in a T1
    // object that is not present in the T2 object takes its default
    // value". Only an `@appendable` type may meet one: Table 19 requires
    // two final types to have the same set of member IDs.
    let appendable = ast.effective_extensibility(type_) == zerodds_ast::Extensibility::Appendable;
    let mut statements = Vec::new();
    if layout.needs_dheader {
        // The DHEADER is what says where the writer's members stopped —
        // "skipping the members an older reader does not know is what
        // the DHEADER is for". `end` is that offset; a type that cannot
        // meet a shorter writer still reads it, to move the cursor.
        if appendable {
            statements.push(let_statement(
                "end",
                rust::Expr::Binary {
                    op: rust::BinaryOp::Add,
                    lhs: Box::new(rust::Expr::Try(Box::new(call("read_dheader", Vec::new())))),
                    rhs: Box::new(call("position", Vec::new())),
                },
            ));
        } else {
            statements.push(let_statement(
                "_dheader",
                rust::Expr::Try(Box::new(call("read_dheader", Vec::new()))),
            ));
        }
    }

    let mut fields = Vec::new();
    for plan in &layout.members {
        let Some((name, kind)) = member_of(ast, type_, plan.member, key, codec) else {
            continue;
        };
        // The appendable tail. Under encoding version 2 the delimiter
        // says where the writer stopped; under version 1 §7.2.4.2 leaves
        // an appendable type undelimited and the payload's end is what
        // all three vendors use instead — Cyclone, RTI and Fast DDS each
        // default the tail of a short XCDR1 stream, measured 2026-08-09
        // as `evo-appendable-tail`.
        if appendable && matches!(plan.header, MemberHeader::NoHeader) {
            let Some(member) = member_node_of(ast, type_, plan.member) else {
                continue;
            };
            let Some(member_type) = member.type_.resolved() else {
                continue;
            };
            let reached = if layout.needs_dheader {
                rust::Expr::Binary {
                    op: rust::BinaryOp::Ge,
                    lhs: Box::new(call("position", Vec::new())),
                    rhs: Box::new(rust::Expr::Path(Path::single("end"))),
                }
            } else {
                rust::Expr::Binary {
                    op: rust::BinaryOp::Eq,
                    lhs: Box::new(call("remaining", Vec::new())),
                    rhs: Box::new(rust::Expr::Literal(Literal::int(0))),
                }
            };
            statements.push(let_statement(
                &name,
                rust::Expr::If {
                    condition: Box::new(reached),
                    then: rust::Block {
                        statements: vec![rust::Statement::Tail(
                            crate::default_rust::member_default(
                                ast,
                                module_of(ast, type_),
                                member,
                                member_type,
                            )?,
                        )],
                    },
                    otherwise: Some(rust::Block {
                        statements: vec![rust::Statement::Tail(rust::Expr::Try(Box::new(
                            read_for(&name, &kind)?,
                        )))],
                    }),
                },
            ));
            fields.push((plan.member, field_binding(&name)));
            continue;
        }
        // An `@optional` member reads into an `Option<T>`, so the marker
        // the specification puts on the wire decides between `Some` and
        // `None` instead of between a value and a refusal.
        if member_node_of(ast, type_, plan.member).is_some_and(|member| member.facets.is_optional())
        {
            statements.extend(read_optional_member(plan, &name, &kind)?);
            fields.push((plan.member, field_binding(&name)));
            continue;
        }
        match plan.header {
            MemberHeader::NoHeader | MemberHeader::OptionalFlag => {}
            MemberHeader::PidCdr1 { .. } | MemberHeader::OptionalCdr1 { .. } => {
                statements.push(let_statement(
                    "pid",
                    rust::Expr::Try(Box::new(call("read_pid", Vec::new()))),
                ));
            }
            MemberHeader::EmHeader { .. } => {
                statements.push(let_statement(
                    "emheader",
                    rust::Expr::Try(Box::new(call("read_emheader", Vec::new()))),
                ));
            }
        }
        // Every read is fallible, so every read is followed by `?`. A
        // decoder that could not fail would have to guess at bytes that
        // are not there, and a guess is a value nobody sent.
        statements.push(let_statement(
            &name,
            rust::Expr::Try(Box::new(read_for(&name, &kind)?)),
        ));
        // The mirror of the encoder's patch step. `Reader::read_pid`
        // makes the parameter's value its own alignment origin — §7.4.1.2
        // calls each parameter "a CDR-encapsulated block of data" — and
        // `finish_pid` is the only thing that puts the origin back and
        // steps over the padding to the next four-byte boundary.
        //
        // Without it the origin stayed at the last parameter's value
        // start for the rest of the aggregate. A parameter whose value
        // begins at an offset that is 4 modulo 8 then aligns an
        // eight-byte member after it four bytes late:
        // `@final struct S { @optional octet b; double c; };` writes `c`
        // at payload offset 8 and read it at 12. It also skipped
        // `check_declared_length`, so a peer's length that disagreed
        // with the value that followed it was accepted in silence.
        match plan.header {
            MemberHeader::NoHeader | MemberHeader::OptionalFlag | MemberHeader::EmHeader { .. } => {
            }
            MemberHeader::PidCdr1 { .. } | MemberHeader::OptionalCdr1 { .. } => {
                statements.push(rust::Statement::Semi(rust::Expr::Try(Box::new(call(
                    "finish_pid",
                    vec![rust::Expr::Path(Path::single("pid"))],
                )))));
            }
        }
        fields.push((plan.member, field_binding(&name)));
    }

    // Rule (23): a PL_CDR1 parameter list ends at `PID_SENTINEL`, not at
    // the last member the reader recognises. The encoder writes one and
    // the decoder left it in the stream, which puts every byte after the
    // aggregate four positions early for any caller that reads on.
    if layout.needs_sentinel {
        statements.extend(read_sentinel());
    }

    statements.push(rust::Statement::Tail(rust::Expr::Call {
        callee: Box::new(rust::Expr::Path(Path::single("Ok"))),
        args: vec![nested_struct_literal(ast, type_, fields, &|| None)],
    }));

    Ok(statements)
}

/// A `@mutable` aggregate: the members its headers name, in the order
/// the stream names them.
///
/// XTypes 1.3 §7.4.1.2.3 — "any mutable structure member's value may be
/// omitted" — and §7.4.3.4.2, which gives every member an EMHEADER
/// carrying its id. A `@mutable` list is unordered by construction and
/// the ids are what orders it; this column read it in *plan* order
/// instead, so a member the stream omitted took the next member's bytes
/// and a member the peer reordered was read as the wrong member. Both
/// produce a sample and neither reports anything. The reflective, C and
/// Python columns have read by id since the evolution run.
///
/// Two loops, one per encoding version, because the two have different
/// ends: rule (21) bounds the list with the DHEADER's extent and rule
/// (23) with `PID_SENTINEL`.
///
/// A member the stream did not carry takes its default afterwards —
/// §7.4.1.2.3, "if the receiver's member is not optional, it logically
/// takes its default value" — and an `@optional` one takes the absence,
/// which is what its slot already holds.
fn decode_by_member_id(
    ast: &Ast,
    type_: Id<TypeNode>,
    layout: &TypeLayout,
    key: LayoutKey,
    codec: &'static str,
) -> Result<Vec<rust::Statement>, EncodeError> {
    let mut slots = Vec::new();
    for plan in &layout.members {
        let Some((name, kind)) = member_of(ast, type_, plan.member, key, codec) else {
            continue;
        };
        slots.push(MutableSlot {
            name,
            kind,
            member_id: plan.member_id,
        });
    }

    let mut statements = Vec::new();
    // Rule (21): under encoding version 2 the DHEADER is what says where
    // the list stops. Under version 1 rule (23) uses `PID_SENTINEL`
    // instead and there is no delimiter to read.
    if layout.needs_dheader {
        statements.push(let_statement(
            "end",
            rust::Expr::Binary {
                op: rust::BinaryOp::Add,
                lhs: Box::new(rust::Expr::Try(Box::new(call("read_dheader", Vec::new())))),
                rhs: Box::new(call("position", Vec::new())),
            },
        ));
    }
    // One slot per member, empty until a header names it. `Option` is
    // also the "seen" flag: a second header for the same id is a stream
    // that says two things about one member, and §7.4.3.4.2 gives each
    // one header.
    for slot in &slots {
        statements.push(rust::Statement::Let {
            name: Ident::new(&rust::escape_keyword(&slot.name)),
            type_: None,
            value: Some(rust::Expr::Path(Path::single("None"))),
            mutable: true,
        });
    }

    if key.encoding.is_v1() {
        statements.extend(parameter_list_loop(&slots)?);
    } else {
        statements.extend(member_header_loop(&slots)?);
    }

    // The members no header named. Written after the loop and not
    // inside it, because "did not arrive" is only knowable once the
    // list has ended.
    let mut fields = Vec::new();
    for plan in &layout.members {
        let Some((name, _)) = member_of(ast, type_, plan.member, key, codec) else {
            continue;
        };
        let bound = rust::escape_keyword(&name);
        let optional = member_node_of(ast, type_, plan.member)
            .is_some_and(|member| member.facets.is_optional());
        if !optional {
            let Some(member) = member_node_of(ast, type_, plan.member) else {
                continue;
            };
            let Some(member_type) = member.type_.resolved() else {
                continue;
            };
            statements.push(let_statement(
                &name,
                or_default(
                    &bound,
                    crate::default_rust::member_default(
                        ast,
                        module_of(ast, type_),
                        member,
                        member_type,
                    )?,
                ),
            ));
        }
        fields.push((
            plan.member,
            (Ident::new(&bound), rust::Expr::Path(Path::single(&bound))),
        ));
    }

    statements.push(rust::Statement::Tail(rust::Expr::Call {
        callee: Box::new(rust::Expr::Path(Path::single("Ok"))),
        args: vec![nested_struct_literal(ast, type_, fields, &|| None)],
    }));
    Ok(statements)
}

/// The value of a member no header named: `bound` if one did, its
/// default if not.
///
/// Spelled the way clippy asks for, so a crate including the generated
/// text needs no allowance: `unwrap_or_default()` where Table 9's value
/// is also Rust's `Default` (a zero, `false`, `'\0'`, an empty `String`,
/// `Vec` or `BTreeMap`), `unwrap_or(value)` where the value is a constant
/// expression, and `unwrap_or_else(|| value)` where building it calls
/// something. A `match` with a `None` arm was all three, and
/// `manual_unwrap_or{,_default}` fired once per member.
fn or_default(bound: &str, value: rust::Expr) -> rust::Expr {
    let receiver = Box::new(rust::Expr::Path(Path::single(bound)));
    if is_rust_default(&value) {
        return rust::Expr::MethodCall {
            receiver,
            method: Ident::new("unwrap_or_default"),
            args: Vec::new(),
        };
    }
    if is_constant(&value) {
        return rust::Expr::MethodCall {
            receiver,
            method: Ident::new("unwrap_or"),
            args: vec![value],
        };
    }
    rust::Expr::MethodCall {
        receiver,
        method: Ident::new("unwrap_or_else"),
        args: vec![rust::Expr::Closure {
            params: Vec::new(),
            body: Box::new(value),
        }],
    }
}

/// Whether `value` is what `Default::default()` gives for its type.
fn is_rust_default(value: &rust::Expr) -> bool {
    match value {
        rust::Expr::Literal(Literal::Int(text)) => text == "0",
        rust::Expr::Literal(Literal::Float(text)) => {
            matches!(text.as_str(), "0.0" | "0.0f32" | "0.0f64")
        }
        rust::Expr::Literal(Literal::Bool(flag)) => !flag,
        rust::Expr::Literal(Literal::Char(ch)) => *ch == '\0',
        rust::Expr::Call { callee, args } if args.is_empty() => {
            let rust::Expr::Path(path) = callee.as_ref() else {
                return false;
            };
            let names: Vec<&str> = path.segments().iter().map(Ident::as_str).collect();
            matches!(
                names.as_slice(),
                ["String", "new"] | ["Vec", "new"] | ["BTreeMap", "new"]
            )
        }
        _ => false,
    }
}

/// Whether `value` is built without calling anything: literals, paths,
/// and aggregates of them.
///
/// zerodds-lint: recursion-depth = the default value's nesting, one level
/// per composite literal; `default_rust` bounds it at 64.
fn is_constant(value: &rust::Expr) -> bool {
    match value {
        rust::Expr::Literal(_) | rust::Expr::Path(_) => true,
        rust::Expr::Array(elements) | rust::Expr::Tuple(elements) => {
            elements.iter().all(is_constant)
        }
        rust::Expr::Repeat { value, .. } => is_constant(value),
        rust::Expr::StructLit { fields, base, .. } => {
            base.is_none() && fields.iter().all(|(_, field)| is_constant(field))
        }
        rust::Expr::Cast { value, .. } => is_constant(value),
        _ => false,
    }
}

/// `return Err(WireError::refused(r.position(), "…"));`
fn refuse_statement(why: &str) -> rust::Statement {
    rust::Statement::Semi(rust::Expr::Return(Some(Box::new(rust::Expr::Call {
        callee: Box::new(rust::Expr::Path(Path::single("Err"))),
        args: vec![rust::Expr::Call {
            callee: Box::new(rust::Expr::Path(Path::new(&["WireError", "refused"]))),
            args: vec![
                call("position", Vec::new()),
                rust::Expr::Literal(Literal::string(why)),
            ],
        }],
    }))))
}

/// `if <slot>.is_some() { return Err(…); }`
fn refuse_a_second_header(slot: &str, member_id: u32) -> rust::Statement {
    rust::Statement::Semi(rust::Expr::If {
        condition: Box::new(rust::Expr::MethodCall {
            receiver: Box::new(rust::Expr::Path(Path::single(slot))),
            method: Ident::new("is_some"),
            args: Vec::new(),
        }),
        then: rust::Block {
            statements: vec![refuse_statement(&format!(
                "one header per member, and member id {member_id} has two"
            ))],
        },
        otherwise: None,
    })
}

/// Rule (21): EMHEADERs until the delimiter's extent is used up.
fn member_header_loop(slots: &[MutableSlot]) -> Result<Vec<rust::Statement>, EncodeError> {
    let mut body = vec![
        let_statement(
            "header",
            rust::Expr::Try(Box::new(call("read_emheader", Vec::new()))),
        ),
        // Where this member ends, whatever it turns out to be. Read
        // before the value, because after it the length code's own
        // rounding is no longer recoverable.
        let_statement(
            "ends",
            rust::Expr::Binary {
                op: rust::BinaryOp::Add,
                lhs: Box::new(call("position", Vec::new())),
                rhs: Box::new(rust::Expr::Field {
                    receiver: Box::new(rust::Expr::Path(Path::single("header"))),
                    name: Ident::new("extent"),
                }),
            },
        ),
    ];

    // §7.2.2.4.4.4.6: a member this type does not declare is skipped
    // when the writer said it may be, and discards the whole sample
    // when it said it may not.
    let mut chain = Some(rust::Block {
        statements: vec![rust::Statement::Semi(rust::Expr::If {
            condition: Box::new(rust::Expr::Field {
                receiver: Box::new(rust::Expr::Path(Path::single("header"))),
                name: Ident::new("must_understand"),
            }),
            then: rust::Block {
                statements: vec![refuse_statement(
                    "a must-understand member this type does not declare, per §7.2.2.4.4.4.6",
                )],
            },
            otherwise: Some(rust::Block {
                statements: vec![rust::Statement::Semi(rust::Expr::Try(Box::new(call(
                    "skip_to",
                    vec![rust::Expr::Path(Path::single("ends"))],
                ))))],
            }),
        })],
    });

    for slot in slots.iter().rev() {
        let bound = rust::escape_keyword(&slot.name);
        let mut arm = vec![refuse_a_second_header(&bound, slot.member_id)];
        arm.push(rust::Statement::Semi(rust::Expr::Assign {
            target: Box::new(rust::Expr::Path(Path::single(&bound))),
            value: Box::new(rust::Expr::Call {
                callee: Box::new(rust::Expr::Path(Path::single("Some"))),
                args: vec![rust::Expr::Try(Box::new(read_for(&slot.name, &slot.kind)?))],
            }),
        }));
        // The header said how long the member is; a value that did not
        // fill it means the two disagree, and reading on would take the
        // next header out of this member's bytes.
        arm.push(rust::Statement::Semi(rust::Expr::If {
            condition: Box::new(rust::Expr::Binary {
                op: rust::BinaryOp::Ne,
                lhs: Box::new(call("position", Vec::new())),
                rhs: Box::new(rust::Expr::Path(Path::single("ends"))),
            }),
            then: rust::Block {
                statements: vec![refuse_statement(
                    "a member filling the extent its EMHEADER declared",
                )],
            },
            otherwise: None,
        }));
        chain = Some(rust::Block {
            statements: vec![rust::Statement::Semi(rust::Expr::If {
                condition: Box::new(rust::Expr::Binary {
                    op: rust::BinaryOp::Eq,
                    lhs: Box::new(rust::Expr::Field {
                        receiver: Box::new(rust::Expr::Path(Path::single("header"))),
                        name: Ident::new("member_id"),
                    }),
                    rhs: Box::new(rust::Expr::Literal(Literal::int(i128::from(
                        slot.member_id,
                    )))),
                }),
                then: rust::Block { statements: arm },
                otherwise: chain,
            })],
        });
    }
    if let Some(chain) = chain {
        body.extend(chain.statements);
    }

    Ok(vec![rust::Statement::Semi(rust::Expr::While {
        condition: Box::new(rust::Expr::Binary {
            op: rust::BinaryOp::Lt,
            lhs: Box::new(call("position", Vec::new())),
            rhs: Box::new(rust::Expr::Path(Path::single("end"))),
        }),
        body: rust::Block { statements: body },
    })])
}

/// Rule (23): parameters until `PID_SENTINEL`.
fn parameter_list_loop(slots: &[MutableSlot]) -> Result<Vec<rust::Statement>, EncodeError> {
    // A sentinel is the only thing that ends the list, so the loop
    // condition is a flag the sentinel sets rather than a count.
    let mut statements = vec![rust::Statement::Let {
        name: Ident::new("ended"),
        type_: None,
        value: Some(rust::Expr::Literal(Literal::Bool(false))),
        mutable: true,
    }];

    // Stepping over a parameter this type has no member for, in the
    // one place three different reasons reach it: an id the model does
    // not declare, a vendor-private parameter and one inside
    // §7.4.1.2.2's reserved range.
    let skip_parameter_block = rust::Block {
        statements: vec![rust::Statement::Semi(rust::Expr::Try(Box::new(call(
            "skip_parameter",
            vec![rust::Expr::Path(Path::single("pid"))],
        ))))],
    };
    let unknown = rust::Block {
        statements: vec![rust::Statement::Semi(rust::Expr::If {
            condition: Box::new(rust::Expr::Field {
                receiver: Box::new(rust::Expr::Path(Path::single("pid"))),
                name: Ident::new("must_understand"),
            }),
            then: rust::Block {
                statements: vec![refuse_statement(
                    "a must-understand parameter this type does not declare, per §7.2.2.4.4.4.6",
                )],
            },
            otherwise: Some(skip_parameter_block.clone()),
        })],
    };
    let mut chain = Some(unknown.clone());

    for slot in slots.iter().rev() {
        let bound = rust::escape_keyword(&slot.name);
        let mut arm = vec![refuse_a_second_header(&bound, slot.member_id)];
        arm.push(rust::Statement::Semi(rust::Expr::Assign {
            target: Box::new(rust::Expr::Path(Path::single(&bound))),
            value: Box::new(rust::Expr::Call {
                callee: Box::new(rust::Expr::Path(Path::single("Some"))),
                args: vec![rust::Expr::Try(Box::new(read_for(&slot.name, &slot.kind)?))],
            }),
        }));
        arm.push(rust::Statement::Semi(rust::Expr::Try(Box::new(call(
            "finish_pid",
            vec![rust::Expr::Path(Path::single("pid"))],
        )))));
        chain = Some(rust::Block {
            statements: vec![rust::Statement::Semi(rust::Expr::If {
                condition: Box::new(rust::Expr::Binary {
                    op: rust::BinaryOp::Eq,
                    lhs: Box::new(rust::Expr::Field {
                        receiver: Box::new(rust::Expr::Path(Path::single("pid"))),
                        name: Ident::new("pid"),
                    }),
                    rhs: Box::new(rust::Expr::Literal(Literal::int(i128::from(
                        slot.member_id,
                    )))),
                }),
                then: rust::Block { statements: arm },
                otherwise: chain,
            })],
        });
    }

    // Three gates in front of the id chain, in the order the clauses
    // rank them.
    //
    // `PID_IGNORE` first: §7.4.1.2.2 says "All consumers of this Data
    // Representation shall ignore parameters with this ID" and states no
    // exception, so it outranks the must-understand bit that `unknown`
    // would refuse on.
    //
    // Then `is_user_member`, which is false for a parameter carrying
    // `FLAG_IMPL_EXTENSION` and for one inside the reserved range. Both
    // are legal for a peer to send and neither is a member of this type,
    // so both take the unknown path instead of being matched by their
    // low bits against a member that happens to share them.
    let matched = rust::Block {
        statements: chain.map_or_else(Vec::new, |chain| chain.statements),
    };
    let tail = rust::Block {
        statements: vec![rust::Statement::Semi(rust::Expr::If {
            condition: Box::new(rust::Expr::Field {
                receiver: Box::new(rust::Expr::Path(Path::single("pid"))),
                name: Ident::new("ignore"),
            }),
            then: skip_parameter_block,
            otherwise: Some(rust::Block {
                statements: vec![rust::Statement::Semi(rust::Expr::If {
                    condition: Box::new(rust::Expr::MethodCall {
                        receiver: Box::new(rust::Expr::Path(Path::single("pid"))),
                        method: Ident::new("is_user_member"),
                        args: Vec::new(),
                    }),
                    then: matched,
                    otherwise: Some(unknown),
                })],
            }),
        })],
    };
    let body = vec![
        let_statement(
            "pid",
            rust::Expr::Try(Box::new(call("read_pid", Vec::new()))),
        ),
        rust::Statement::Semi(rust::Expr::If {
            condition: Box::new(rust::Expr::Field {
                receiver: Box::new(rust::Expr::Path(Path::single("pid"))),
                name: Ident::new("end"),
            }),
            then: rust::Block {
                statements: vec![rust::Statement::Semi(rust::Expr::Assign {
                    target: Box::new(rust::Expr::Path(Path::single("ended"))),
                    value: Box::new(rust::Expr::Literal(Literal::Bool(true))),
                })],
            },
            otherwise: Some(tail),
        }),
    ];

    statements.push(rust::Statement::Semi(rust::Expr::While {
        condition: Box::new(rust::Expr::Not(Box::new(rust::Expr::Path(Path::single(
            "ended",
        ))))),
        body: rust::Block { statements: body },
    }));
    Ok(statements)
}

/// What the two `@mutable` loops need of a member: the id its header
/// carries, the name its slot takes, and how to read it.
struct MutableSlot {
    /// The member's name, as the source wrote it.
    name: String,
    /// What reading it takes.
    kind: MemberWrite,
    /// The id its header carries.
    member_id: u32,
}

/// The write call one member takes.
///
/// One arm per primitive rather than a formatted name: the writer for a
/// `u32` and the writer for an `i32` are different functions, and a
/// name assembled from a string would hide a wrong one until the bytes
/// came out different.
fn write_for(
    receiver: rust::Expr,
    name: &str,
    kind: &MemberWrite,
) -> Result<rust::Statement, EncodeError> {
    let value = rust::Expr::Field {
        receiver: Box::new(receiver),
        name: Ident::new(&rust::escape_keyword(name)),
    };
    write_value_to("w", name, kind, value, false)
}

/// The same, naming the writer it goes to.
///
/// The KeyHash builds its own holder rather than writing to the
/// sample's writer: the holder is big-endian regardless of what the
/// sample is, and sharing one writer would make the hash depend on the
/// sample's byte order.
///
/// `borrowed` says the expression is already a `&T` — a binding out of
/// `match &self.<name> { Some(v) => … }`, or a closure's element —
/// rather than the field itself. It decides one thing per shape and no
/// more: a primitive is dereferenced, a string is not re-borrowed.
/// Passing the wrong one does not compile, which is the point of
/// carrying it rather than guessing from the shape of the expression.
///
/// `pub(crate)`: `emit::interface_rust` hand-assembles the DDS-RPC 1.0
/// §7.5.1.1 topic types the same way this file assembles an aggregate's
/// codec, member by member, and calls this rather than re-deriving how
/// one value goes onto a `Writer`.
pub(crate) fn write_value_to(
    sink: &str,
    name: &str,
    kind: &MemberWrite,
    value: rust::Expr,
    borrowed: bool,
) -> Result<rust::Statement, EncodeError> {
    write_value_to_sink(sink, SinkShape::Reference, name, kind, value, borrowed)
}

/// Whether the name the writes go to is already a `&mut Writer`.
///
/// Every arm but one calls a method on the sink, and a method call
/// auto-references. `MemberWrite::Nested` hands it over as an *argument* —
/// `self.k.encode(sink)` — and there Rust does not: a local `Writer` where a
/// `&mut Writer` is wanted is `error[E0308]`, in generated code, which is the
/// worst place for it. The key-holder path is the one with a local, so it says
/// so instead of the emitter guessing from the name.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum SinkShape {
    /// A `&mut Writer` parameter — pass it along as it is.
    Reference,
    /// A local `Writer` binding — take a reference where one is needed.
    Owned,
}

fn write_value_to_sink(
    sink: &str,
    shape: SinkShape,
    name: &str,
    kind: &MemberWrite,
    value: rust::Expr,
    borrowed: bool,
) -> Result<rust::Statement, EncodeError> {
    let (call, fallible) = write_call(sink, shape, name, kind, value, borrowed)?;
    // A write that can refuse is followed by `?`. Dropping the `Result`
    // would compile — it is only a warning — and would put the narrowed
    // character on the wire, which is the defect the runtime stopped
    // committing.
    Ok(rust::Statement::Semi(if fallible {
        rust::Expr::Try(Box::new(call))
    } else {
        call
    }))
}

/// The call one value's write is, and whether it returns a `Result`.
///
/// One arm per shape, and the shape decides three things at once: which
/// runtime function, how the value is handed over, and whether the
/// result has to be looked at.
///
/// zerodds-lint: recursion-depth 64 (collection nesting of the owned
/// `MemberWrite` tree, via `element_writer`). That tree is built by
/// `write_of_at`, whose `MAX_WRITE_DEPTH` bounds its height, and it
/// is owned, so it holds no cycle of its own.
fn write_call(
    sink: &str,
    shape: SinkShape,
    name: &str,
    kind: &MemberWrite,
    value: rust::Expr,
    borrowed: bool,
) -> Result<(rust::Expr, bool), EncodeError> {
    let sink_expr = || rust::Expr::Path(Path::single(sink));
    let deref = |value: rust::Expr| {
        if borrowed {
            rust::Expr::Deref(Box::new(value))
        } else {
            value
        }
    };
    // A `String` field goes in by reference and a `&String` binding is
    // one already; both coerce to the `&str` the runtime takes. A
    // collection is the same question one type up — `&Vec<T>` and
    // `&[T; N]` both coerce to the `&[T]` the runtime takes.
    let by_ref = |value: rust::Expr| {
        if borrowed {
            value
        } else {
            rust::Expr::Ref {
                target: Box::new(value),
                mutable: false,
            }
        }
    };
    Ok(match kind {
        MemberWrite::External(inner) => {
            return write_call(
                sink,
                shape,
                name,
                inner,
                rust::Expr::MethodCall {
                    receiver: Box::new(value),
                    method: Ident::new("as_ref"),
                    args: Vec::new(),
                },
                true,
            );
        }
        MemberWrite::Primitive(primitive) => (
            rust::Expr::MethodCall {
                receiver: Box::new(sink_expr()),
                method: Ident::new(writer_name(*primitive)),
                args: vec![deref(value)],
            },
            // `char` is one byte and `wchar` two, and a code point that
            // fits neither is refused rather than narrowed —
            // §7.2.2.2.1.2.2 and §7.2.2.2.1.2.5.
            matches!(primitive, PrimitiveKind::Char | PrimitiveKind::WChar),
        ),
        MemberWrite::String { bound, .. } => {
            // A bounded string writes the same bytes as an unbounded one
            // of the same length; the bound is a promise about the
            // source, not a difference on the wire. The runtime takes it
            // as a second argument and not as part of the name — and
            // refuses a value that overruns it, which is why the bounded
            // form is fallible and the plain one is not.
            let (method, mut args, fallible) = match bound {
                Some(bound) => (
                    "write_string_bounded",
                    vec![rust::Expr::Literal(Literal::int(*bound))],
                    true,
                ),
                None => ("write_string", Vec::new(), false),
            };
            let mut all = vec![by_ref(value)];
            all.append(&mut args);
            (
                rust::Expr::MethodCall {
                    receiver: Box::new(sink_expr()),
                    method: Ident::new(method),
                    args: all,
                },
                fallible,
            )
        }
        MemberWrite::Fixed {
            digits,
            scale,
            count,
        } => (
            rust::Expr::MethodCall {
                receiver: Box::new(sink_expr()),
                method: Ident::new("write_fixed"),
                args: vec![
                    by_ref(value),
                    rust::Expr::Literal(Literal::int(i128::from(*digits))),
                    rust::Expr::Literal(Literal::int(i128::from(*scale))),
                    fixed_digits_expr(*count),
                ],
            },
            // Always fallible: a value the declaration cannot carry is
            // refused rather than truncated, and a truncated decimal is the
            // wrong number with nothing to notice it by.
            true,
        ),
        MemberWrite::WString {
            bound,
            policy,
            units,
            order,
            position,
            ..
        } => {
            // Every write here is fallible, bounded or not: the
            // `CHARACTER_COUNT` cell refuses a code point outside the
            // BMP rather than truncating it to sixteen bits the way
            // Fast CDR does, so even an unbounded `wstring` can say no.
            //
            // `WstringCodeUnits::Utf8` names the runtime's UTF-8 pair,
            // which takes no order: UTF-8 octets have no byte order of
            // their own, and C1 and C3 are vacuous because there is no
            // fixed-width code unit for either to speak about.
            let (method, mut args) = match (units, bound) {
                (WstringCodeUnits::Utf8, Some(bound)) => (
                    "write_wstring_utf8_bounded",
                    vec![rust::Expr::Literal(Literal::int(*bound))],
                ),
                (WstringCodeUnits::Utf8, None) => ("write_wstring_utf8", Vec::new()),
                (WstringCodeUnits::Utf16, Some(bound)) => (
                    "write_wstring_bounded",
                    vec![rust::Expr::Literal(Literal::int(*bound))],
                ),
                (WstringCodeUnits::Utf16, None) => ("write_wstring", Vec::new()),
            };
            let mut all = vec![by_ref(value)];
            all.append(&mut args);
            all.push(wstring_policy_expr(*policy));
            if *units == WstringCodeUnits::Utf16 {
                all.push(wstring_order_expr(*order));
            }
            all.push(wire_position_expr(name, *position)?);
            (
                rust::Expr::MethodCall {
                    receiver: Box::new(sink_expr()),
                    method: Ident::new(method),
                    args: all,
                },
                true,
            )
        }
        MemberWrite::Enumeration { bits } => (
            rust::Expr::MethodCall {
                receiver: Box::new(sink_expr()),
                method: Ident::new(enum_writer(*bits)),
                args: vec![deref(value)],
            },
            false,
        ),
        // The bound goes into the call and the holder is widened to the
        // `u64` the runtime takes. Fallible: a value with a bit set
        // above the bound is refused rather than masked, which is what
        // `runtime/c/src/writer.c` does with `ZDW_RANGE`.
        MemberWrite::Bitmask { bits, .. } => (
            rust::Expr::MethodCall {
                receiver: Box::new(sink_expr()),
                method: Ident::new("write_bitmask"),
                args: vec![
                    rust::Expr::Literal(Literal::int(i128::from(*bits))),
                    widen_to_u64(deref(value), *bits),
                ],
            },
            true,
        ),
        // An aggregate encodes itself. There is no `Writer::write_nested`
        // and there was never going to be one: the nested type's own
        // `encode` is what carries its plan, and a runtime function
        // could not have known it.
        //
        // Under a per-sample plan the call names the nested type's
        // **body** and hands it the sample's decision: the same nested
        // type is framed short in a sample below RTI's step and extended
        // in one past it, so a call through the public entry would frame
        // it by the nested type's own size instead of the sample's.
        MemberWrite::Nested {
            codec, extended, ..
        } => {
            let mut args = vec![match shape {
                SinkShape::Reference => sink_expr(),
                SinkShape::Owned => rust::Expr::Ref {
                    target: Box::new(sink_expr()),
                    mutable: true,
                },
            }];
            args.extend(extended.argument());
            (
                rust::Expr::MethodCall {
                    receiver: Box::new(value),
                    method: Ident::new(&nested_method(codec, *extended)),
                    // The one place the sink is an argument and not a
                    // receiver, so the one place auto-referencing does
                    // not happen for us.
                    args,
                },
                true,
            )
        }
        MemberWrite::Sequence {
            element,
            bound,
            delimited,
            packed,
            carries_length,
            ..
        } => {
            // `sequence<T, N>`: the count is checked before a byte is
            // written. Table 17 makes `O2.length <= T1.length` the
            // condition for constructing one, the wire carries no bound
            // to compare against, and a sender that wrote more elements
            // than its own declaration allows has written a value of a
            // type nobody declared. G16, on the write side.
            let method = match (bound, delimited) {
                (Some(_), true) => "write_delimited_sequence_bounded",
                (Some(_), false) => "write_sequence_bounded",
                (None, true) => "write_delimited_sequence",
                (None, false) => "write_sequence",
            };
            let mut args = vec![by_ref(value)];
            if let Some(bound) = bound {
                args.push(rust::Expr::Literal(Literal::int(*bound)));
            }
            args.push(sequence_element_writer(
                name,
                element,
                *packed,
                *carries_length,
            )?);
            (
                rust::Expr::MethodCall {
                    receiver: Box::new(sink_expr()),
                    method: Ident::new(method),
                    args,
                },
                true,
            )
        }
        MemberWrite::Map {
            key,
            value: entry_value,
            bound,
            delimited,
            ascending,
            ..
        } => {
            // The count on the wire is the **entry** count and not the
            // number of halves — §7.4.1.1.4 defines a `map<K, V>` by
            // equivalence to a `sequence` whose element is the pair —
            // and the runtime writes it from `BTreeMap::len`, so there
            // is nothing to count here. `map<K, V, N>` bounds that same
            // number, which is why the bounded form passes it through
            // unchanged from the sequence's shape.
            let method = match (bound, delimited) {
                (Some(_), true) => "write_delimited_map_bounded",
                (Some(_), false) => "write_map_bounded",
                (None, true) => "write_delimited_map",
                (None, false) => "write_map",
            };
            // Held as its entries, the map goes out in the order it is held; ascending by key
            // is a view over them, and a `BTreeMap` is ascending already.
            let held = match crate::adapter::cst::rust_map_form() {
                crate::adapter::cst::RustMapForm::Entries if *ascending => rust::Expr::Ref {
                    target: Box::new(rust::Expr::Call {
                        callee: Box::new(rust::Expr::Path(Path::new(&[
                            "zerodds_wire",
                            "AscendingKey",
                        ]))),
                        args: vec![by_ref(value)],
                    }),
                    mutable: false,
                },
                _ => by_ref(value),
            };
            let mut args = vec![held];
            if let Some(bound) = bound {
                args.push(rust::Expr::Literal(Literal::int(*bound)));
            }
            args.push(element_writer(name, key)?);
            args.push(element_writer(name, entry_value)?);
            (
                rust::Expr::MethodCall {
                    receiver: Box::new(sink_expr()),
                    method: Ident::new(method),
                    args,
                },
                true,
            )
        }
        MemberWrite::Array {
            element,
            dims,
            delimited,
            packed,
        } => {
            // No length: an array's element count is in the type, not on
            // the wire. XTypes 1.3 §7.4.3.5. A multi-dimensional array is
            // one flat run (G4), so the delimiter — where rule (9) asks
            // for one — goes round the whole thing and the inner
            // dimensions are plain.
            (
                rust::Expr::MethodCall {
                    receiver: Box::new(sink_expr()),
                    method: Ident::new(if *delimited {
                        "write_delimited_array"
                    } else {
                        "write_array"
                    }),
                    args: vec![
                        by_ref(value),
                        array_element_writer(name, element, dims.len().saturating_sub(1), *packed)?,
                    ],
                },
                true,
            )
        }
        MemberWrite::Unsupported(kind) => {
            return Err(EncodeError::NoWriter {
                member: name.to_string(),
                kind,
            });
        }
    })
}

/// The digit-count axis as the generated code names it.
///
/// A path and not a boolean: the emitted call says `FixedDigits::Declared`
/// or `FixedDigits::ValuePrecision`, so a reader of the generated file sees
/// which reading of CORBA 3.3 part 2 §9.3.2.8 it writes without going back
/// to the plan that chose it.
fn fixed_digits_expr(count: FixedDigitCount) -> rust::Expr {
    rust::Expr::Path(Path::new(&[
        "zerodds_wire",
        "FixedDigits",
        match count {
            FixedDigitCount::Declared => "Declared",
            FixedDigitCount::ValuePrecision => "ValuePrecision",
            FixedDigitCount::ValueStringDigits => "ValueStringDigits",
        },
    ]))
}

/// `|w, v| <the element's write>`, always returning a `Result`.
///
/// A closure and not a named path. `Writer::write_i32` takes an `i32` by
/// value where an element function takes a `&i32`; a nested aggregate's
/// write is a method on the *value*; and a collection's write takes its
/// own element function as a second argument, which no path can supply.
/// The runtime's element functions were made `Fn`-generic and the CST
/// gained `Expr::Closure` for exactly this, and then nothing emitted
/// one — `element_writer_name` kept naming paths and refused the two
/// shapes that have none. That refusal is `AL-9`, and this is it closed.
///
/// zerodds-lint: recursion-depth 64 (collection nesting of the owned
/// `MemberWrite` tree, via `write_call`). That tree is built by
/// `write_of_at`, whose `MAX_WRITE_DEPTH` bounds its height, and it
/// is owned, so it holds no cycle of its own.
fn element_writer(at: &str, element: &MemberWrite) -> Result<rust::Expr, EncodeError> {
    // A `wstring` element is written at the element position, which is
    // substituted here for the same reason the `wchar` element's
    // framing is: `write_call` was handed a member's shape, and a
    // caller that could leave the position alone would take the
    // member's answer for an element by not asking.
    let substituted = as_element(element);
    let element = substituted.as_ref().unwrap_or(element);
    // A `wchar` element takes the element framing, which under TAO's
    // dialect is not the member's. `write_call` names the member's
    // method, so the element's is substituted here rather than passed
    // down through five shapes that do not care.
    if matches!(element, MemberWrite::Primitive(PrimitiveKind::WChar)) {
        return Ok(rust::Expr::Path(Path::new(&[
            "Writer",
            "write_wchar_element",
        ])));
    }
    let (call, fallible) = write_call(
        "w",
        SinkShape::Reference,
        at,
        element,
        rust::Expr::Path(Path::single("v")),
        true,
    )?;
    Ok(rust::Expr::Closure {
        params: vec![Ident::new("w"), Ident::new("v")],
        // An infallible write followed by `Ok(())`: one element-function
        // shape rather than two, because three of the four element kinds
        // can refuse and a generator choosing between two shapes would
        // be choosing per construct. Not `Ok(call)`: that hands the
        // write's `()` to a function, which is `clippy::unit_arg` in
        // every crate that includes the generated text.
        body: Box::new(if fallible {
            call
        } else {
            rust::Expr::Block(rust::Block {
                statements: vec![
                    rust::Statement::Semi(call),
                    rust::Statement::Tail(rust::Expr::Call {
                        callee: Box::new(rust::Expr::Path(Path::single("Ok"))),
                        args: vec![rust::Expr::Tuple(Vec::new())],
                    }),
                ],
            })
        }),
    })
}

/// The element function for an array with `remaining` dimensions still
/// to unwrap.
///
/// `long grid[2][3]` is `[[i32; 3]; 2]`, so the outer `write_array`
/// hands each row to a function that writes a row.
///
/// `packed` wraps each level's element in a
/// `Writer::begin_packed_element` / `finish_packed_element` pair, at the
/// same site `zerodds-wire-codec`'s `write_array_dimension` writes them:
/// the row and every cell inside it each begin their own alignment block.
///
/// zerodds-lint: recursion-depth 8 (one level per dimension of the array
/// declarator: `remaining` falls by one per call and the cell ends it; the
/// depth is the number of dimensions the IDL writes, not a property of a
/// sample)
fn array_element_writer(
    at: &str,
    element: &MemberWrite,
    remaining: usize,
    packed: bool,
) -> Result<rust::Expr, EncodeError> {
    if remaining == 0 {
        // A cell: the element position and the packed block, and never an
        // element length — only a `sequence` has that axis.
        return sequence_element_writer(at, element, packed, false);
    }
    let inner = array_element_writer(at, element, remaining - 1, packed)?;
    let call = rust::Expr::MethodCall {
        receiver: Box::new(rust::Expr::Path(Path::single("w"))),
        method: Ident::new("write_array"),
        args: vec![rust::Expr::Path(Path::single("v")), inner],
    };
    if !packed {
        return Ok(rust::Expr::Closure {
            params: vec![Ident::new("w"), Ident::new("v")],
            body: Box::new(call),
        });
    }
    Ok(rust::Expr::Closure {
        params: vec![Ident::new("w"), Ident::new("v")],
        body: Box::new(rust::Expr::Block(rust::Block {
            statements: vec![
                let_statement("zd_origin", begin_packed("w")),
                rust::Statement::Semi(rust::Expr::Try(Box::new(call))),
                rust::Statement::Semi(finish_packed("w", "zd_origin")),
                rust::Statement::Tail(ok_unit()),
            ],
        })),
    })
}

/// The element function a `sequence`'s write takes, with the two HDDS
/// axes the reference codec puts around each element.
///
/// With neither axis selected this is [`element_writer`] unchanged, so
/// every other dialect's emitted code is the same text it was.
fn sequence_element_writer(
    at: &str,
    element: &MemberWrite,
    packed: bool,
    carries_length: bool,
) -> Result<rust::Expr, EncodeError> {
    if !packed && !carries_length {
        return element_writer(at, element);
    }
    let (call, fallible) = element_write_call(at, element)?;
    let mut statements = Vec::new();
    if packed {
        statements.push(let_statement("zd_origin", begin_packed("w")));
    }
    if carries_length {
        statements.push(let_statement(
            "zd_length",
            rust::Expr::MethodCall {
                receiver: Box::new(rust::Expr::Path(Path::single("w"))),
                method: Ident::new("write_dheader"),
                args: Vec::new(),
            },
        ));
    }
    statements.push(rust::Statement::Semi(if fallible {
        rust::Expr::Try(Box::new(call))
    } else {
        call
    }));
    if carries_length {
        statements.push(rust::Statement::Semi(finish_dheader("w", "zd_length")));
    }
    if packed {
        statements.push(rust::Statement::Semi(finish_packed("w", "zd_origin")));
    }
    statements.push(rust::Statement::Tail(ok_unit()));
    Ok(rust::Expr::Closure {
        params: vec![Ident::new("w"), Ident::new("v")],
        body: Box::new(rust::Expr::Block(rust::Block { statements })),
    })
}

/// The write one collection element is, at the element position, and
/// whether it returns a `Result`.
///
/// [`write_call`] names a `wchar`'s *member* framing; a collection
/// element takes the other one, so it is substituted here. A `wstring`
/// element has its position rewritten by [`as_element`] first, for the
/// same reason.
fn element_write_call(at: &str, element: &MemberWrite) -> Result<(rust::Expr, bool), EncodeError> {
    let substituted = as_element(element);
    let element = substituted.as_ref().unwrap_or(element);
    if matches!(element, MemberWrite::Primitive(PrimitiveKind::WChar)) {
        return Ok((
            rust::Expr::Call {
                callee: Box::new(rust::Expr::Path(Path::new(&[
                    "Writer",
                    "write_wchar_element",
                ]))),
                args: vec![
                    rust::Expr::Path(Path::single("w")),
                    rust::Expr::Path(Path::single("v")),
                ],
            },
            true,
        ));
    }
    write_call(
        "w",
        SinkShape::Reference,
        at,
        element,
        rust::Expr::Path(Path::single("v")),
        true,
    )
}

/// `w.begin_packed_element()`.
fn begin_packed(sink: &str) -> rust::Expr {
    rust::Expr::MethodCall {
        receiver: Box::new(rust::Expr::Path(Path::single(sink))),
        method: Ident::new("begin_packed_element"),
        args: Vec::new(),
    }
}

/// `w.finish_packed_element(saved)`.
fn finish_packed(sink: &str, saved: &str) -> rust::Expr {
    rust::Expr::MethodCall {
        receiver: Box::new(rust::Expr::Path(Path::single(sink))),
        method: Ident::new("finish_packed_element"),
        args: vec![rust::Expr::Path(Path::single(saved))],
    }
}

/// `w.finish_dheader(header)`.
fn finish_dheader(sink: &str, header: &str) -> rust::Expr {
    rust::Expr::MethodCall {
        receiver: Box::new(rust::Expr::Path(Path::single(sink))),
        method: Ident::new("finish_dheader"),
        args: vec![rust::Expr::Path(Path::single(header))],
    }
}

/// `Ok(())`.
fn ok_unit() -> rust::Expr {
    rust::Expr::Call {
        callee: Box::new(rust::Expr::Path(Path::single("Ok"))),
        args: vec![rust::Expr::Tuple(Vec::new())],
    }
}

/// Wrap a generated `encode`/`decode` body in a packed-element block.
///
/// `ElementPadding::Packed` makes every aggregate and union value its own
/// alignment block: its members align from its own first octet, not from
/// the encapsulation, and the value is not padded up to its own alignment
/// before it starts. `sink` is `w` on the write side and `r` on the read
/// side.
///
/// The `finish` goes before the body's final statement — the `Ok(…)` the
/// body builds — so the restored origin is in effect for whatever the
/// caller writes after the value. A body that returns early on an error
/// leaves the origin displaced, which the caller discards with the
/// buffer: the same shape `zerodds-wire-codec`'s `?` between begin and
/// finish has.
fn wrap_packed_body(
    mut statements: Vec<rust::Statement>,
    sink: &str,
    packed: bool,
) -> Vec<rust::Statement> {
    if !packed {
        return statements;
    }
    let tail = statements.pop();
    let mut out = Vec::with_capacity(statements.len() + 3);
    out.push(let_statement("zd_origin", begin_packed(sink)));
    out.extend(statements);
    out.push(rust::Statement::Semi(finish_packed(sink, "zd_origin")));
    if let Some(tail) = tail {
        out.push(tail);
    }
    out
}

/// The same shape, reached as a collection element rather than as a
/// member.
///
/// `None` where the shape carries no position, which is every shape but
/// one. A `Some` is a shape whose emitted call names a position and
/// therefore has to be rewritten before the call is built — not a
/// difference in the octets today, because all nine measured
/// implementations answer the two positions alike on all four `wstring`
/// axes, and the rewrite is what keeps that a measurement rather than a
/// thing nobody asked.
fn as_element(element: &MemberWrite) -> Option<MemberWrite> {
    match element {
        MemberWrite::WString {
            bound,
            narrow,
            policy,
            units,
            order,
            position: _,
        } => Some(MemberWrite::WString {
            bound: *bound,
            narrow: narrow.clone(),
            policy: *policy,
            units: *units,
            order: *order,
            position: WirePosition::Element,
        }),
        _ => None,
    }
}

/// `zerodds_wire::WstringLengthPolicy::new(C1, C2, C3)`.
///
/// The constructor and not one of the five named constants, for every
/// cell alike. A table from cell to name is a table that can be wrong
/// about a cell, and the three cells no implementation writes have no
/// name to look up — which is exactly the shape that left the ROS cell
/// unreachable while the names were the values.
///
/// Absolute paths rather than imports, because the generated file's
/// import list is built by each consumer of this emitter and a new name
/// in it would have to be added in each one.
fn wstring_policy_expr(policy: WstringLengthPolicy) -> rust::Expr {
    let counts = match policy.counts {
        WstringLengthCounts::Octets => "Octets",
        WstringLengthCounts::Characters => "Characters",
    };
    let terminator = match policy.terminator {
        WstringTerminator::Absent => "Absent",
        WstringTerminator::Present => "Present",
    };
    let unit = match policy.unit {
        WstringUnitWidth::Two => "Two",
        WstringUnitWidth::Four => "Four",
    };
    rust::Expr::Call {
        callee: Box::new(rust::Expr::Path(Path::new(&[
            "zerodds_wire",
            "WstringLengthPolicy",
            "new",
        ]))),
        args: vec![
            rust::Expr::Path(Path::new(&["zerodds_wire", "WstringLengthCounts", counts])),
            rust::Expr::Path(Path::new(&[
                "zerodds_wire",
                "WstringTerminator",
                terminator,
            ])),
            rust::Expr::Path(Path::new(&["zerodds_wire", "WstringUnitWidth", unit])),
        ],
    }
}

/// `zerodds_wire::WstringUnitOrder::<C4>` — the fourth axis.
fn wstring_order_expr(order: WstringUnitOrder) -> rust::Expr {
    rust::Expr::Path(Path::new(&[
        "zerodds_wire",
        "WstringUnitOrder",
        match order {
            WstringUnitOrder::Stream => "Stream",
            WstringUnitOrder::BigEndianAlways => "BigEndianAlways",
            WstringUnitOrder::ByteOrderMark => "ByteOrderMark",
            WstringUnitOrder::HostUnswapped => "HostUnswapped",
        },
    ]))
}

/// `zerodds_wire::WirePosition::<position>`.
///
/// The two positions a `wstring` can be reached at, and an error for
/// the four it cannot. Not a fallback to `Member`: a position that
/// silently became the member's is precisely how a `wchar` element got
/// a member's framing through five emitters, and the runtime's own
/// enumeration has no name for a `wstring` in a parameter sentinel to
/// map onto.
fn wire_position_expr(member: &str, position: WirePosition) -> Result<rust::Expr, EncodeError> {
    let name = match position {
        WirePosition::Member => "Member",
        WirePosition::Element => "Element",
        WirePosition::Discriminator
        | WirePosition::KeyHolderMember
        | WirePosition::Sentinel
        | WirePosition::ExtendedMarker => {
            return Err(EncodeError::NoWriter {
                member: member.to_string(),
                kind: "a wstring at a position the wire runtime has no name for",
            });
        }
    };
    Ok(rust::Expr::Path(Path::new(&[
        "zerodds_wire",
        "WirePosition",
        name,
    ])))
}

/// The integer writer an enumeration's bit bound calls for.
const fn enum_writer(bits: u16) -> &'static str {
    match bits {
        0..=8 => "write_u8",
        9..=16 => "write_u16",
        17..=32 => "write_u32",
        _ => "write_u64",
    }
}

/// The expression that reads one value, of type `Result<T, WireError>`.
///
/// The narrowing §7.2.2.7 asks for is part of this expression and not a
/// step after it, so a member and a collection element get the same
/// treatment from one place — the element position is the one
/// §7.3.1.2.1.12.4 names and the one OpenDDS implements separately from
/// the collection's own bound.
///
/// zerodds-lint: recursion-depth 64 — one frame per external-member nesting
/// level; the shared lowering bounds that nesting before it reaches here.
///
/// `pub(crate)`: see [`write_value_to`].
pub(crate) fn read_for(name: &str, kind: &MemberWrite) -> Result<rust::Expr, EncodeError> {
    let read = read_whole(name, kind)?;
    match narrowing_of(kind) {
        Some(narrow) => narrowed(read, kind, narrow, name),
        None => Ok(read),
    }
}

/// The narrowing of a shape, where the shape has one.
const fn narrowing_of(kind: &MemberWrite) -> Option<&Narrowing> {
    match kind {
        MemberWrite::String { narrow, .. }
        | MemberWrite::WString { narrow, .. }
        | MemberWrite::Sequence { narrow, .. }
        | MemberWrite::Map { narrow, .. } => narrow.as_ref(),
        _ => None,
    }
}

/// `{ let v = <read>?; match r.fit_…(v, bound, TryConstruct::X)? { … } }`
///
/// The whole value comes off the wire first and the bound is applied
/// afterwards. A reader that stopped at its own bound would leave the
/// rest in the stream and put every following member at the wrong
/// offset — OpenDDS reads all twelve characters of `"Hello World!"`
/// before it defaults or truncates, and the members after it come back
/// correct, which is the property `try_construct_behaviour.md` singles
/// out.
fn narrowed(
    read: rust::Expr,
    kind: &MemberWrite,
    narrow: &Narrowing,
    name: &str,
) -> Result<rust::Expr, EncodeError> {
    let fit = match kind {
        MemberWrite::String { .. } => "fit_string",
        // The bound counts UTF-16 code units and not octets, so the
        // runtime has its own narrowing rather than `fit_string` with
        // another argument: one function taking "octets or units" would
        // be a call site deciding which by getting an argument right.
        MemberWrite::WString { .. } => "fit_wstring",
        MemberWrite::Sequence { .. } => "fit_sequence",
        // Table 11 names `map` in the same breath as `sequence`, and
        // the bound counts entries, so the runtime has its own `fit_map`
        // rather than a `fit_sequence` over the halves.
        MemberWrite::Map { .. } => match crate::adapter::cst::rust_map_form() {
            crate::adapter::cst::RustMapForm::Entries => "fit_map_entries",
            crate::adapter::cst::RustMapForm::BTreeMap => "fit_map",
        },
        // Table 11: TRIM "only applies to elements or members of type
        // string, wide string, sequence, or map", and the front end
        // refuses the annotation anywhere else
        // (`validate.rs::refuse_trim`). No other shape carries a
        // `Narrowing` at all, so this arm is a model that did not come
        // through the front end.
        _ => {
            return Err(EncodeError::NoWriter {
                member: name.to_string(),
                kind: "a bound on a type that cannot have one",
            });
        }
    };
    let bound = rust::escape_keyword(name);
    // USE_DEFAULT is the only behaviour that needs a value here; under
    // DISCARD `fit_…` has already returned `Err(WireError::Discarded)`
    // and under TRIM it has returned the trimmed value.
    let default = match (&narrow.default, narrow.behaviour) {
        (Some(default), _) => default.clone(),
        (None, TryConstruct::UseDefault) => {
            return Err(EncodeError::NoWriter {
                member: name.to_string(),
                kind: "a `@try_construct(USE_DEFAULT)` position whose default this column \
                       cannot spell",
            });
        }
        // Under DISCARD and TRIM the runtime never answers `Default`,
        // so the arm exists only because the match has to be
        // exhaustive. A refusal and not a panic: a panic in a decoder is
        // a denial of service a peer can send, and this one would mean
        // the generated code and the runtime disagree about what the
        // behaviour was.
        (None, _) => rust::Expr::Path(Path::single("__unreachable")),
    };
    Ok(rust::Expr::Block(rust::Block {
        statements: vec![
            let_statement(name, rust::Expr::Try(Box::new(read))),
            rust::Statement::Tail(rust::Expr::Match {
                scrutinee: Box::new(rust::Expr::Try(Box::new(call(
                    fit,
                    vec![
                        rust::Expr::Path(Path::single(&bound)),
                        rust::Expr::Literal(Literal::int(narrow.bound)),
                        rust::Expr::Path(Path::new(&[
                            "TryConstruct",
                            try_construct_variant(narrow.behaviour),
                        ])),
                    ],
                )))),
                arms: vec![
                    rust::MatchArm {
                        pattern: rust::Pattern::TupleStruct {
                            path: Path::new(&["Narrowed", "Value"]),
                            elements: vec![rust::Pattern::Binding(Ident::new(&bound))],
                        },
                        guard: None,
                        body: rust::Expr::Call {
                            callee: Box::new(rust::Expr::Path(Path::single("Ok"))),
                            args: vec![rust::Expr::Path(Path::single(&bound))],
                        },
                    },
                    rust::MatchArm {
                        pattern: rust::Pattern::Path(Path::new(&["Narrowed", "Default"])),
                        guard: None,
                        body: if narrow.behaviour == TryConstruct::UseDefault {
                            rust::Expr::Call {
                                callee: Box::new(rust::Expr::Path(Path::single("Ok"))),
                                args: vec![default],
                            }
                        } else {
                            rust::Expr::Call {
                                callee: Box::new(rust::Expr::Path(Path::single("Err"))),
                                args: vec![rust::Expr::Call {
                                    callee: Box::new(rust::Expr::Path(Path::new(&[
                                        "WireError",
                                        "refused",
                                    ]))),
                                    args: vec![
                                        call("position", Vec::new()),
                                        rust::Expr::Literal(Literal::string(
                                            "a narrowing that asked for no default and got one",
                                        )),
                                    ],
                                }],
                            }
                        },
                    },
                ],
            }),
        ],
    }))
}

/// The runtime's spelling of one behaviour.
const fn try_construct_variant(behaviour: TryConstruct) -> &'static str {
    match behaviour {
        TryConstruct::Discard => "Discard",
        TryConstruct::UseDefault => "UseDefault",
        TryConstruct::Trim => "Trim",
    }
}

/// The read itself, before any bound is applied.
///
/// zerodds-lint: recursion-depth 64 — one frame per external-member nesting
/// level; the shared lowering bounds that nesting before it reaches here.
fn read_whole(name: &str, kind: &MemberWrite) -> Result<rust::Expr, EncodeError> {
    Ok(match kind {
        MemberWrite::External(inner) => rust::Expr::MethodCall {
            receiver: Box::new(read_for(name, inner)?),
            method: Ident::new("map"),
            args: vec![rust::Expr::Path(Path::new(&["Box", "new"]))],
        },
        MemberWrite::Primitive(primitive) => call(reader_name(*primitive), Vec::new()),
        MemberWrite::String { .. } => call("read_string", Vec::new()),
        MemberWrite::Fixed {
            digits,
            scale,
            count,
        } => call(
            "read_fixed",
            vec![
                rust::Expr::Literal(Literal::int(i128::from(*digits))),
                rust::Expr::Literal(Literal::int(i128::from(*scale))),
                fixed_digits_expr(*count),
            ],
        ),
        MemberWrite::WString {
            policy,
            units,
            order,
            position,
            ..
        } => {
            // The mirror of `write_call`'s branch: `Utf8` names the
            // runtime's UTF-8 reader, which takes no order.
            if *units == WstringCodeUnits::Utf8 {
                call(
                    "read_wstring_utf8",
                    vec![
                        wstring_policy_expr(*policy),
                        wire_position_expr(name, *position)?,
                    ],
                )
            } else {
                call(
                    "read_wstring",
                    vec![
                        wstring_policy_expr(*policy),
                        wstring_order_expr(*order),
                        wire_position_expr(name, *position)?,
                    ],
                )
            }
        }
        MemberWrite::Enumeration { bits } => call(enum_reader(*bits), Vec::new()),
        // `read_bitmask` answers in the `u64` it reads into, and the
        // field is the holder the bound calls for, so the narrowing
        // rides on the `Result` rather than after it: `read_for` may
        // wrap this expression in a `try_construct` arm, and a cast
        // written outside would have been applied to the wrong thing.
        MemberWrite::Bitmask { bits, declared } => narrow_from_u64(
            call(
                "read_bitmask",
                vec![
                    rust::Expr::Literal(Literal::int(i128::from(*bits))),
                    rust::Expr::Literal(Literal::int(i128::from(*declared))),
                ],
            ),
            *bits,
        ),
        // The mirror of the encoder's `self.<field>.encode(w)`. The read
        // side needs no `extended`: `Reader::read_pid_as` takes the
        // header form off the wire, so one decoder serves both forms a
        // per-sample plan writes.
        MemberWrite::Nested {
            type_name, codec, ..
        } => rust::Expr::Call {
            callee: Box::new(rust::Expr::Path(Path::new(&[
                type_name.as_str(),
                &format!("decode{codec}"),
            ]))),
            args: vec![rust::Expr::Path(Path::single("r"))],
        },
        MemberWrite::Sequence {
            element,
            delimited,
            packed,
            carries_length,
            ..
        } => call(
            if *delimited {
                "read_delimited_sequence"
            } else {
                "read_sequence"
            },
            vec![sequence_element_reader(
                name,
                element,
                *packed,
                *carries_length,
            )?],
        ),
        MemberWrite::Map {
            key,
            value,
            delimited,
            ..
        } => call(
            match (crate::adapter::cst::rust_map_form(), *delimited) {
                (crate::adapter::cst::RustMapForm::Entries, true) => "read_delimited_map_entries",
                (crate::adapter::cst::RustMapForm::Entries, false) => "read_map_entries",
                (crate::adapter::cst::RustMapForm::BTreeMap, true) => "read_delimited_map",
                (crate::adapter::cst::RustMapForm::BTreeMap, false) => "read_map",
            },
            vec![element_reader(name, key)?, element_reader(name, value)?],
        ),
        MemberWrite::Array {
            element,
            dims,
            delimited,
            packed,
        } => call(
            // `_n`, not the `Vec` form: the field an array materialises
            // to is `[T; N]`, and `N` is inferred from it.
            if *delimited {
                "read_delimited_array_n"
            } else {
                "read_array_n"
            },
            vec![array_element_reader(
                name,
                element,
                dims.len().saturating_sub(1),
                *packed,
            )?],
        ),
        MemberWrite::Unsupported(kind) => {
            return Err(EncodeError::NoWriter {
                member: name.to_string(),
                kind,
            });
        }
    })
}

/// The element function a collection's read takes.
///
/// A path where one has the right shape — `Reader::read_i32` and
/// `Cell::decode` are both `fn(&mut Reader) -> Result<T, WireError>` —
/// and a closure where the read needs an argument of its own.
fn element_reader(at: &str, element: &MemberWrite) -> Result<rust::Expr, EncodeError> {
    // The element position, substituted for the reason
    // `element_writer` gives.
    let substituted = as_element(element);
    let element = substituted.as_ref().unwrap_or(element);
    // An element with a bound of its own takes a closure whatever its
    // shape: §7.3.1.2.1.12.4 gives the element position its own
    // behaviour, and a named path has nowhere to put it. This is the
    // split Table 17 draws and OpenDDS implements — the sequence's
    // length overflow and an element's bound overflow fail separately.
    if narrowing_of(element).is_some() {
        return Ok(rust::Expr::Closure {
            params: vec![Ident::new("r")],
            body: Box::new(read_for(at, element)?),
        });
    }
    Ok(match element {
        // As `element_writer`: the element's `wchar` reader, not the
        // member's.
        MemberWrite::Primitive(PrimitiveKind::WChar) => {
            rust::Expr::Path(Path::new(&["Reader", "read_wchar_element"]))
        }
        MemberWrite::Primitive(primitive) => {
            rust::Expr::Path(Path::new(&["Reader", reader_name(*primitive)]))
        }
        MemberWrite::String { .. } => rust::Expr::Path(Path::new(&["Reader", "read_string"])),
        // A closure and not a path, for the reason `wstring` needs one:
        // `read_fixed` takes the declaration and the digit count as
        // arguments, so there is no bare
        // `fn(&mut Reader) -> Result<T, WireError>` to hand over.
        MemberWrite::Fixed { .. } => rust::Expr::Closure {
            params: vec![Ident::new("r")],
            body: Box::new(read_for(at, element)?),
        },
        // A closure and not a path: `read_wstring` takes the cell and
        // the order as arguments, so there is no bare
        // `fn(&mut Reader) -> Result<T, WireError>` to hand over.
        MemberWrite::WString { .. } => rust::Expr::Closure {
            params: vec![Ident::new("r")],
            body: Box::new(read_for(at, element)?),
        },
        MemberWrite::Enumeration { bits } => {
            rust::Expr::Path(Path::new(&["Reader", enum_reader(*bits)]))
        }
        MemberWrite::Nested {
            type_name, codec, ..
        } => rust::Expr::Path(Path::new(&[type_name.as_str(), &format!("decode{codec}")])),
        // A closure for the reason `read_wstring` needs one: the bound
        // and the declared mask are arguments, so there is no bare
        // `fn(&mut Reader) -> Result<T, WireError>` to name.
        MemberWrite::Bitmask { .. }
        | MemberWrite::External(_)
        | MemberWrite::Sequence { .. }
        | MemberWrite::Array { .. }
        | MemberWrite::Map { .. } => rust::Expr::Closure {
            params: vec![Ident::new("r")],
            body: Box::new(read_for(at, element)?),
        },
        MemberWrite::Unsupported(kind) => {
            return Err(EncodeError::NoWriter {
                member: at.to_string(),
                kind,
            });
        }
    })
}

/// A bitmask's holder widened to the `u64` `Writer::write_bitmask`
/// takes.
///
/// `as u64` and not `u64::from`: the 64-bit holder is already a `u64`
/// and `u64::from(x)` on one is a type error, while the cast is
/// accepted for all four widths. Widening is exact in every one of
/// them, so nothing can be lost here.
fn widen_to_u64(value: rust::Expr, bits: u16) -> rust::Expr {
    if crate::adapter::cst::bitmask_primitive(bits) == rust::Primitive::U64 {
        return value;
    }
    rust::Expr::Cast {
        value: Box::new(value),
        type_: Box::new(rust::Type::Primitive(rust::Primitive::U64)),
    }
}

/// `Reader::read_bitmask`'s `u64` narrowed back to the holder the field
/// is declared as.
///
/// Exact for the same reason the writer's widening is: the runtime has
/// already refused every bit above the bound, so nothing this cast drops
/// could have been set.
fn narrow_from_u64(read: rust::Expr, bits: u16) -> rust::Expr {
    let holder = crate::adapter::cst::bitmask_primitive(bits);
    if holder == rust::Primitive::U64 {
        return read;
    }
    rust::Expr::MethodCall {
        receiver: Box::new(read),
        method: Ident::new("map"),
        args: vec![rust::Expr::Closure {
            params: vec![Ident::new("v")],
            body: Box::new(rust::Expr::Cast {
                value: Box::new(rust::Expr::Path(Path::single("v"))),
                type_: Box::new(rust::Type::Primitive(holder)),
            }),
        }],
    }
}

/// The element function for an array with `remaining` dimensions left.
///
/// `packed` wraps each level's element in a
/// `Reader::begin_packed_element` / `finish_packed_element` pair, the
/// read side of `array_element_writer`'s: the row and every cell inside
/// it each begin their own alignment block.
///
/// zerodds-lint: recursion-depth 8 (as `array_element_writer`: one level
/// per dimension of the array declarator)
fn array_element_reader(
    at: &str,
    element: &MemberWrite,
    remaining: usize,
    packed: bool,
) -> Result<rust::Expr, EncodeError> {
    if remaining == 0 {
        // A cell: the element position and the packed block, and never an
        // element length.
        return sequence_element_reader(at, element, packed, false);
    }
    let inner = array_element_reader(at, element, remaining - 1, packed)?;
    let call = rust::Expr::MethodCall {
        receiver: Box::new(rust::Expr::Path(Path::single("r"))),
        method: Ident::new("read_array_n"),
        args: vec![inner],
    };
    if !packed {
        return Ok(rust::Expr::Closure {
            params: vec![Ident::new("r")],
            body: Box::new(call),
        });
    }
    Ok(rust::Expr::Closure {
        params: vec![Ident::new("r")],
        body: Box::new(rust::Expr::Block(rust::Block {
            statements: vec![
                let_statement("zd_origin", begin_packed("r")),
                let_statement("value", rust::Expr::Try(Box::new(call))),
                rust::Statement::Semi(finish_packed("r", "zd_origin")),
                rust::Statement::Tail(rust::Expr::Call {
                    callee: Box::new(rust::Expr::Path(Path::single("Ok"))),
                    args: vec![rust::Expr::Path(Path::single("value"))],
                }),
            ],
        })),
    })
}

/// The element function a `sequence`'s read takes, with the two HDDS
/// axes the reference codec puts around each element.
///
/// With neither axis selected this is [`element_reader`] unchanged, so
/// every other dialect's emitted code is the same text it was.
///
/// The element's own length is checked against what reading it consumed,
/// exactly as `zerodds-wire-codec`'s sequence walk does: the declared
/// count is a peer's claim, and a value that did not fill it leaves the
/// next element to be read from inside this one.
fn sequence_element_reader(
    at: &str,
    element: &MemberWrite,
    packed: bool,
    carries_length: bool,
) -> Result<rust::Expr, EncodeError> {
    if !packed && !carries_length {
        return element_reader(at, element);
    }
    let read = element_read_expr(at, element)?;
    let mut statements = Vec::new();
    if packed {
        statements.push(let_statement("zd_origin", begin_packed("r")));
    }
    if carries_length {
        statements.push(let_statement(
            "zd_declared",
            rust::Expr::Cast {
                value: Box::new(rust::Expr::Try(Box::new(call("read_u32", Vec::new())))),
                type_: Box::new(rust::Type::named(Path::single("usize"))),
            },
        ));
        statements.push(let_statement("zd_before", call("position", Vec::new())));
    }
    statements.push(let_statement("value", rust::Expr::Try(Box::new(read))));
    if carries_length {
        statements.push(rust::Statement::Semi(rust::Expr::If {
            condition: Box::new(rust::Expr::Binary {
                op: rust::BinaryOp::Ne,
                lhs: Box::new(rust::Expr::Binary {
                    op: rust::BinaryOp::Sub,
                    lhs: Box::new(call("position", Vec::new())),
                    rhs: Box::new(rust::Expr::Path(Path::single("zd_before"))),
                }),
                rhs: Box::new(rust::Expr::Path(Path::single("zd_declared"))),
            }),
            then: rust::Block {
                statements: vec![rust::Statement::Semi(rust::Expr::Return(Some(Box::new(
                    refused_err(
                        "r",
                        "a sequence element of the octet count its own length declared",
                    ),
                ))))],
            },
            otherwise: None,
        }));
    }
    if packed {
        statements.push(rust::Statement::Semi(finish_packed("r", "zd_origin")));
    }
    statements.push(rust::Statement::Tail(rust::Expr::Call {
        callee: Box::new(rust::Expr::Path(Path::single("Ok"))),
        args: vec![rust::Expr::Path(Path::single("value"))],
    }));
    Ok(rust::Expr::Closure {
        params: vec![Ident::new("r")],
        body: Box::new(rust::Expr::Block(rust::Block { statements })),
    })
}

/// The read one collection element is, at the element position.
///
/// [`read_for`] names a `wchar`'s *member* framing; a collection element
/// takes the other one, so it is substituted here. A `wstring` element
/// has its position rewritten by [`as_element`] first.
fn element_read_expr(at: &str, element: &MemberWrite) -> Result<rust::Expr, EncodeError> {
    let substituted = as_element(element);
    let element = substituted.as_ref().unwrap_or(element);
    if matches!(element, MemberWrite::Primitive(PrimitiveKind::WChar)) {
        return Ok(call("read_wchar_element", Vec::new()));
    }
    read_for(at, element)
}

/// The integer reader an enumeration's bit bound calls for.
const fn enum_reader(bits: u16) -> &'static str {
    match bits {
        0..=8 => "read_u8",
        9..=16 => "read_u16",
        17..=32 => "read_u32",
        _ => "read_u64",
    }
}

/// What kind of write a member needs.
///
/// `pub(crate)`: see [`write_value_to`]. `emit::interface_rust` never
/// constructs a variant beside [`Self::Primitive`] — it gets every other
/// shape from [`write_of`] — so it names the type and this one variant
/// only.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) enum MemberWrite {
    /// Owning indirection in the Rust binding, with unchanged wire layout.
    External(Box<MemberWrite>),
    /// A primitive, with its own writer.
    Primitive(PrimitiveKind),
    /// A string, with its bound when it has one.
    ///
    /// The bound is written into the call rather than checked at
    /// runtime by the writer: a bounded string that overruns is a
    /// program error, and the bound is what the IDL said, not a
    /// parameter of the wire format.
    String {
        /// The bound, absent when unbounded.
        bound: Option<i128>,
        /// What this *position* does with a value the bound cannot hold.
        /// Absent when the string is unbounded, because there is then
        /// nothing a value can fail to fit.
        narrow: Option<Narrowing>,
    },
    /// A `fixed<P, S>`, with its declaration and the digit-count axis.
    ///
    /// The value is a decimal **string** in this column — `cst.rs` gives
    /// `fixed` the Rust spelling `String` and `default_rust.rs` gives it a
    /// zero at the declared scale — for the reason the other twenty columns
    /// carry it as text too: Rust has no decimal type in `core`, and a
    /// `fixed<31, 2>` does not fit any integer Rust has. A binary float
    /// would be the wrong answer rather than an approximate one, because
    /// `fixed` exists to be exact.
    ///
    /// Three fields and not one: `P` and `S` are the declaration, and
    /// `count` is the dialect axis over CORBA 3.3 part 2 §9.3.2.8's open
    /// question of how many digit nibbles go out. All three go into the
    /// emitted call, so the generated codec states the cell it writes.
    Fixed {
        /// `P` — the total digits the declaration names.
        digits: u8,
        /// `S` — the digits after the point.
        scale: u8,
        /// Whether `P` digits go out whatever the value is, or as many as
        /// the value has. `zerodds_ast_layout::key::FixedDigitCount`.
        count: FixedDigitCount,
    },
    /// A `wstring`, with its bound and the four axes that decide its
    /// octets.
    ///
    /// Five fields and not one, because the cell is a product: C1, C2
    /// and C3 are [`WstringLengthPolicy`]'s three coordinates, C4 is
    /// [`WstringUnitOrder`], and the position is what keeps the claim
    /// that the two positions agree a measurement rather than an
    /// assumption. All four come out of the [`LayoutKey`] and go into
    /// the emitted call, so the generated codec states the cell it
    /// writes instead of inheriting one from the runtime.
    WString {
        /// The bound in **UTF-16 code units**, absent when unbounded.
        ///
        /// Units and not octets: `wstring<N>` is an `N`-unit buffer in
        /// every measured mapping, and an octet bound would mean two
        /// different capacities under C3's two widths.
        bound: Option<i128>,
        /// What this *position* does with a value the bound cannot
        /// hold. Absent when the `wstring` is unbounded.
        narrow: Option<Narrowing>,
        /// C1 × C2 × C3 — what the length counts, whether a terminator
        /// joins the run, how wide one code unit is.
        policy: WstringLengthPolicy,
        /// What the content's characters are made of. Under
        /// [`WstringCodeUnits::Utf8`] the characters go out as their own
        /// UTF-8 octets and C1 and C3 are vacuous, so the emitted call
        /// names the runtime's UTF-8 entry point instead.
        units: WstringCodeUnits,
        /// C4 — in what byte order the units go out, and whether a
        /// byte-order mark leads them.
        order: WstringUnitOrder,
        /// Where the `wstring` is reached from.
        position: WirePosition,
    },
    /// A sequence, with its element and its bound.
    Sequence {
        /// What each element is.
        element: Box<MemberWrite>,
        /// The bound, absent when unbounded.
        bound: Option<i128>,
        /// As [`MemberWrite::String`]'s.
        narrow: Option<Narrowing>,
        /// Whether a `DHEADER` precedes the length. Rule (12), decided
        /// by `zerodds_ast_layout::collection_is_delimited` so that the
        /// emitted bytes and the planned layout cannot disagree.
        delimited: bool,
        /// Whether each element begins its own alignment block.
        /// `ElementPadding::Packed`'s writer half: this column wraps the
        /// element function in a `Writer::begin_packed_element` /
        /// `finish_packed_element` pair, at the same site
        /// `zerodds-wire-codec` writes them.
        packed: bool,
        /// Whether each element is preceded by its own four-octet
        /// length. `SequenceElementDelimiter::BeforeEachNonFinalElement`,
        /// decided by `zerodds_ast_layout::xcdr::sequence_element_carries_length`
        /// so the two walkers ask one crossing.
        carries_length: bool,
    },
    /// A map, with both halves and its bound.
    ///
    /// Two element functions and not one: a `map<K, V>` is a sequence of
    /// pairs (§7.4.1.1.4) and the halves are two different types, so the
    /// runtime takes a function per half.
    ///
    /// # The entry order is the container's and is not decided here
    ///
    /// `adapter::cst` materialises an IDL `map` as a `BTreeMap<K, V>`,
    /// which iterates ascending by key, and
    /// `zerodds_ast_layout::MapEntryOrder::AscendingKey` is defined as
    /// what a binding that holds a `map` in an ordered container writes.
    /// So this column answers the axis by the type it already emits.
    /// There is no walk here to bypass it and no second reading to keep
    /// in step — which is why this shape carries no ordering field, and
    /// why `crates/lang-rules/src/encode.rs` is not one of the sites
    /// `wire-probe`'s `map_entry_order.rs` counts crossings in.
    Map {
        /// What each entry's key is.
        key: Box<MemberWrite>,
        /// What each entry's value is.
        value: Box<MemberWrite>,
        /// The bound, in **entries**, absent when unbounded.
        bound: Option<i128>,
        /// As [`MemberWrite::String`]'s. Table 11 names `map` among the
        /// forms a TRIM may be written on.
        narrow: Option<Narrowing>,
        /// Whether a `DHEADER` precedes the entry count. Rule (14)
        /// exempts a map only when both halves are primitive and rule
        /// (15) delimits the rest, decided by
        /// `zerodds_ast_layout::xcdr::plan` through `MapDelimiter` so
        /// that the emitted bytes and the planned layout cannot
        /// disagree.
        delimited: bool,
        /// Whether the entries are written ascending by key: the map is held as its entries
        /// (`adapter::cst::RustMapForm::Entries`), which keep the order they were put in, and
        /// `MapEntryOrder::AscendingKey` asked for the other one.
        ascending: bool,
    },
    /// An array, with its element and its dimensions outermost first.
    Array {
        /// What each element is.
        element: Box<MemberWrite>,
        /// The dimensions, outermost first.
        dims: Vec<i128>,
        /// Whether a `DHEADER` precedes the elements. Rule (9).
        delimited: bool,
        /// Whether each element begins its own alignment block, as
        /// [`MemberWrite::Sequence`]'s. Arrays never carry an element
        /// length: only `sequence` has that axis.
        packed: bool,
    },
    /// An enumeration, written as the integer its bit bound calls for.
    Enumeration {
        /// The bit bound.
        bits: u16,
    },
    /// A bitmask, written in the holder Table 33 of XTypes 1.3
    /// §7.4.1.1.3.2 gives for its `@bit_bound`.
    ///
    /// # Not `Enumeration`, and not `Nested`
    ///
    /// Not [`MemberWrite::Enumeration`], although both are an integer in
    /// a holder: an enumeration's holder width goes through
    /// `EnumHolderWidth`, the axis M1 measured, and a bitmask's does
    /// not — `zerodds_ast_layout::xcdr::enumeration_bit_bound` says so
    /// in as many words. Sharing the shape would have let a setting
    /// meant for one construct move the other's octets.
    ///
    /// Not [`MemberWrite::Nested`] either, although the other twenty
    /// columns give a bitmask its own codec function. `adapter::cst`
    /// materialises a bitmask as `pub type Flags = u16;` — a type alias
    /// to a primitive, because a Rust `enum` cannot hold two bits at
    /// once — and there is no `impl` block to hang an `encode` on.
    Bitmask {
        /// The bound Table 33 sizes the holder by, from
        /// `zerodds_ast_layout::plan::scalar_write`.
        bits: u16,
        /// The positions the type names, one bit per position. A bit set
        /// inside the bound that this mask does not carry is refused on
        /// the way in.
        declared: u64,
    },
    /// Another aggregate, which encodes itself.
    ///
    /// The name is carried because the decode side has to write it:
    /// `Cell::decode(r)` names the type and `r.read_nested()` cannot.
    Nested {
        /// The generated Rust type's name.
        type_name: String,
        /// The suffix the nested type's codec functions carry: empty
        /// for the type's first codec, [`SECONDARY_SUFFIX`] for the
        /// second one beside it.
        ///
        /// Carried rather than recomputed at the call site: `write_call`
        /// and `read_whole` are handed a `MemberWrite` and no key, and a
        /// nested call that named `encode` while the enclosing body was
        /// the XCDR1 one would frame the member under version 2 inside a
        /// version 1 stream — a sample that decodes into wrong values
        /// with nothing reporting it.
        codec: &'static str,
        /// How the sample's PL_CDR1 step decision reaches this call, and
        /// which of the nested type's two methods it names under
        /// [`Extended::Fixed`].
        ///
        /// Carried for the same reason as `codec`, and read from the same
        /// key: `write_call` is handed the shape and nothing else, so a
        /// call that could not say whether the flag was threaded would
        /// frame every nested type of a per-sample plan with the short
        /// header, also inside a sample past RTI's step.
        extended: Extended,
    },
    /// A kind with no writer yet, named so the error can say which.
    Unsupported(&'static str),
}

/// What one position does with a value its bound cannot hold.
///
/// DDS-XTypes 1.3 §7.2.2.7 attaches the behaviour to the position — a
/// member, a collection element, a map key, a union case — and not to
/// the type, so it is carried alongside the shape rather than derived
/// from it. One `typedef string<5>` may be used at two positions that
/// want different answers.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct Narrowing {
    /// What the receiving type holds.
    bound: i128,
    /// Which of the three §7.3.1.2.1.12 behaviours this position asks
    /// for.
    behaviour: TryConstruct,
    /// The value `USE_DEFAULT` substitutes, when that is the behaviour.
    ///
    /// Computed only for `USE_DEFAULT`: a structure's default is a walk
    /// of its own members, and computing one for every bounded string
    /// in the model would be a walk per string for a value nothing
    /// reads.
    default: Option<rust::Expr>,
}

/// Where a type is being used, for the purpose of `try_construct`.
///
/// `pub(crate)`: see [`write_value_to`]. `emit::interface_rust` builds
/// one per field from `Ast::effective_try_construct` itself — an
/// operation parameter has no member declarator, so it always passes
/// `written: None`.
#[derive(Debug, Clone, Copy)]
pub(crate) struct Position {
    /// The behaviour this position asks for, already resolved through
    /// the alias chain by `Ast::effective_try_construct`.
    pub(crate) behaviour: TryConstruct,
    /// What the *member declarator* wrote, which `element_try_construct`
    /// needs for the one case §7.3.1.2.1.12 lets a member's annotation
    /// reach the elements: an array declared on the member.
    pub(crate) written: Option<TryConstruct>,
}

fn member_of(
    ast: &Ast,
    type_: Id<TypeNode>,
    node: zerodds_ast_core::NodeId,
    key: LayoutKey,
    codec: &'static str,
) -> Option<(String, MemberWrite)> {
    // `all_members_of`, so a member the aggregate inherited is found
    // here instead of being skipped as though the plan had not named it.
    let member = ast
        .all_members_of(type_)
        .into_iter()
        .find(|id| ast.member(*id).is_ok_and(|m| m.node_id == node))?;
    let member = ast.member(member).ok()?;
    // The member's own `@try_construct`, resolved through the alias
    // chain — §7.2.2.7 puts the behaviour on the position, and this is
    // the member position.
    let written = member.facets.xtypes.as_ref().and_then(|x| x.try_construct);
    let position = Position {
        behaviour: ast.effective_try_construct(written, member.type_.resolved()),
        written,
    };
    // Types this member names are written relative to the module the
    // aggregate itself lands in.
    let from = module_of(ast, type_);
    let mut write = member.type_.resolved().map_or(
        MemberWrite::Unsupported("member with an unresolved type"),
        |target| write_of(ast, from, target, key, codec, position),
    );
    // §7.2.2.4.4.4.9 puts an explicit `@default` ahead of Table 9's, and
    // only the member position can carry one — a collection element has
    // no declarator to write it on.
    if let Some(target) = member.type_.resolved() {
        if let Some(narrow) = narrowing_of_mut(&mut write) {
            if narrow.behaviour == TryConstruct::UseDefault {
                narrow.default = crate::default_rust::member_value_default(
                    ast,
                    module_of(ast, type_),
                    member,
                    target,
                )
                .ok();
            }
        }
    }
    Some((member.name.clone(), external_write(ast, member, write)))
}

fn external_write(ast: &Ast, member: &zerodds_ast::MemberNode, write: MemberWrite) -> MemberWrite {
    if crate::adapter::cst::is_external(ast, member) {
        MemberWrite::External(Box::new(write))
    } else {
        write
    }
}

/// The narrowing of a shape, where the shape has one.
fn narrowing_of_mut(write: &mut MemberWrite) -> Option<&mut Narrowing> {
    match write {
        MemberWrite::String { narrow, .. }
        | MemberWrite::WString { narrow, .. }
        | MemberWrite::Sequence { narrow, .. }
        | MemberWrite::Map { narrow, .. } => narrow.as_mut(),
        _ => None,
    }
}

/// The declaration one plan entry names.
///
/// `member_of` answers with the name and the writer shape; the default
/// value needs the declaration itself, for the `@default` it may carry.
fn member_node_of(
    ast: &Ast,
    type_: Id<TypeNode>,
    node: zerodds_ast_core::NodeId,
) -> Option<&zerodds_ast::MemberNode> {
    let member = ast
        .all_members_of(type_)
        .into_iter()
        .find(|id| ast.member(*id).is_ok_and(|m| m.node_id == node))?;
    ast.member(member).ok()
}

/// What writing one type takes.
///
/// Recursive, because a sequence of sequences is written by writing the
/// outer length and then each inner sequence — the shape of the writer
/// follows the shape of the type, which is what keeps the two from
/// drifting apart.
///
/// `pub(crate)`: see [`write_value_to`]. Unlike [`member_of`], this
/// takes a bare `Id<TypeNode>` and not a member of some aggregate — an
/// operation parameter or an exception member is neither, and DDS-RPC
/// 1.0 §7.5.1.1.4-7 synthesizes structures for them that never reach the
/// model at all.
pub(crate) fn write_of(
    ast: &Ast,
    from: Id<zerodds_ast::ModuleNode>,
    target: Id<TypeNode>,
    key: LayoutKey,
    codec: &'static str,
    at: Position,
) -> MemberWrite {
    write_of_at(ast, from, target, key, codec, at, 0)
}

/// How deep a member's writer may nest before the walk refuses the type.
///
/// The same 64 every reference walk in the tree carries;
/// `zerodds-wire-codec`'s `default.rs` and `zerodds-types`'
/// `dynamic/type_.rs` both stop there.
const MAX_WRITE_DEPTH: usize = 64;

/// [`write_of`], carrying how many collection and typedef steps the walk
/// has already taken.
///
/// An aggregate is a leaf here — it writes through its own generated
/// codec under [`MemberWrite::Nested`] — so only the connectors can come
/// back round: a `sequence`'s element, an array's element, a `map`'s two
/// halves, a `typedef`'s target. A cycle among those is a finding the
/// model's invariant pass reports rather than a shape it rejects, and
/// without the counter this function read one forever.
///
/// zerodds-lint: recursion-depth 64 (sequence, array, map and typedef
/// nesting; bounded by `MAX_WRITE_DEPTH`, which returns
/// [`MemberWrite::Unsupported`] naming the construct — the same channel
/// every other refusal in this walk uses, so the emitter reports it by
/// name instead of writing a member it cannot spell)
fn write_of_at(
    ast: &Ast,
    from: Id<zerodds_ast::ModuleNode>,
    target: Id<TypeNode>,
    key: LayoutKey,
    codec: &'static str,
    at: Position,
    depth: usize,
) -> MemberWrite {
    if depth > MAX_WRITE_DEPTH {
        return MemberWrite::Unsupported("a type whose writer nests past the recursion bound");
    }
    let Ok(node) = ast.type_(target) else {
        return MemberWrite::Unsupported("member with a stale type handle");
    };
    // The narrowing a bound calls for, computed once for both bounded
    // shapes. `None` for an unbounded one: there is nothing a value can
    // fail to fit.
    let narrowing = |bound: Option<i128>| {
        bound.map(|bound| Narrowing {
            bound,
            behaviour: at.behaviour,
            default: if at.behaviour == TryConstruct::UseDefault {
                crate::default_rust::default_of(ast, from, target, &node.name).ok()
            } else {
                None
            },
        })
    };
    match &node.kind {
        TypeKind::Primitive(kind) => MemberWrite::Primitive(*kind),
        // A `wstring`. The default cell is a UInt32 **octet** count and
        // UTF-16 code units with no NUL — XTypes 1.3 §7.4.1.1.2 — and
        // it is one cell of eight, not one form of three. The three
        // binary axes are what a length prefix counts (C1), whether a
        // terminating NUL joins the run and is counted (C2), and how
        // wide one code unit is (C3); `WstringLengthPolicy` is their
        // product, `WstringLengthPolicy::ALL_CELLS` enumerates it, and
        // five of the eight cells carry the name of an implementation
        // measured writing them. The byte **order** of the units is a
        // fourth axis, `WstringUnitOrder`, with four points of its own,
        // because it crosses the length convention rather than
        // extending it.
        //
        // All four come out of the key, the way `MapDelimiter` and
        // `MapEntryOrder` do, and go into the emitted call as
        // arguments. A runtime default would be a second place the wire
        // format lives.
        //
        // The bound counts UTF-16 code units and not octets, which is
        // `Reader::fit_wstring` and not `Reader::fit_string`.
        TypeKind::String {
            width: CharWidth::WChar,
            bound,
        } => {
            let bound = bound.expr().and_then(|expr| ast.eval_integral(expr).ok());
            MemberWrite::WString {
                bound,
                narrow: narrowing(bound),
                policy: key.wstring_length,
                units: key.wstring_code_units,
                order: key.wstring_unit_order,
                // The member position. A `wstring` reached as a
                // collection element is rewritten to
                // `WirePosition::Element` by `element_writer` and
                // `element_reader`, which is where the `wchar` framing's
                // element answer is substituted as well — the nine
                // measured implementations answer the two positions
                // alike on all four `wstring` axes, and passing the
                // position is what keeps that a measurement.
                position: WirePosition::Member,
            }
        }
        TypeKind::String { bound, .. } => {
            let bound = bound.expr().and_then(|expr| ast.eval_integral(expr).ok());
            MemberWrite::String {
                bound,
                narrow: narrowing(bound),
            }
        }
        // The digit count comes out of the key the way `MapEntryOrder` and
        // the four `wstring` axes do, and goes into the emitted call as an
        // argument. A runtime default would be a second place the wire
        // format lives.
        TypeKind::Fixed { digits, scale } => MemberWrite::Fixed {
            digits: *digits,
            scale: *scale,
            count: key.fixed_digits,
        },
        TypeKind::Sequence { element, bound } => element.resolved().map_or_else(
            || MemberWrite::Unsupported("sequence with an unresolved element"),
            |element| {
                let bound = bound.expr().and_then(|expr| ast.eval_integral(expr).ok());
                // Whether each element carries its own length is the same
                // crossing `zerodds-wire-codec`'s sequence walk asks, so
                // the emitted bytes and the reference codec cannot part.
                let Some(carries_length) =
                    zerodds_ast_layout::xcdr::sequence_element_carries_length(ast, element, key)
                else {
                    return MemberWrite::Unsupported(
                        "a sequence element whose own length cannot be told",
                    );
                };
                MemberWrite::Sequence {
                    element: Box::new(write_of_at(
                        ast,
                        from,
                        element,
                        key,
                        codec,
                        inside(ast, target, at),
                        depth + 1,
                    )),
                    bound,
                    narrow: narrowing(bound),
                    delimited: zerodds_ast_layout::collection_is_delimited(ast, element, key),
                    packed: key.element_padding == ElementPadding::Packed,
                    carries_length,
                }
            },
        ),
        TypeKind::Array { element, dims } => element.resolved().map_or_else(
            || MemberWrite::Unsupported("array with an unresolved element"),
            |element| {
                // A dimension that does not evaluate is not a zero-length
                // array. Substituting one wrote no elements at all and
                // said the sample had been encoded.
                let Ok(dims) = dims
                    .iter()
                    .map(|dim| ast.eval_integral(*dim))
                    .collect::<Result<Vec<_>, _>>()
                else {
                    return MemberWrite::Unsupported("array whose length does not evaluate");
                };
                MemberWrite::Array {
                    element: Box::new(write_of_at(
                        ast,
                        from,
                        element,
                        key,
                        codec,
                        inside(ast, target, at),
                        depth + 1,
                    )),
                    delimited: zerodds_ast_layout::array_is_delimited(ast, element, key),
                    dims,
                    packed: key.element_padding == ElementPadding::Packed,
                }
            },
        ),
        // A `map<K, V>` is a sequence of pairs, §7.4.1.1.4, and the two
        // halves are two different types — so both are walked, at the
        // element position §7.3.1.2.1.12.4 gives them.
        TypeKind::Map {
            key: k,
            value,
            bound,
        } => {
            let (Some(k), Some(value)) = (k.resolved(), value.resolved()) else {
                return MemberWrite::Unsupported("map with an unresolved key or value");
            };
            // The same refusal the C emitter makes, and the plan's own:
            // a setting that asked for ascending entries over a key type
            // whose values have no order cannot be honoured, and falling
            // back to declaration order would be a flag that reported
            // success and changed no octet. This column reaches it only
            // through the plan — the order it writes is its `BTreeMap`'s
            // — which is why the crossing itself is not named here.
            if zerodds_ast_layout::plan::map_entry_ordering(ast, target, key).is_err() {
                return MemberWrite::Unsupported(
                    "a map whose key type has no value order, under --map-entries ascending-key",
                );
            }
            let bound = bound.expr().and_then(|expr| ast.eval_integral(expr).ok());
            let mut deps = zerodds_ast_layout::xcdr::DepCollector::default();
            let Ok(layout) = zerodds_ast_layout::xcdr::plan(ast, target, key, &mut deps) else {
                return MemberWrite::Unsupported("map whose layout does not plan");
            };
            let inside = inside(ast, target, at);
            let ascending = matches!(
                zerodds_ast_layout::plan::map_entry_ordering(ast, target, key),
                Ok(Some(_))
            );
            MemberWrite::Map {
                key: Box::new(write_of_at(ast, from, k, key, codec, inside, depth + 1)),
                value: Box::new(write_of_at(ast, from, value, key, codec, inside, depth + 1)),
                bound,
                ascending,
                narrow: narrowing(bound),
                // Rule (14) exempts a map only when both halves are
                // primitive; the plan reads that through `MapDelimiter`,
                // and asking it here is what keeps the emitted bytes and
                // the planned extent from disagreeing.
                delimited: layout.needs_dheader,
            }
        }
        // A bitmask is the unsigned integer Table 33 of §7.4.1.1.3.2
        // gives for its `@bit_bound`, and the width is the **plan's**
        // answer: `plan::scalar_write` is the crossing every other
        // column asks — `crates/lang-rules/src/emit/encode.rs` reaches
        // `ScalarWrite::Bitmask` through the same call — so the twenty
        // other columns' octets and this one's cannot part company by
        // this column computing a width of its own.
        //
        // Unlike an enumeration this does **not** write through a
        // generated codec: `adapter::cst` materialises a bitmask as
        // `pub type Flags = u16;`, and a type alias to a primitive has
        // no `impl` block for an `encode` to live in.
        TypeKind::Bitmask { .. } => {
            let Some(ScalarWrite::Bitmask { bits }) =
                zerodds_ast_layout::plan::scalar_write(ast, target, key, WcharPosition::Member)
            else {
                return MemberWrite::Unsupported("a bitmask the layout plan gives no holder for");
            };
            let Some(declared) = declared_bits(ast, target, bits) else {
                return MemberWrite::Unsupported(
                    "a bitmask with a bit position that does not evaluate",
                );
            };
            MemberWrite::Bitmask { bits, declared }
        }
        // An enumeration writes through its own generated codec, like any
        // other named type. Writing the holder straight from here emitted
        // `w.write_u32(self.coord_convention)` — the field is the
        // enumeration and not its holder, so the generated file did not
        // compile, and no enumeration had ever been a struct member in a
        // tree this emitter had been pointed at. `enumeration_encode_fn`
        // already produces the `match self { Self::X => 0, .. }` that
        // respects `@value`, and it takes the holder's width from the
        // same `key`, so nothing about the wire changes.
        TypeKind::Enumeration { .. } => match crate::adapter::cst::relative_path(ast, from, node) {
            Ok(path) => MemberWrite::Nested {
                type_name: path
                    .segments()
                    .iter()
                    .map(|s| s.as_str().to_string())
                    .collect::<Vec<_>>()
                    .join("::"),
                codec,
                extended: Extended::of(key),
            },
            Err(_) => MemberWrite::Unsupported("an enumeration the model has no module for"),
        },
        // The behaviour was already resolved through the alias chain by
        // `Ast::effective_try_construct`, so the position carries over
        // unchanged.
        TypeKind::Alias { aliased } => aliased.resolved().map_or_else(
            || MemberWrite::Unsupported("typedef with an unresolved target"),
            |aliased| write_of_at(ast, from, aliased, key, codec, at, depth + 1),
        ),
        // A union is written the same way, and for the same reason: it
        // has a generated codec of its own, and `union_codec` gives it
        // exactly the `encode(&self, w)` and `decode(r)` that
        // `MemberWrite::Nested` calls. Leaving it out of this arm did not
        // make a union member unwritable in some deeper sense — it made
        // the emitter refuse a type it had already generated the codec
        // for, which is what `NoWriter { member: "cov", kind: "union" }`
        // was: `FramedPose.cov` is a `CovMatrix`, an `@appendable` union
        // over an enumerated discriminator, and every one of its own
        // cases emitted fine.
        // A bitset joins them: `adapter::cst` materialises it as a
        // struct with one field per named bitfield, and `bitset_encode_fn`
        // gives that struct the `encode(&self, w)` and `decode(r)` this
        // shape calls. It was in the `other` arm below, which is what
        // `NoWriter { kind: "bitset" }` was.
        TypeKind::Structure { .. }
        | TypeKind::Valuetype { .. }
        | TypeKind::Union { .. }
        | TypeKind::Bitset { .. } => {
            // The path, not the name: `Nested` becomes `<path>::decode`
            // in the reader, and a sibling module's type does not resolve
            // by its simple name from here.
            match crate::adapter::cst::relative_path(ast, from, node) {
                Ok(path) => MemberWrite::Nested {
                    type_name: path
                        .segments()
                        .iter()
                        .map(|s| s.as_str().to_string())
                        .collect::<Vec<_>>()
                        .join("::"),
                    codec,
                    extended: Extended::of(key),
                },
                Err(_) => MemberWrite::Unsupported("a type the model has no module for"),
            }
        }
        other => MemberWrite::Unsupported(other.as_str()),
    }
}

/// The positions a bitmask names, one bit per position.
///
/// The mask `Reader::read_bitmask` reduces a stream to. §7.4.1.1.3.2
/// puts a bit above the bound outside the bitmask, so a position at or
/// beyond the bound is not part of the mask; a bit set inside the bound
/// that the type does not name is not part of the value either, and the
/// reader keeps only the declared positions.
///
/// The same rule `bitmask_body` in `crates/lang-rules/src/emit/decode.rs`
/// emits inline for the other twenty columns — a second copy rather than
/// a shared call, because the two are compared against each other and a
/// shared helper would make them agree about a mistake.
fn declared_bits(ast: &Ast, type_: Id<TypeNode>, bound: u16) -> Option<u64> {
    let mut declared = 0u64;
    for bit in ast.bits_of(type_) {
        // `None` and not `continue`: a bit whose position does not
        // evaluate is a bit this mask cannot name, and leaving it out
        // would make the reader drop a bit the type does declare, for a
        // reason nothing states. The caller
        // turns this into `MemberWrite::Unsupported` naming the
        // construct.
        let position = ast.effective_bit_position(*bit)?;
        if position < bound && position < 64 {
            declared |= 1u64 << position;
        }
    }
    Some(declared)
}

/// The position a collection's elements sit in.
///
/// §7.3.1.2.1.12.4 gives the elements their own behaviour —
/// `sequence<@try_construct(TRIM) String5, 10>` — and Table 17 keeps it
/// separate from the collection's own: the sequence's length overflow
/// and an element's bound overflow are two different failures, which is
/// the split OpenDDS's generated element loop implements.
fn inside(ast: &Ast, collection: Id<TypeNode>, at: Position) -> Position {
    Position {
        behaviour: ast.element_try_construct(collection, at.written),
        written: None,
    }
}

/// The writer a primitive takes.
///
/// Exhaustive, so a primitive added to the model without a writer is a
/// compile error rather than a member that silently takes another one's.
const fn writer_name(kind: PrimitiveKind) -> &'static str {
    match kind {
        PrimitiveKind::Boolean => "write_bool",
        PrimitiveKind::Octet | PrimitiveKind::UInt8 => "write_u8",
        PrimitiveKind::Char => "write_char",
        // Member and element take different octets under TAO's framing,
        // so there is no positionless `write_wchar` to name here. This
        // arm is the member's; `element_writer_name` has the element's.
        PrimitiveKind::WChar => "write_wchar_member",
        PrimitiveKind::Int8 => "write_i8",
        PrimitiveKind::Int16 => "write_i16",
        PrimitiveKind::UInt16 => "write_u16",
        PrimitiveKind::Int32 => "write_i32",
        PrimitiveKind::UInt32 => "write_u32",
        PrimitiveKind::Int64 => "write_i64",
        PrimitiveKind::UInt64 => "write_u64",
        PrimitiveKind::Float32 => "write_f32",
        PrimitiveKind::Float64 => "write_f64",
        PrimitiveKind::Float128 => "write_f128",
    }
}

const fn reader_name(kind: PrimitiveKind) -> &'static str {
    match kind {
        PrimitiveKind::Boolean => "read_bool",
        PrimitiveKind::Octet | PrimitiveKind::UInt8 => "read_u8",
        PrimitiveKind::Char => "read_char",
        // As `writer_name`: the member's reader.
        PrimitiveKind::WChar => "read_wchar_member",
        PrimitiveKind::Int8 => "read_i8",
        PrimitiveKind::Int16 => "read_i16",
        PrimitiveKind::UInt16 => "read_u16",
        PrimitiveKind::Int32 => "read_i32",
        PrimitiveKind::UInt32 => "read_u32",
        PrimitiveKind::Int64 => "read_i64",
        PrimitiveKind::UInt64 => "read_u64",
        PrimitiveKind::Float32 => "read_f32",
        PrimitiveKind::Float64 => "read_f64",
        PrimitiveKind::Float128 => "read_f128",
    }
}

fn call(method: &str, args: Vec<rust::Expr>) -> rust::Expr {
    rust::Expr::MethodCall {
        receiver: Box::new(rust::Expr::Path(Path::single("r"))),
        method: Ident::new(method),
        args,
    }
}

/// Bind a framing header so it can be patched once its value is written.
/// Bind an opened header, propagating the writer's refusal.
///
/// The three operations this opens — `write_pid`, `write_pid_extended`
/// and `write_emheader` — refuse a member id their header's field cannot
/// carry (XTypes 1.3 §7.2.2.4.4.4.4 for the 28 bits, §7.4.1.2.2 for the
/// reserved short ids), so each returns a `Result` and the `?` carries
/// the refusal out of the generated `encode`. Nothing here can trip it
/// today, because the plan chose the id and the form together; it is
/// there because a generated codec must not be the one place a masked
/// id reaches the wire.
fn bind_header(name: &str, method: &str, args: Vec<rust::Expr>) -> rust::Statement {
    rust::Statement::Let {
        name: Ident::new(name),
        type_: None,
        value: Some(rust::Expr::Try(Box::new(rust::Expr::MethodCall {
            receiver: Box::new(rust::Expr::Path(Path::single("w"))),
            method: Ident::new(method),
            args,
        }))),
        mutable: false,
    }
}

fn call_statement(method: &str, args: Vec<rust::Expr>) -> rust::Statement {
    rust::Statement::Semi(rust::Expr::MethodCall {
        receiver: Box::new(rust::Expr::Path(Path::single("w"))),
        method: Ident::new(method),
        args,
    })
}

/// The same call, with the `?` a fallible one needs.
fn try_call_statement(method: &str, args: Vec<rust::Expr>) -> rust::Statement {
    rust::Statement::Semi(rust::Expr::Try(Box::new(rust::Expr::MethodCall {
        receiver: Box::new(rust::Expr::Path(Path::single("w"))),
        method: Ident::new(method),
        args,
    })))
}

/// `ParameterRole::Member` or `ParameterRole::Optional`, as a path.
fn parameter_role(role: &str) -> rust::Expr {
    rust::Expr::Path(Path::new(&["ParameterRole", role]))
}

/// One `@optional` member's read, into the `Option<T>` field.
///
/// The mirror of [`write_optional_member`], marker for marker:
///
/// - rule (20): the BOOLEAN `<is_present>` decides;
/// - rule (19): a declared parameter length of zero is the absence
///   (§7.4.1.2), and `finish_pid` still runs either way — the parameter
///   occupies its place in the list whether or not it carries a value,
///   and skipping the close would leave the alignment origin inside it;
/// - inside a `@mutable` type: the parameter is present, because a
///   parameter list that omitted it would not have brought the reader
///   here at all.
fn read_optional_member(
    plan: &zerodds_ast_layout::plan::MemberPlan,
    name: &str,
    kind: &MemberWrite,
) -> Result<Vec<rust::Statement>, EncodeError> {
    let some = |value: rust::Expr| rust::Expr::Call {
        callee: Box::new(rust::Expr::Path(Path::single("Some"))),
        args: vec![value],
    };
    let read = rust::Expr::Try(Box::new(read_for(name, kind)?));
    match plan.header {
        MemberHeader::OptionalFlag => Ok(vec![let_statement(
            name,
            rust::Expr::If {
                condition: Box::new(rust::Expr::Try(Box::new(call("read_bool", Vec::new())))),
                then: rust::Block {
                    statements: vec![rust::Statement::Tail(some(read))],
                },
                otherwise: Some(rust::Block {
                    statements: vec![rust::Statement::Tail(rust::Expr::Path(Path::single(
                        "None",
                    )))],
                }),
            },
        )]),
        MemberHeader::OptionalCdr1 { .. } => Ok(vec![
            let_statement(
                "pid",
                rust::Expr::Try(Box::new(call(
                    "read_pid_as",
                    vec![parameter_role("Optional")],
                ))),
            ),
            let_statement(
                name,
                rust::Expr::If {
                    condition: Box::new(rust::Expr::Binary {
                        op: rust::BinaryOp::Eq,
                        lhs: Box::new(rust::Expr::Field {
                            receiver: Box::new(rust::Expr::Path(Path::single("pid"))),
                            name: Ident::new("length"),
                        }),
                        rhs: Box::new(rust::Expr::Literal(Literal::int(0))),
                    }),
                    then: rust::Block {
                        statements: vec![rust::Statement::Tail(rust::Expr::Path(Path::single(
                            "None",
                        )))],
                    },
                    otherwise: Some(rust::Block {
                        statements: vec![rust::Statement::Tail(some(read))],
                    }),
                },
            ),
            rust::Statement::Semi(rust::Expr::Try(Box::new(call(
                "finish_pid",
                vec![rust::Expr::Path(Path::single("pid"))],
            )))),
        ]),
        _ => {
            let mut out = match plan.header {
                MemberHeader::PidCdr1 { .. } => vec![let_statement(
                    "pid",
                    rust::Expr::Try(Box::new(call("read_pid", Vec::new()))),
                )],
                MemberHeader::EmHeader { .. } => vec![let_statement(
                    "emheader",
                    rust::Expr::Try(Box::new(call("read_emheader", Vec::new()))),
                )],
                _ => Vec::new(),
            };
            out.push(let_statement(name, some(read)));
            if matches!(plan.header, MemberHeader::PidCdr1 { .. }) {
                out.push(rust::Statement::Semi(rust::Expr::Try(Box::new(call(
                    "finish_pid",
                    vec![rust::Expr::Path(Path::single("pid"))],
                )))));
            }
            Ok(out)
        }
    }
}

/// One `field: local` pair of the struct literal a decoder ends with.
///
/// Both halves go through `escape_keyword`, and that is the whole point:
/// the field is a member named in IDL and the local is the binding
/// `let_statement` made for it, so a member called `type` needs `r#type`
/// on both sides. Escaping only the field produced `r#type: type`, which
/// is a Rust syntax error — the generated file did not compile, and the
/// three call sites that built this pair by hand had each got it the
/// same way wrong.
fn field_binding(name: &str) -> (Ident, rust::Expr) {
    let escaped = rust::escape_keyword(name);
    (
        Ident::new(&escaped),
        rust::Expr::Path(Path::single(&escaped)),
    )
}

fn let_statement(name: &str, value: rust::Expr) -> rust::Statement {
    rust::Statement::Let {
        name: Ident::new(&rust::escape_keyword(name)),
        type_: None,
        value: Some(value),
        mutable: false,
    }
}

/// `#[allow(<lint>)]`.
fn allow_lint(lint: &str) -> rust::Attr {
    rust::Attr {
        path: Path::single("allow"),
        args: vec![rust::AttrArg::Word(Ident::new(lint))],
    }
}

/// `impl Default` for an aggregate, from the values XTypes 1.3 Table 9
/// and `@default` give its members.
///
/// Not `#[derive(Default)]`: Rust's default and the specification's are
/// two different things wherever a member is an enumeration — Rust has
/// none without a `#[default]` attribute, and §7.3.1.2.1.10 names the
/// first enumerator or the one `@default_literal` picks. The emitter
/// already computes the right value for every member, and this is where
/// a caller can reach it.
///
/// # Errors
/// [`EncodeError`] naming the member whose default the model cannot
/// give.
fn default_impl(ast: &Ast, type_: Id<TypeNode>) -> Result<Option<rust::Item>, EncodeError> {
    let Ok(node) = ast.type_(type_) else {
        return Ok(None);
    };
    // A bitset joins them, and for the same reason: it materialises as
    // a struct, so `Default::default()` on one is a thing a caller can
    // write and used to be `the trait bound `BS: Default` is not
    // satisfied`. A valuetype is deliberately not here — it was not
    // before — and a bitmask cannot be: `impl Default for u16` is not
    // this crate's to write.
    if !matches!(
        node.kind,
        TypeKind::Structure { .. } | TypeKind::Union { .. } | TypeKind::Bitset { .. }
    ) {
        return Ok(None);
    }
    let from = node.module;
    // Named from inside its own module, which is the plain name.
    let self_type = rust::Type::named(Path::single(&rust::escape_keyword(&node.name)));
    let value = crate::default_rust::default_of(ast, from, type_, &node.name)?;
    Ok(Some(rust::Item::Impl(rust::Impl {
        doc: Doc::none(),
        // Where every member's Table 9 value happens to be Rust's own
        // default, clippy sees a derivable impl. It is written out on
        // purpose (see above), so the allowance is the generator's and
        // sits on the impl, not in each crate that includes the text.
        attrs: vec![allow_lint("clippy::derivable_impls")],
        generics: Vec::new(),
        trait_: Some(rust::Type::named(Path::single("Default"))),
        self_type: self_type.clone(),
        items: vec![rust::Item::Function(rust::Function {
            doc: Doc::none(),
            attrs: Vec::new(),
            visibility: Visibility::Private,
            name: Ident::new("default"),
            generics: Vec::new(),
            params: Vec::new(),
            returns: Some(rust::Type::named(Path::single("Self"))),
            body: Some(rust::Block {
                statements: vec![rust::Statement::Tail(value)],
            }),
        })],
    })))
}

/// The module a type is declared in, for naming other types relative to
/// it. A stale handle falls back to the root, where a relative path is
/// the plain name — the same thing the emitter wrote before it named
/// anything relatively.
/// `self`, then `.parent` once per structure between `type_` and the one that declares
/// `member`: the base is the first member `parent` of the derived structure (XTypes 1.3
/// §7.5.1.4.1.3).
fn holder_of(ast: &Ast, type_: Id<TypeNode>, member: NodeId) -> rust::Expr {
    let mut place = rust::Expr::Path(Path::single("self"));
    for _ in 0..crate::emit::inheritance_depth(ast, type_, member) {
        place = rust::Expr::Field {
            receiver: Box::new(place),
            name: Ident::new(crate::adapter::cst::PARENT),
        };
    }
    place
}

/// A literal of `type_` from the fields of its own members and those of its bases, each
/// level nested in the `parent` of the one above it. `rest` is what a level not every
/// member of which is named takes the others from.
fn nested_struct_literal(
    ast: &Ast,
    type_: Id<TypeNode>,
    entries: Vec<(NodeId, (Ident, rust::Expr))>,
    rest: &impl Fn() -> Option<Box<rust::Expr>>,
) -> rust::Expr {
    let deepest = entries
        .iter()
        .map(|(node, _)| crate::emit::inheritance_depth(ast, type_, *node))
        .max()
        .unwrap_or(0);
    let mut inner: Option<rust::Expr> = None;
    for depth in (0..=deepest).rev() {
        let mut fields: Vec<(Ident, rust::Expr)> = Vec::new();
        if let Some(parent) = inner.take() {
            fields.push((Ident::new(crate::adapter::cst::PARENT), parent));
        }
        for (node, field) in &entries {
            if crate::emit::inheritance_depth(ast, type_, *node) == depth {
                fields.push(field.clone());
            }
        }
        let path = if depth == 0 {
            Path::single("Self")
        } else {
            let owner = crate::emit::base_at(ast, type_, depth);
            ast.type_(owner)
                .ok()
                .and_then(|node| {
                    crate::adapter::cst::relative_path(ast, module_of(ast, type_), node).ok()
                })
                .unwrap_or_else(|| Path::single("Self"))
        };
        inner = Some(rust::Expr::StructLit {
            path,
            fields,
            base: rest(),
        });
    }
    inner.unwrap_or(rust::Expr::StructLit {
        path: Path::single("Self"),
        fields: Vec::new(),
        base: rest(),
    })
}

fn module_of(ast: &Ast, type_: Id<TypeNode>) -> Id<zerodds_ast::ModuleNode> {
    ast.type_(type_)
        .map_or_else(|_| ast.root_module(), |node| node.module)
}

fn alloc_format(error: &impl core::fmt::Debug) -> String {
    format!("{error:?}")
}

/// Classify what the planner said: a clause of the specification, or a
/// gap of this tree.
///
/// The planner already carries the distinction in its own type —
/// `LayoutError::RecursionOutsideCollection` names IDL 4.2
/// §7.4.1.4.4.4.4 — and it was lost here, where every reason became one
/// string. Read the variant, never the message: a classification that
/// greps its own error text breaks the first time the text is reworded.
pub(crate) fn plan_refusal(error: zerodds_ast_layout::plan::LayoutError) -> EncodeError {
    match error {
        LayoutError::RecursionOutsideCollection { .. } => EncodeError::SpecRefuses {
            detail: format!("{error}"),
        },
        other => EncodeError::NoLayout {
            detail: alloc_format(&other),
        },
    }
}

/// `_ => Err(WireError::refused(r.position(), "<what>"))`: the arm a
/// generated decoder ends a match with when the octets name nothing the
/// type declares.
fn refusal_arm(what: &str) -> rust::MatchArm {
    rust::MatchArm {
        pattern: rust::Pattern::Wildcard,
        guard: None,
        body: refused_err("r", what),
    }
}

/// `Err(WireError::refused(<sink>.position(), "<what>"))`, the value a
/// generated codec returns for octets or a sample it cannot carry.
fn refused_err(sink: &str, what: &str) -> rust::Expr {
    rust::Expr::Call {
        callee: Box::new(rust::Expr::Path(Path::single("Err"))),
        args: vec![rust::Expr::Call {
            callee: Box::new(rust::Expr::Path(Path::new(&["WireError", "refused"]))),
            args: vec![
                rust::Expr::MethodCall {
                    receiver: Box::new(rust::Expr::Path(Path::single(sink))),
                    method: Ident::new("position"),
                    args: Vec::new(),
                },
                rust::Expr::Literal(Literal::string(what)),
            ],
        }],
    }
}