pbrs 0.1.0

Pure-Rust Protocol Buffers kernel with the Google protobuf v4 application API
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
//! Generate field-wise typed messages (not DynamicMessage wrappers, not upb).
#![allow(
    clippy::disallowed_methods,
    clippy::expect_used,
    reason = "protoc plugin / compile_protos is sync build-time IO, not an async worker"
)]

use crate::dynamic::{
    Cardinality, DescriptorPool, FieldDescriptor, FieldType, MessageDescriptor, MethodDescriptor,
    Presence, ServiceDescriptor,
};
use crate::error::ParseError;
use crate::wire::{self, decode_tag, encode_len_field, encode_varint, read_len_bytes, WIRE_LEN};
use std::cell::{Cell, RefCell};
use std::fmt::Write as _;
use std::path::{Path, PathBuf};
use std::process::Command;

thread_local! {
    static IDENTS: RefCell<std::collections::BTreeMap<String, String>> =
        const { RefCell::new(std::collections::BTreeMap::new()) };
    static FIELD_IDENTS: RefCell<std::collections::BTreeMap<u32, String>> =
        const { RefCell::new(std::collections::BTreeMap::new()) };
    static FIELD_RAWS: RefCell<std::collections::BTreeMap<u32, String>> =
        const { RefCell::new(std::collections::BTreeMap::new()) };
    static STUBS: Cell<Stubs> = const { Cell::new(Stubs::Kernel) };
    static EMIT_DEPS: Cell<bool> = const { Cell::new(false) };
}

/// Which gRPC service stubs to emit alongside generated messages.
///
/// A `.proto` `service` block turns into client and server types. Which flavour
/// you get depends on which gRPC stack you are using; the two are mutually
/// exclusive because they claim the same `FooClient` / `FooServer` names.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum Stubs {
    /// Messages only.
    None,
    /// `tonic` 0.14 stubs over `protobuf_tonic::ProtobufCodec`.
    /// Select with [`Config::emit_tonic_stubs`].
    Tonic,
    /// Native `pbrs-grpc` stubs. The default. Requires the generating crate
    /// to depend on `pbrs-grpc`. `FooClient` dials with `connect` /
    /// `connect_tls` / `connect_unix` / `from_io`; `FooServer` serves with
    /// `serve` / `serve_tls` / `serve_unix`.
    /// The `protoc-gen-pbrs` plugin uses this default; set
    /// `PURE_PROTOBUF_STUBS=tonic` for tonic stubs.
    #[default]
    Kernel,
}

/// Resolve the stub flavour from the thread-local config, then the plugin
/// environment.
fn stubs_setting() -> Stubs {
    match std::env::var("PURE_PROTOBUF_STUBS").as_deref() {
        Ok("kernel") => Stubs::Kernel,
        Ok("tonic") => Stubs::Tonic,
        Ok("none") => Stubs::None,
        _ => STUBS.with(Cell::get),
    }
}

pub fn generate_from_code_generator_request(
    bytes: &[u8],
) -> Result<Vec<(String, String)>, ParseError> {
    let mut files_to_generate = Vec::new();
    let mut proto_files = Vec::new();
    let mut pos = 0;
    while pos < bytes.len() {
        let (n, w) = decode_tag(bytes, &mut pos)?;
        match (n, w) {
            (1, WIRE_LEN) => {
                let s = read_len_bytes(bytes, &mut pos)?;
                files_to_generate.push(String::from_utf8_lossy(s).into_owned());
            }
            (15, WIRE_LEN) => {
                let blob = read_len_bytes(bytes, &mut pos)?.to_vec();
                proto_files.push(blob);
            }
            _ => wire::skip_field(bytes, &mut pos, w)?,
        }
    }
    let mut fds = Vec::new();
    for blob in &proto_files {
        encode_len_field(&mut fds, 1, blob);
    }
    let pool = DescriptorPool::from_file_descriptor_set(&fds)?;
    let names: Vec<String> = pool.collect_names();
    let targets = if files_to_generate.is_empty() {
        vec!["generated.proto".into()]
    } else {
        files_to_generate.clone()
    };
    let mut out_files = Vec::new();
    for target in &targets {
        let wanted: std::collections::BTreeSet<String> = std::iter::once(target.clone()).collect();
        let target_is_wkt = wanted.iter().any(|w| {
            let s = w.replace('\\', "/");
            s.contains("google/protobuf/") && !s.contains("test_messages")
        });
        let mut src = String::from(
            "// @generated by protoc-gen-pbrs\n#[allow(unused, non_snake_case, non_camel_case_types, clippy::all, clippy::restriction, clippy::indexing_slicing, clippy::cast_possible_truncation, clippy::cast_possible_wrap, clippy::cast_sign_loss, clippy::let_underscore_must_use, unreachable_pub, reason = \"generated by protoc-gen-pbrs\")]\nmod __gen {\n#![allow(unused, non_snake_case, non_camel_case_types, clippy::all, clippy::restriction, clippy::indexing_slicing, clippy::cast_possible_truncation, clippy::cast_possible_wrap, clippy::cast_sign_loss, clippy::let_underscore_must_use, unsafe_code, unreachable_pub, reason = \"generated by protoc-gen-pbrs\")]\nuse pbrs::prelude::*;\nuse pbrs::{Enum, Map, MapMut, MapView, ParseError, ProtoBytes, ProtoString, Repeated, RepeatedMut, RepeatedView, SerializeError, UnknownEnumValue};\nuse pbrs::UnknownFields;\n\n",
        );
        let no_reflect = std::env::var("PURE_PROTOBUF_NO_REFLECT").as_deref() == Ok("1");
        if no_reflect {
            // Accessor/binary tests: skip FileDescriptorSet hex and JSON/text.
        } else if std::env::var("PURE_PROTOBUF_SHARED_POOL").as_deref() == Ok("1") {
            src.push_str(
                "fn generated_pool() -> std::sync::Arc<pbrs::DescriptorPool> {\n    pbrs::gencode::conformance_pool()\n}\n\n",
            );
        } else {
            emit_fds(&mut src, &fds);
            src.push_str(
                "fn generated_pool() -> std::sync::Arc<pbrs::DescriptorPool> {\n    static P: std::sync::OnceLock<std::sync::Arc<pbrs::DescriptorPool>> = std::sync::OnceLock::new();\n    P.get_or_init(|| {\n        std::sync::Arc::new(pbrs::DescriptorPool::from_file_descriptor_set(FILE_DESCRIPTOR_SET).expect(\"fds\"))\n    }).clone()\n}\n\n",
            );
        }
        let mut emit_names = Vec::new();
        for name in &names {
            let Some(desc) = pool.get_message(name) else {
                continue;
            };
            if desc.is_map_entry {
                continue;
            }
            let wkt = desc.full_name.starts_with("google.protobuf.");
            let emit_wkt = wkt && !target_is_wkt && std::env::var("PURE_PROTOBUF_NO_WKT").is_err();
            let emit_deps = EMIT_DEPS.with(Cell::get)
                || std::env::var("PURE_PROTOBUF_EMIT_DEPS").as_deref() == Ok("1");
            if !file_matches(&wanted, &desc.file_name) && !emit_wkt && !(emit_deps && !wkt) {
                continue;
            }
            emit_names.push(name.clone());
        }
        let pub_files = pool.public_import_files(&targets);
        emit_names.retain(|n| {
            pool.get_message(n).is_none_or(|d| {
                !pub_files
                    .iter()
                    .any(|p| file_matches(&std::iter::once(p.clone()).collect(), &d.file_name))
            })
        });
        let mut emit_enums = Vec::new();
        for name in pool.collect_enum_names() {
            let Some(ed) = pool.get_enum(&name) else {
                continue;
            };
            let wkt = ed.full_name.starts_with("google.protobuf.");
            let emit_wkt = wkt && !target_is_wkt && std::env::var("PURE_PROTOBUF_NO_WKT").is_err();
            let emit_deps = EMIT_DEPS.with(Cell::get)
                || std::env::var("PURE_PROTOBUF_EMIT_DEPS").as_deref() == Ok("1");
            if !file_matches(&wanted, &ed.file_name) && !emit_wkt && !(emit_deps && !wkt) {
                continue;
            }
            emit_enums.push(name);
        }
        emit_enums.retain(|n| {
            pool.get_enum(n).is_none_or(|d| {
                !pub_files
                    .iter()
                    .any(|p| file_matches(&std::iter::once(p.clone()).collect(), &d.file_name))
            })
        });
        let mut ident_names = emit_names.clone();
        ident_names.extend(emit_enums.iter().cloned());
        let msg_set: std::collections::BTreeSet<String> =
            pool.collect_names().into_iter().collect();
        IDENTS.with(|c| *c.borrow_mut() = unique_idents(&ident_names, &msg_set));
        emit_public_uses(&mut src, &pool, &pub_files);
        for name in &emit_enums {
            let ed = pool.get_enum(name).expect("emit enum");
            emit_enum(&mut src, &ed);
        }
        for name in &emit_names {
            let desc = pool.get_message(name).expect("emit");
            emit_message(&mut src, &desc);
            emit_map_decoders(&mut src, &desc);
        }
        emit_nested_mods(&mut src, &emit_names, &emit_enums);
        let services: Vec<_> = pool
            .collect_services()
            .into_iter()
            .filter(|s| file_matches(&wanted, &s.file_name))
            .collect();
        match stubs_setting() {
            Stubs::None => {}
            Stubs::Tonic if !services.is_empty() => {
                src.push_str("\n// --- gRPC stubs (protobuf-tonic, not tonic-prost) ---\n");
                src.push_str("use protobuf_tonic::ProtobufCodec;\n");
                src.push_str("use std::convert::Infallible;\n");
                src.push_str("use std::future::Future;\n");
                src.push_str("use std::pin::Pin;\n");
                src.push_str("use std::sync::Arc;\n");
                src.push_str("use std::task::{Context, Poll};\n");
                for svc in &services {
                    emit_service(&mut src, svc);
                }
            }
            Stubs::Kernel if !services.is_empty() => {
                src.push_str("\n// --- gRPC stubs (pbrs-grpc kernel) ---\n");
                for svc in &services {
                    emit_kernel_service(&mut src, svc);
                }
            }
            Stubs::Tonic | Stubs::Kernel => {}
        }
        src.push_str("}\n#[allow(unused_imports, reason = \"generated by protoc-gen-pbrs\")]\npub use __gen::*;\n");
        let stem = std::path::Path::new(target)
            .file_stem()
            .and_then(|s| s.to_str())
            .unwrap_or("generated");
        out_files.push((format!("{stem}.rs"), src));
    }
    Ok(out_files)
}

/// Generate from a FileDescriptorSet plus the proto paths to emit.
pub fn generate_from_file_descriptor_set(
    fds: &[u8],
    files_to_generate: &[String],
) -> Result<Vec<(String, String)>, ParseError> {
    let mut req = Vec::new();
    for f in files_to_generate {
        encode_string_field(&mut req, 1, f);
    }
    let mut pos = 0;
    while pos < fds.len() {
        let (n, w) = decode_tag(fds, &mut pos)?;
        if n == 1 && w == WIRE_LEN {
            let blob = read_len_bytes(fds, &mut pos)?;
            encode_len_field(&mut req, 15, blob);
        } else {
            wire::skip_field(fds, &mut pos, w)?;
        }
    }
    generate_from_code_generator_request(&req)
}

/// prost-build-shaped compile: `protoc` FileDescriptorSet, then gencode into
/// `OUT_DIR` (or [`Config::out_dir`]).
pub fn compile_protos(
    protos: &[impl AsRef<Path>],
    includes: &[impl AsRef<Path>],
) -> Result<(), ParseError> {
    Config::new().compile_protos(protos, includes)
}

/// Options for [`compile_protos`].
#[derive(Default)]
pub struct Config {
    out_dir: Option<PathBuf>,
    stubs: Stubs,
    emit_deps: bool,
}

impl Config {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn out_dir(&mut self, path: impl Into<PathBuf>) -> &mut Self {
        self.out_dir = Some(path.into());
        self
    }

    /// Choose which gRPC stub flavour to emit. Default [`Stubs::Kernel`].
    pub fn stubs(&mut self, stubs: Stubs) -> &mut Self {
        self.stubs = stubs;
        self
    }

    /// Emit tonic `FooClient`/`FooServer` stubs.
    ///
    /// Needed because the default is [`Stubs::Kernel`]. `false` means
    /// messages only. Mutually exclusive with [`Self::emit_kernel_stubs`];
    /// the last call wins.
    pub fn emit_tonic_stubs(&mut self, enable: bool) -> &mut Self {
        self.stubs = if enable { Stubs::Tonic } else { Stubs::None };
        self
    }

    /// Emit native `pbrs-grpc` `FooClient`/`FooServer` stubs.
    ///
    /// This is the default for [`Config::new`] / [`compile_protos`]. Pass
    /// `true` to be explicit; `false` is messages only. The generating crate
    /// must depend on `pbrs-grpc`. `FooClient` gets the same dialers as
    /// `Channel` (`connect`, `connect_tls`, `connect_unix`, `from_io`, and
    /// the lazy/`_with` variants) and the same overlays, including
    /// `https_scheme` for already-encrypted `from_io` streams and `origin`
    /// for `:authority`. Read those
    /// overlays with `scheme` / `authority` / `grpc_user_agent` /
    /// `rpc_timeout` / `waits_for_ready` / `compresses_outbound` /
    /// `gzip_level` / `accepts_compressed` / `concurrent_rpc_limit` / `stream_buffer_size` / `send_buffer_size` / `limits` / `config`.
    /// Methods you omit on the generated `Foo` trait answer `UNIMPLEMENTED`.
    /// Mutually exclusive with [`Self::emit_tonic_stubs`]; the last call wins.
    ///
    /// ```no_run
    /// // build.rs
    /// pbrs::codegen::compile_protos(&["proto/hello.proto"], &["proto"])
    ///     .expect("codegen");
    /// ```
    pub fn emit_kernel_stubs(&mut self, enable: bool) -> &mut Self {
        self.stubs = if enable { Stubs::Kernel } else { Stubs::None };
        self
    }

    /// Emit imported non-WKT messages into the same generated file.
    ///
    /// Needed for `grpc.testing.test.proto`, which imports
    /// `messages.proto` / `empty.proto`. `PURE_PROTOBUF_EMIT_DEPS=1`
    /// remains the plugin/env equivalent.
    pub fn emit_deps(&mut self, enable: bool) -> &mut Self {
        self.emit_deps = enable;
        self
    }

    pub fn compile_protos(
        &self,
        protos: &[impl AsRef<Path>],
        includes: &[impl AsRef<Path>],
    ) -> Result<(), ParseError> {
        let out = match &self.out_dir {
            Some(p) => p.clone(),
            None => PathBuf::from(
                std::env::var("OUT_DIR").map_err(|_| ParseError::new("missing OUT_DIR"))?,
            ),
        };
        std::fs::create_dir_all(&out).map_err(|e| ParseError::owned(e.to_string()))?;
        for p in protos {
            println!("cargo:rerun-if-changed={}", p.as_ref().display());
        }
        let fds_path = out.join("pbrs.fds");
        let mut cmd = Command::new("protoc");
        cmd.arg("--include_imports")
            .arg(format!("--descriptor_set_out={}", fds_path.display()));
        for inc in includes {
            cmd.arg("-I").arg(inc.as_ref());
        }
        for p in protos {
            cmd.arg(p.as_ref());
        }
        let status = cmd.status().map_err(|e| ParseError::owned(e.to_string()))?;
        if !status.success() {
            return Err(ParseError::owned(format!("protoc failed: {status}")));
        }
        let bytes = std::fs::read(&fds_path).map_err(|e| ParseError::owned(e.to_string()))?;
        let names: Vec<String> = protos
            .iter()
            .map(|p| {
                p.as_ref()
                    .file_name()
                    .and_then(|s| s.to_str())
                    .unwrap_or("generated.proto")
                    .to_string()
            })
            .collect();
        STUBS.with(|c| c.set(self.stubs));
        EMIT_DEPS.with(|c| c.set(self.emit_deps));
        let files = generate_from_file_descriptor_set(&bytes, &names);
        STUBS.with(|c| c.set(Stubs::default()));
        EMIT_DEPS.with(|c| c.set(false));
        let files = files?;
        for (name, src) in files {
            std::fs::write(out.join(name), src).map_err(|e| ParseError::owned(e.to_string()))?;
        }
        Ok(())
    }
}

pub fn encode_code_generator_response(files: &[(String, String)]) -> Vec<u8> {
    let mut out = Vec::new();
    // PROTO3_OPTIONAL | SUPPORTS_EDITIONS
    encode_varint_field(&mut out, 2, 3);
    encode_varint_field(&mut out, 3, 998); // EDITION_PROTO2
    encode_varint_field(&mut out, 4, 1000); // EDITION_2023
    for (name, content) in files {
        let mut file = Vec::new();
        encode_string_field(&mut file, 1, name);
        encode_string_field(&mut file, 15, content);
        encode_len_field(&mut out, 15, &file);
    }
    out
}

fn file_matches(wanted: &std::collections::BTreeSet<String>, file_name: &str) -> bool {
    if file_name.is_empty() {
        return true;
    }
    let file_stem = std::path::Path::new(file_name)
        .file_stem()
        .and_then(|s| s.to_str())
        .unwrap_or(file_name);
    wanted.iter().any(|w| {
        let w_stem = std::path::Path::new(w)
            .file_stem()
            .and_then(|s| s.to_str())
            .unwrap_or(w);
        w == file_name || w.ends_with(file_name) || file_name.ends_with(w) || w_stem == file_stem
    })
}

fn emit_fds(src: &mut String, fds: &[u8]) {
    src.push_str("pub const FILE_DESCRIPTOR_SET: &[u8] = &[\n");
    for (i, b) in fds.iter().enumerate() {
        if i % 16 == 0 {
            src.push_str("    ");
        }
        let _ = write!(src, "0x{b:02x},");
        if i % 16 == 15 {
            src.push('\n');
        }
    }
    if fds.len() % 16 != 0 {
        src.push('\n');
    }
    src.push_str("];\n\n");
}

fn encode_varint_field(out: &mut Vec<u8>, n: u32, v: u64) {
    crate::wire::encode_tag(out, n, crate::wire::WIRE_VARINT);
    encode_varint(out, v);
}

fn encode_string_field(out: &mut Vec<u8>, n: u32, s: &str) {
    encode_len_field(out, n, s.as_bytes());
}

fn ident_last(s: &str) -> String {
    let key = s.trim_start_matches('.');
    let last = key.rsplit('.').next().unwrap_or(key);
    rust_ident_from_last(last)
}

fn must_mangle_view_suffix(full: &str, messages: &std::collections::BTreeSet<String>) -> bool {
    let key = full.trim_start_matches('.');
    let last = key.rsplit('.').next().unwrap_or(key);
    let Some(without) = last.strip_suffix("View") else {
        return false;
    };
    if without.is_empty() {
        return false;
    }
    let sibling = match key.rsplit_once('.') {
        Some((parent, _)) => format!("{parent}.{without}"),
        None => without.to_string(),
    };
    messages.contains(&sibling)
}

fn file_stem_ident(path: &str) -> String {
    std::path::Path::new(path)
        .file_stem()
        .and_then(|s| s.to_str())
        .unwrap_or(path)
        .replace('-', "_")
}

fn emit_public_uses(src: &mut String, pool: &DescriptorPool, pub_files: &[String]) {
    for p in pub_files {
        let stem = file_stem_ident(p);
        let _ = writeln!(src, "pub use crate::{stem}::*;");
    }
    let _ = pool;
}

fn field_raw(f: &FieldDescriptor) -> String {
    FIELD_RAWS.with(|c| {
        c.borrow()
            .get(&f.number)
            .cloned()
            .unwrap_or_else(|| f.name.clone())
    })
}

fn unique_idents(
    full_names: &[String],
    messages: &std::collections::BTreeSet<String>,
) -> std::collections::BTreeMap<String, String> {
    let mut used: std::collections::BTreeSet<String> = std::collections::BTreeSet::new();
    let mut out = std::collections::BTreeMap::new();
    for full in full_names {
        let mut last = ident_last(full);
        if must_mangle_view_suffix(full, messages) {
            last.push('_');
        }
        let id = if used.contains(&last) {
            let parent = full.rsplit_once('.').map(|x| x.0).unwrap_or("X");
            format!("{}{last}", ident_last(parent))
        } else {
            last
        };
        used.insert(id.clone());
        out.insert(full.clone(), id);
    }
    out
}

fn rust_ident(s: &str) -> String {
    let key = s.trim_start_matches('.');
    if let Some(id) = IDENTS.with(|c| c.borrow().get(key).cloned()) {
        return id;
    }
    ident_last(key)
}

fn is_rust_keyword(id: &str) -> bool {
    matches!(
        id,
        "as" | "async"
            | "await"
            | "break"
            | "const"
            | "continue"
            | "crate"
            | "dyn"
            | "else"
            | "enum"
            | "extern"
            | "false"
            | "fn"
            | "for"
            | "if"
            | "impl"
            | "in"
            | "let"
            | "loop"
            | "match"
            | "mod"
            | "move"
            | "mut"
            | "pub"
            | "ref"
            | "return"
            | "self"
            | "Self"
            | "static"
            | "struct"
            | "super"
            | "trait"
            | "true"
            | "type"
            | "union"
            | "unsafe"
            | "use"
            | "where"
            | "while"
            | "abstract"
            | "become"
            | "box"
            | "do"
            | "final"
            | "macro"
            | "override"
            | "priv"
            | "try"
            | "typeof"
            | "unsized"
            | "virtual"
            | "yield"
            | "gen"
            | "unknown"
    )
}

fn field_ident_base(name: &str) -> String {
    let mut id = String::new();
    for (i, c) in name.chars().enumerate() {
        if c.is_ascii_alphanumeric() || c == '_' {
            id.push(c);
        } else if i > 0 {
            id.push('_');
        }
    }
    if id.is_empty() {
        id.push('_');
    }
    if id.chars().next().is_some_and(|c| c.is_ascii_digit()) {
        id = format!("f_{id}");
    }
    if matches!(
        id.as_str(),
        "crate" | "self" | "Self" | "super" | "_" | "new"
    ) {
        format!("{id}_")
    } else if is_rust_keyword(&id) {
        format!("r#{id}")
    } else {
        id
    }
}

fn field_name_with_collision_avoidance(desc: &MessageDescriptor, f: &FieldDescriptor) -> String {
    let name = f.name.as_str();
    for prefix in ["clear_", "has_", "set_"] {
        if let Some(rest) = name.strip_prefix(prefix) {
            if desc.fields.values().any(|o| o.name == rest) {
                return format!("{name}_{}", f.number);
            }
        }
    }
    for suffix in ["_mut", "_opt"] {
        if let Some(rest) = name.strip_suffix(suffix) {
            if desc.fields.values().any(|o| o.name == rest) {
                return format!("{name}_{}", f.number);
            }
        }
    }
    name.to_string()
}

fn bind_field_idents(desc: &MessageDescriptor) {
    let mut used = std::collections::BTreeSet::new();
    used.insert("unknown".into());
    let mut raw_used = std::collections::BTreeSet::new();
    let mut out = std::collections::BTreeMap::new();
    let mut raws = std::collections::BTreeMap::new();
    for f in desc.fields.values() {
        let mut raw = field_name_with_collision_avoidance(desc, f);
        if !raw_used.insert(raw.clone()) {
            raw = format!("{}_{}", raw, f.number);
            raw_used.insert(raw.clone());
        }
        let mut id = field_ident_base(&raw);
        if !used.insert(id.clone()) {
            id = format!("{}_{}", id.trim_start_matches("r#"), f.number);
            used.insert(id.clone());
        }
        out.insert(f.number, id);
        raws.insert(f.number, raw);
    }
    FIELD_IDENTS.with(|c| *c.borrow_mut() = out);
    FIELD_RAWS.with(|c| *c.borrow_mut() = raws);
}

fn field_id(f: &FieldDescriptor) -> String {
    FIELD_IDENTS.with(|c| {
        c.borrow()
            .get(&f.number)
            .cloned()
            .unwrap_or_else(|| field_ident_base(&f.name))
    })
}

fn rust_ident_from_last(last: &str) -> String {
    let mut id = String::new();
    for (i, c) in last.chars().enumerate() {
        if c.is_ascii_alphanumeric() || c == '_' {
            id.push(c);
        } else if i > 0 {
            id.push('_');
        }
    }
    if id.is_empty() {
        id.push('M');
    }
    if matches!(
        id.as_str(),
        "crate" | "self" | "Self" | "super" | "_" | "new"
    ) {
        return format!("{id}_");
    }
    if is_rust_keyword(&id) {
        return format!("r#{id}");
    }
    if matches!(
        id.as_str(),
        "Value" | "Option" | "Result" | "Vec" | "String" | "Box" | "Type"
    ) {
        return format!("Pb{id}");
    }
    id
}

fn scalar_type(field: &FieldDescriptor) -> String {
    match field.field_type {
        FieldType::Double => "f64".into(),
        FieldType::Float => "f32".into(),
        FieldType::Int64 | FieldType::Sint64 | FieldType::Sfixed64 => "i64".into(),
        FieldType::Int32 | FieldType::Sint32 | FieldType::Sfixed32 | FieldType::Enum => {
            "i32".into()
        }
        FieldType::Uint64 | FieldType::Fixed64 => "u64".into(),
        FieldType::Uint32 | FieldType::Fixed32 => "u32".into(),
        FieldType::Bool => "bool".into(),
        FieldType::String => "pbrs::rt::LazyStr".into(),
        FieldType::Bytes => "pbrs::rt::LazyBytes".into(),
        FieldType::Message | FieldType::Group => rust_ident(
            field
                .type_name
                .as_deref()
                .or_else(|| field.message.as_ref().map(|m| m.full_name.as_str()))
                .unwrap_or("UnknownMsg"),
        ),
    }
}

fn map_kv(field: &FieldDescriptor) -> (String, String) {
    let entry = field.message.as_ref();
    let k = entry
        .and_then(|e| e.field(1))
        .map(scalar_type)
        .unwrap_or_else(|| "ProtoString".into());
    let v = entry
        .and_then(|e| e.field(2))
        .map(scalar_type)
        .unwrap_or_else(|| "i32".into());
    (k, v)
}

fn is_option(field: &FieldDescriptor) -> bool {
    !field.is_map
        && field.cardinality != Cardinality::Repeated
        && (field.field_type == FieldType::Message
            || field.field_type == FieldType::Group
            || field.presence == Presence::Explicit)
}

fn is_packed_scalar(f: &FieldDescriptor) -> bool {
    !f.is_map && f.cardinality == Cardinality::Repeated && f.packed && f.field_type.is_packable()
}

fn is_lazy_msg(f: &FieldDescriptor) -> bool {
    !f.is_map
        && f.cardinality != Cardinality::Repeated
        && f.field_type == FieldType::Message
        && !f.delimited
}

fn is_wkt_msg(f: &FieldDescriptor) -> bool {
    let name = f
        .type_name
        .as_deref()
        .or_else(|| f.message.as_ref().map(|m| m.full_name.as_str()))
        .unwrap_or("")
        .trim_start_matches('.');
    name.starts_with("google.protobuf.")
}

fn is_memcpy_packed(f: &FieldDescriptor) -> bool {
    f.packed
        && matches!(
            f.field_type,
            FieldType::Fixed32
                | FieldType::Fixed64
                | FieldType::Sfixed32
                | FieldType::Sfixed64
                | FieldType::Float
                | FieldType::Double
        )
}

/// packed_fixed32/64 and packed_float stay on the hot struct. Putting every
/// memcpy-packed TAT slot on hot grew `size_of` to 824 and lost strings vs v4.
fn is_hot_packed_memcpy(f: &FieldDescriptor) -> bool {
    is_memcpy_packed(f)
        && f.name.starts_with("packed_")
        && matches!(
            f.field_type,
            FieldType::Fixed32 | FieldType::Fixed64 | FieldType::Float
        )
}

fn is_hot_repeated_nested(f: &FieldDescriptor) -> bool {
    !f.is_map
        && f.cardinality == Cardinality::Repeated
        && f.field_type == FieldType::Message
        && !f.delimited
        && f.name == "repeated_nested_message"
}

fn is_light_merge_field(f: &FieldDescriptor) -> bool {
    if f.is_map || f.cardinality == Cardinality::Repeated {
        return false;
    }
    !matches!(
        f.field_type,
        FieldType::String | FieldType::Bytes | FieldType::Message | FieldType::Group
    )
}

fn is_cold_field(f: &FieldDescriptor) -> bool {
    // Maps and repeated string/bytes stay hot (map_64 / strings).
    // packed_fixed32/64, packed_float, and repeated_nested_message stay hot.
    if f.is_map || is_hot_packed_memcpy(f) || is_hot_repeated_nested(f) {
        return false;
    }
    if f.cardinality == Cardinality::Repeated {
        return !matches!(f.field_type, FieldType::String | FieldType::Bytes);
    }
    is_lazy_msg(f) && is_wkt_msg(f)
}

fn uses_sparse_cold(desc: &MessageDescriptor) -> bool {
    !desc.message_set_wire_format
        && (1..=4).contains(
            &desc
                .fields
                .values()
                .filter(|f| is_light_merge_field(f))
                .count(),
        )
        && desc
            .fields
            .values()
            .filter(|f| !is_light_merge_field(f))
            .count()
            >= 3
}

fn uses_cold_storage(desc: &MessageDescriptor) -> bool {
    uses_sparse_cold(desc) || desc.fields.values().filter(|f| is_cold_field(f)).count() >= 6
}

fn stored_hot(desc: &MessageDescriptor, f: &FieldDescriptor) -> bool {
    !stored_cold(desc, f)
}

fn stored_cold(desc: &MessageDescriptor, f: &FieldDescriptor) -> bool {
    if uses_sparse_cold(desc) {
        !is_light_merge_field(f)
    } else {
        uses_cold_storage(desc) && is_cold_field(f)
    }
}

fn store_mut(desc: &MessageDescriptor, f: &FieldDescriptor) -> String {
    let id = field_id(f);
    if stored_cold(desc, f) {
        format!("self.cold_mut().{id}")
    } else {
        format!("self.{id}")
    }
}

fn emit_zeroed_fields_struct<'a>(
    src: &mut String,
    name: &str,
    fields: impl Iterator<Item = &'a FieldDescriptor>,
) {
    let _ = writeln!(src, "#[derive(Clone, Debug, PartialEq)]");
    let _ = writeln!(src, "struct {name} {{");
    for f in fields {
        let _ = writeln!(src, "    {}: {},", field_id(f), field_storage_ty(f));
    }
    let _ = writeln!(src, "}}");
    let _ = writeln!(src, "impl Default for {name} {{");
    let _ = writeln!(src, "    #[inline(always)]");
    let _ = writeln!(
        src,
        "    fn default() -> Self {{ unsafe {{ pbrs::rt::zeroed_message() }} }}"
    );
    let _ = writeln!(src, "}}");
}

fn emit_box_eq(src: &mut String, field: &str, ty: &str) {
    let _ = writeln!(
        src,
        "        match (self.{field}.as_deref(), other.{field}.as_deref()) {{"
    );
    let _ = writeln!(src, "            (None, None) => {{}}");
    let _ = writeln!(
        src,
        "            (Some(a), Some(b)) => if a != b {{ return false; }}"
    );
    let _ = writeln!(
        src,
        "            (None, Some(b)) => if *b != {ty}::default() {{ return false; }}"
    );
    let _ = writeln!(
        src,
        "            (Some(a), None) => if *a != {ty}::default() {{ return false; }}"
    );
    let _ = writeln!(src, "        }}");
}

fn packed_storage_ty(f: &FieldDescriptor) -> &'static str {
    match f.field_type {
        FieldType::Int32 | FieldType::Enum => "pbrs::rt::PackedI32",
        FieldType::Int64 => "pbrs::rt::PackedI64",
        FieldType::Uint32 => "pbrs::rt::PackedU32",
        FieldType::Uint64 => "pbrs::rt::PackedU64",
        FieldType::Sint32 => "pbrs::rt::PackedS32",
        FieldType::Sint64 => "pbrs::rt::PackedS64",
        FieldType::Fixed32 => "pbrs::rt::PackedFx32",
        FieldType::Sfixed32 => "pbrs::rt::PackedSfx32",
        FieldType::Fixed64 => "pbrs::rt::PackedFx64",
        FieldType::Sfixed64 => "pbrs::rt::PackedSfx64",
        FieldType::Float => "pbrs::rt::PackedF32",
        FieldType::Double => "pbrs::rt::PackedF64",
        FieldType::Bool => "pbrs::rt::PackedBool",
        _ => "pbrs::rt::PackedI32",
    }
}

fn field_storage_ty(f: &FieldDescriptor) -> String {
    if f.is_map {
        let (k, v) = map_kv(f);
        return format!("Map<{k}, {v}>");
    }
    let t = scalar_type(f);
    if is_packed_scalar(f) {
        packed_storage_ty(f).into()
    } else if f.cardinality == Cardinality::Repeated {
        format!("Repeated<{t}>")
    } else if is_lazy_msg(f) {
        format!("pbrs::rt::LazyMsg<{t}>")
    } else if f.field_type == FieldType::Message || f.field_type == FieldType::Group {
        format!("Option<Box<{t}>>")
    } else if is_option(f) {
        match f.field_type {
            FieldType::Bool => "pbrs::rt::OptBool".into(),
            FieldType::String => "Option<Box<pbrs::rt::LazyStr>>".into(),
            FieldType::Bytes => "Option<Box<pbrs::rt::LazyBytes>>".into(),
            _ => format!("Option<{t}>"),
        }
    } else {
        t
    }
}

fn emit_message(src: &mut String, desc: &MessageDescriptor) {
    bind_field_idents(desc);
    let name = rust_ident(&desc.full_name);
    let view = format!("{name}View");
    let mut_ = format!("{name}Mut");
    let cold_name = format!("{name}Cold");
    let use_cold = uses_cold_storage(desc);
    if use_cold {
        emit_zeroed_fields_struct(
            src,
            &cold_name,
            desc.fields.values().filter(|f| stored_cold(desc, f)),
        );
    }
    let _ = writeln!(src, "#[derive(Clone, Debug)]");
    let _ = writeln!(src, "pub struct {name} {{");
    for f in desc.fields.values().filter(|f| stored_hot(desc, f)) {
        let _ = writeln!(src, "    {}: {},", field_id(f), field_storage_ty(f));
    }
    if use_cold {
        let _ = writeln!(src, "    cold: Option<Box<{cold_name}>>,");
    }
    let _ = writeln!(src, "    unknown: UnknownFields,");
    let _ = writeln!(src, "    cached_size: pbrs::rt::CachedSize,");
    let _ = writeln!(src, "}}");
    let _ = writeln!(src, "impl PartialEq for {name} {{");
    let _ = writeln!(src, "    fn eq(&self, other: &Self) -> bool {{");
    for f in desc.fields.values().filter(|f| stored_hot(desc, f)) {
        let id = field_id(f);
        let _ = writeln!(
            src,
            "        if self.{id} != other.{id} {{ return false; }}"
        );
    }
    if use_cold {
        emit_box_eq(src, "cold", &cold_name);
    }
    let _ = writeln!(src, "        self.unknown == other.unknown");
    let _ = writeln!(src, "    }}");
    let _ = writeln!(src, "}}");
    let _ = writeln!(src, "impl Eq for {name} {{}}");
    let _ = writeln!(src, "impl Default for {name} {{");
    let _ = writeln!(src, "    #[inline(always)]");
    let _ = writeln!(src, "    fn default() -> Self {{");
    let _ = writeln!(src, "        unsafe {{ pbrs::rt::zeroed_message() }}");
    let _ = writeln!(src, "    }}");
    let _ = writeln!(src, "}}");

    let _ = writeln!(src, "impl {name} {{");
    let _ = writeln!(src, "    pub fn new() -> Self {{ Self::default() }}");
    if use_cold {
        let _ = writeln!(
            src,
            "    #[inline(always)] fn cold_mut(&mut self) -> &mut {cold_name} {{ self.cold.get_or_insert_with(|| Box::new({cold_name}::default())) }}"
        );
    }
    let empty_ok = !desc
        .fields
        .values()
        .any(|f| f.cardinality == Cardinality::Required);
    let _ = writeln!(src, "    pub const EMPTY_PARSE_OK: bool = {empty_ok};");
    let _ = writeln!(
        src,
        "    pub const FULL_NAME: &'static str = \"{}\";",
        desc.full_name
    );
    for f in desc.fields.values() {
        emit_accessors(src, desc, f);
    }
    emit_codec(src, desc);
    if std::env::var("PURE_PROTOBUF_NO_REFLECT").as_deref() != Ok("1") {
        emit_json_text(src, desc);
    }
    let _ = writeln!(src, "}}");
    let _ = writeln!(src, "pbrs::impl_typed_message!({name}, {view}, {mut_});");
}

fn emit_oneof_clear(src: &mut String, desc: &MessageDescriptor, f: &FieldDescriptor) {
    let Some(idx) = f.oneof_index else {
        return;
    };
    let Some(members) = desc.oneofs.get(idx as usize) else {
        return;
    };
    for n in members {
        if *n == f.number {
            continue;
        }
        if let Some(sib) = desc.field(*n) {
            let st = store_mut(desc, sib);
            if is_lazy_msg(sib) {
                let _ = writeln!(src, "        {st} = Default::default();");
            } else if is_option(sib) && sib.field_type == FieldType::Bool {
                let _ = writeln!(src, "        {st} = pbrs::rt::OptBool::NONE;");
            } else if is_option(sib) {
                let _ = writeln!(src, "        {st} = None;");
            } else if sib.cardinality == Cardinality::Repeated || sib.is_map {
                let _ = writeln!(src, "        {st}.clear();");
            } else {
                let _ = writeln!(src, "        {st} = Default::default();");
            }
        }
    }
}

fn emit_accessors(src: &mut String, desc: &MessageDescriptor, f: &FieldDescriptor) {
    let id = field_id(f);
    let m = field_raw(f);
    if f.is_map {
        let (k, v) = map_kv(f);
        let st = store_mut(desc, f);
        if stored_cold(desc, f) {
            let _ = writeln!(
                src,
                "    pub fn {id}(&self) -> MapView<'_, {k}, {v}> {{ self.cold.as_ref().map(|c| c.{id}.as_view()).unwrap_or_else(MapView::empty) }}"
            );
        } else {
            let _ = writeln!(
                src,
                "    pub fn {id}(&self) -> MapView<'_, {k}, {v}> {{ self.{id}.as_view() }}"
            );
        }
        let _ = writeln!(
            src,
            "    pub fn {id}_mut(&mut self) -> MapMut<'_, {k}, {v}> {{ self.cached_size.dirty(); {st}.as_mut() }}"
        );
        let _ = writeln!(
            src,
            "    pub fn set_{m}(&mut self, v: Map<{k}, {v}>) {{ self.cached_size.dirty(); {st} = v; }}"
        );
        return;
    }
    if f.cardinality == Cardinality::Repeated {
        let t = scalar_type(f);
        let st = store_mut(desc, f);
        if stored_cold(desc, f) {
            let _ = writeln!(
                src,
                "    pub fn {id}(&self) -> RepeatedView<'_, {t}> {{ self.cold.as_ref().map(|c| c.{id}.as_view()).unwrap_or_else(|| RepeatedView::from_slice(&[])) }}"
            );
        } else {
            let _ = writeln!(
                src,
                "    pub fn {id}(&self) -> RepeatedView<'_, {t}> {{ self.{id}.as_view() }}"
            );
        }
        let _ = writeln!(
            src,
            "    pub fn {id}_mut(&mut self) -> RepeatedMut<'_, {t}> {{ self.cached_size.dirty(); {st}.as_mut() }}"
        );
        if is_packed_scalar(f) {
            let _ = writeln!(
                src,
                "    pub fn set_{m}(&mut self, v: impl IntoIterator<Item = {t}>) {{ self.cached_size.dirty(); {st} = pbrs::rt::Packed::from_repeated(v.into_iter().collect()); }}"
            );
        } else {
            let _ = writeln!(
                src,
                "    pub fn set_{m}(&mut self, v: impl IntoIterator<Item = {t}>) {{ self.cached_size.dirty(); {st} = v.into_iter().collect(); }}"
            );
        }
        return;
    }
    if f.field_type == FieldType::Message || f.field_type == FieldType::Group {
        let t = scalar_type(f);
        let st = store_mut(desc, f);
        if stored_cold(desc, f) {
            let _ = writeln!(
                src,
                "    pub fn has_{m}(&self) -> bool {{ self.cold.as_ref().is_some_and(|c| c.{id}.is_some()) }}"
            );
            let _ = writeln!(
                src,
                "    pub fn {id}(&self) -> &{t} {{ self.cold.as_ref().and_then(|c| c.{id}.as_deref()).unwrap_or(pbrs::gen_support::default_instance_of()) }}"
            );
            let _ = writeln!(
                src,
                "    pub fn {id}_opt(&self) -> Option<&{t}> {{ self.cold.as_ref().and_then(|c| c.{id}.as_deref()) }}"
            );
        } else {
            let _ = writeln!(
                src,
                "    pub fn has_{m}(&self) -> bool {{ self.{id}.is_some() }}"
            );
            let _ = writeln!(
                src,
                "    pub fn {id}(&self) -> &{t} {{ self.{id}.as_deref().unwrap_or(pbrs::gen_support::default_instance_of()) }}"
            );
            let _ = writeln!(
                src,
                "    pub fn {id}_opt(&self) -> Option<&{t}> {{ self.{id}.as_deref() }}"
            );
        }
        let _ = writeln!(
            src,
            "    pub fn {id}_view(&self) -> {t}View<'_> {{ {t}View(self.{id}()) }}"
        );
        let _ = writeln!(src, "    pub fn set_{m}(&mut self, v: {t}) {{");
        let _ = writeln!(src, "        self.cached_size.dirty();");
        emit_oneof_clear(src, desc, f);
        if is_lazy_msg(f) {
            let _ = writeln!(src, "        {st} = pbrs::rt::LazyMsg::from_owned(v);");
        } else {
            let _ = writeln!(src, "        {st} = Some(Box::new(v));");
        }
        let _ = writeln!(src, "    }}");
        if is_lazy_msg(f) {
            let _ = writeln!(
                src,
                "    pub fn {id}_mut(&mut self) -> &mut {t} {{ self.cached_size.dirty(); {st}.get_or_insert() }}"
            );
            let _ = writeln!(
                src,
                "    pub fn clear_{m}(&mut self) {{ self.cached_size.dirty(); {st}.clear(); }}"
            );
        } else {
            let _ = writeln!(
                src,
                "    pub fn {id}_mut(&mut self) -> &mut {t} {{ self.cached_size.dirty(); {st}.get_or_insert_with(|| Box::new({t}::default())).as_mut() }}"
            );
            let _ = writeln!(
                src,
                "    pub fn clear_{m}(&mut self) {{ self.cached_size.dirty(); {st} = None; }}"
            );
        }
        return;
    }
    if f.field_type == FieldType::String {
        let fallback = string_default_lit(f);
        let st = store_mut(desc, f);
        let read = if stored_cold(desc, f) {
            format!("self.cold.as_ref().and_then(|c| c.{id}.as_ref())")
        } else {
            format!("self.{id}.as_ref()")
        };
        let read_view = if stored_cold(desc, f) {
            format!("self.cold.as_ref().map(|c| c.{id}.as_view())")
        } else {
            format!("Some(self.{id}.as_view())")
        };
        if is_option(f) {
            let _ = writeln!(
                src,
                "    pub fn has_{m}(&self) -> bool {{ {read}.is_some() }}"
            );
            let _ = writeln!(
                src,
                "    pub fn {id}(&self) -> &pbrs::ProtoStr {{ {read}.map(|s| s.as_view()).unwrap_or_else(|| pbrs::ProtoStr::from_bytes({fallback})) }}"
            );
            let _ = writeln!(
                src,
                "    pub fn {id}_opt(&self) -> Option<&pbrs::ProtoStr> {{ {read}.map(|s| s.as_view()) }}"
            );
            let _ = writeln!(
                src,
                "    pub fn set_{m}(&mut self, v: impl pbrs::IntoProxied<ProtoString>) {{"
            );
            let _ = writeln!(src, "        self.cached_size.dirty();");
            emit_oneof_clear(src, desc, f);
            let _ = writeln!(
                src,
                "        {st} = Some(Box::new(pbrs::rt::LazyStr::owned(v.into_proxied(pbrs::__internal::Private))));"
            );
            let _ = writeln!(src, "    }}");
            let _ = writeln!(
                src,
                "    pub fn clear_{m}(&mut self) {{ self.cached_size.dirty(); {st} = None; }}"
            );
        } else {
            let _ = writeln!(
                src,
                "    pub fn {id}(&self) -> &pbrs::ProtoStr {{ {read_view}.unwrap_or_else(|| pbrs::ProtoStr::from_bytes({fallback})) }}"
            );
            let _ = writeln!(
                src,
                "    pub fn set_{m}(&mut self, v: impl pbrs::IntoProxied<ProtoString>) {{ self.cached_size.dirty(); {st} = pbrs::rt::LazyStr::owned(v.into_proxied(pbrs::__internal::Private)); }}"
            );
        }
        return;
    }
    if f.field_type == FieldType::Bytes {
        let fallback = bytes_default_lit(f);
        let st = store_mut(desc, f);
        let read = if stored_cold(desc, f) {
            format!("self.cold.as_ref().and_then(|c| c.{id}.as_ref())")
        } else {
            format!("self.{id}.as_ref()")
        };
        let read_bytes = if stored_cold(desc, f) {
            format!("self.cold.as_ref().map(|c| c.{id}.as_bytes())")
        } else {
            format!("Some(self.{id}.as_bytes())")
        };
        if is_option(f) {
            let _ = writeln!(
                src,
                "    pub fn {id}(&self) -> &[u8] {{ {read}.map(|b| b.as_bytes()).unwrap_or({fallback}) }}"
            );
            let _ = writeln!(
                src,
                "    pub fn {id}_opt(&self) -> Option<&[u8]> {{ {read}.map(|b| b.as_bytes()) }}"
            );
            let _ = writeln!(
                src,
                "    pub fn has_{m}(&self) -> bool {{ {read}.is_some() }}"
            );
            let _ = writeln!(
                src,
                "    pub fn set_{m}(&mut self, v: impl pbrs::IntoProxied<ProtoBytes>) {{ self.cached_size.dirty(); {st} = Some(Box::new(pbrs::rt::LazyBytes::owned(v.into_proxied(pbrs::__internal::Private)))); }}"
            );
            let _ = writeln!(
                src,
                "    pub fn clear_{m}(&mut self) {{ self.cached_size.dirty(); {st} = None; }}"
            );
        } else {
            let _ = writeln!(
                src,
                "    pub fn {id}(&self) -> &[u8] {{ {read_bytes}.unwrap_or({fallback}) }}"
            );
            let _ = writeln!(
                src,
                "    pub fn set_{m}(&mut self, v: impl pbrs::IntoProxied<ProtoBytes>) {{ self.cached_size.dirty(); {st} = pbrs::rt::LazyBytes::owned(v.into_proxied(pbrs::__internal::Private)); }}"
            );
        }
        return;
    }
    let t = scalar_type(f);
    let enum_ty = enum_api_ty(f);
    let get_ty = enum_ty.as_deref().unwrap_or(t.as_str());
    let wrap_open = enum_ty
        .as_ref()
        .map(|e| format!("{e}("))
        .unwrap_or_default();
    let wrap_close = if enum_ty.is_some() { ")" } else { "" };
    let def = scalar_default_expr(f);
    let set_ty = if enum_ty.is_some() {
        format!("impl Into<{t}>")
    } else {
        t.clone()
    };
    let set_val = if enum_ty.is_some() { "v.into()" } else { "v" };
    if is_option(f) && f.field_type == FieldType::Bool {
        let _ = writeln!(
            src,
            "    pub fn has_{m}(&self) -> bool {{ self.{id}.is_some() }}"
        );
        let _ = writeln!(
            src,
            "    pub fn {id}(&self) -> bool {{ self.{id}.unwrap_or({def}) }}"
        );
        let _ = writeln!(
            src,
            "    pub fn {id}_opt(&self) -> Option<bool> {{ self.{id}.get() }}"
        );
        let _ = writeln!(src, "    pub fn set_{m}(&mut self, v: bool) {{");
        let _ = writeln!(src, "        self.cached_size.dirty();");
        emit_oneof_clear(src, desc, f);
        let _ = writeln!(src, "        self.{id} = pbrs::rt::OptBool::some(v);");
        let _ = writeln!(src, "    }}");
        let _ = writeln!(
            src,
            "    pub fn clear_{m}(&mut self) {{ self.cached_size.dirty(); self.{id} = pbrs::rt::OptBool::NONE; }}"
        );
    } else if is_option(f) {
        let _ = writeln!(
            src,
            "    pub fn has_{m}(&self) -> bool {{ self.{id}.is_some() }}"
        );
        let _ = writeln!(
            src,
            "    pub fn {id}(&self) -> {get_ty} {{ {wrap_open}self.{id}.unwrap_or({def}){wrap_close} }}"
        );
        let _ = writeln!(
            src,
            "    pub fn {id}_opt(&self) -> Option<{get_ty}> {{ self.{id}.map(|v| {wrap_open}v{wrap_close}) }}"
        );
        let _ = writeln!(src, "    pub fn set_{m}(&mut self, v: {set_ty}) {{");
        let _ = writeln!(src, "        self.cached_size.dirty();");
        emit_oneof_clear(src, desc, f);
        let _ = writeln!(src, "        self.{id} = Some({set_val});");
        let _ = writeln!(src, "    }}");
        let _ = writeln!(
            src,
            "    pub fn clear_{m}(&mut self) {{ self.cached_size.dirty(); self.{id} = None; }}"
        );
    } else {
        let _ = writeln!(
            src,
            "    pub fn {id}(&self) -> {get_ty} {{ {wrap_open}self.{id}{wrap_close} }}"
        );
        let _ = writeln!(
            src,
            "    pub fn set_{m}(&mut self, v: {set_ty}) {{ self.cached_size.dirty(); self.{id} = {set_val}; }}"
        );
    }
}

fn rust_str(s: &str) -> String {
    format!("\"{}\"", s.escape_default())
}

fn json_match_keys(f: &FieldDescriptor) -> String {
    let json = rust_str(&f.json_name);
    if f.json_name == f.name {
        json
    } else {
        format!("{} | {}", json, rust_str(&f.name))
    }
}

fn is_real_oneof(desc: &MessageDescriptor, f: &FieldDescriptor) -> bool {
    f.oneof_index
        .and_then(|i| desc.oneofs.get(i as usize))
        .is_some_and(|members| members.len() > 1)
}

fn typed_scalar(ty: FieldType) -> bool {
    matches!(
        ty,
        FieldType::Bool
            | FieldType::Int32
            | FieldType::Int64
            | FieldType::Uint32
            | FieldType::Uint64
            | FieldType::Sint32
            | FieldType::Sint64
            | FieldType::Fixed32
            | FieldType::Fixed64
            | FieldType::Sfixed32
            | FieldType::Sfixed64
            | FieldType::Float
            | FieldType::Double
            | FieldType::String
            | FieldType::Bytes
            | FieldType::Enum
    )
}

fn typed_map_key(ty: FieldType) -> bool {
    matches!(
        ty,
        FieldType::Bool
            | FieldType::Int32
            | FieldType::Int64
            | FieldType::Uint32
            | FieldType::Uint64
            | FieldType::Sint32
            | FieldType::Sint64
            | FieldType::Fixed32
            | FieldType::Fixed64
            | FieldType::Sfixed32
            | FieldType::Sfixed64
            | FieldType::String
    )
}

fn enum_supports_typed(f: &FieldDescriptor) -> bool {
    let Some(en) = f.enum_ty.as_ref() else {
        return false;
    };
    if en.closed {
        return false;
    }
    !en.full_name
        .trim_start_matches('.')
        .starts_with("google.protobuf.")
}

fn message_type_name(f: &FieldDescriptor) -> &str {
    f.type_name
        .as_deref()
        .or_else(|| f.message.as_ref().map(|m| m.full_name.as_str()))
        .unwrap_or("")
        .trim_start_matches('.')
}

fn is_fieldwise_wkt(name: &str) -> bool {
    matches!(
        name,
        "google.protobuf.Timestamp"
            | "google.protobuf.Duration"
            | "google.protobuf.Empty"
            | "google.protobuf.BoolValue"
            | "google.protobuf.Int32Value"
            | "google.protobuf.Int64Value"
            | "google.protobuf.UInt32Value"
            | "google.protobuf.UInt64Value"
            | "google.protobuf.FloatValue"
            | "google.protobuf.DoubleValue"
            | "google.protobuf.StringValue"
            | "google.protobuf.BytesValue"
    )
}

/// Official proto3 JSON encode / decode helpers and the `value` getter expr.
fn wrapper_json_spec(name: &str) -> Option<(&'static str, &'static str, &'static str)> {
    Some(match name {
        "google.protobuf.BoolValue" => ("boolean", "as_bool", "self.value()"),
        "google.protobuf.Int32Value" => ("int32", "as_i32", "self.value()"),
        "google.protobuf.Int64Value" => ("int64", "as_i64", "self.value()"),
        "google.protobuf.UInt32Value" => ("uint32", "as_u32", "self.value()"),
        "google.protobuf.UInt64Value" => ("uint64", "as_u64", "self.value()"),
        "google.protobuf.FloatValue" => ("float", "as_f32", "self.value()"),
        "google.protobuf.DoubleValue" => ("double", "as_f64", "self.value()"),
        "google.protobuf.StringValue" => ("string", "as_str", "self.value().as_bytes()"),
        "google.protobuf.BytesValue" => ("bytes", "as_bytes", "self.value()"),
        _ => return None,
    })
}

fn field_supports_typed_json_message(f: &FieldDescriptor, depth: u32) -> bool {
    let name = message_type_name(f);
    if is_fieldwise_wkt(name) {
        return true;
    }
    if name.starts_with("google.protobuf.") {
        return false;
    }
    let Some(m) = f.message.as_ref() else {
        return false;
    };
    m.fields
        .values()
        .all(|nf| field_supports_typed_json(nf, depth + 1))
}

fn field_supports_typed_json(f: &FieldDescriptor, depth: u32) -> bool {
    if depth > 32 {
        return false;
    }
    if f.is_map {
        if !typed_map_key(map_key_ty(f)) {
            return false;
        }
        let Some(entry) = f.message.as_ref() else {
            return false;
        };
        let Some(vf) = entry.field(2) else {
            return false;
        };
        return field_supports_typed_json(vf, depth + 1);
    }
    if f.cardinality == Cardinality::Repeated {
        return match f.field_type {
            FieldType::Message => field_supports_typed_json_message(f, depth),
            FieldType::Enum => enum_supports_typed(f),
            other => typed_scalar(other),
        };
    }
    match f.field_type {
        FieldType::Message => field_supports_typed_json_message(f, depth),
        FieldType::Enum => enum_supports_typed(f),
        other => typed_scalar(other),
    }
}

fn enum_name_from_i32(f: &FieldDescriptor, expr: &str) -> String {
    let Some(en) = f.enum_ty.as_ref() else {
        return "None".into();
    };
    let mut arms = String::new();
    for (n, name) in &en.values {
        let _ = write!(arms, "{n} => Some(\"{}\"), ", name.escape_default());
    }
    format!("match {expr} {{ {arms}_ => None }}")
}

fn enum_lookup_closure(f: &FieldDescriptor) -> String {
    let Some(en) = f.enum_ty.as_ref() else {
        return "|_| None".into();
    };
    let mut arms = String::new();
    for (name, n) in &en.names {
        let _ = write!(arms, "\"{}\" => Some({n}), ", name.escape_default());
    }
    format!("|s| match s {{ {arms}_ => None }}")
}

fn json_encode_leaf(f: &FieldDescriptor, expr: &str, view: bool) -> String {
    match f.field_type {
        FieldType::Bool => format!("pbrs::json::boolean({expr})"),
        FieldType::Int32 | FieldType::Sint32 | FieldType::Sfixed32 => {
            format!("pbrs::json::int32({expr})")
        }
        FieldType::Int64 | FieldType::Sint64 | FieldType::Sfixed64 => {
            format!("pbrs::json::int64({expr})")
        }
        FieldType::Uint32 | FieldType::Fixed32 => format!("pbrs::json::uint32({expr})"),
        FieldType::Uint64 | FieldType::Fixed64 => format!("pbrs::json::uint64({expr})"),
        FieldType::Float => format!("pbrs::json::float({expr})"),
        FieldType::Double => format!("pbrs::json::double({expr})"),
        FieldType::String => format!("pbrs::json::string({expr}.as_bytes())"),
        FieldType::Bytes if view => format!("pbrs::json::bytes({expr}.as_bytes())"),
        FieldType::Bytes => format!("pbrs::json::bytes({expr})"),
        FieldType::Enum => {
            let n = if view {
                expr.to_string()
            } else {
                format!("i32::from({expr})")
            };
            format!(
                "{{ let n = {n}; pbrs::json::enumeration({}, n) }}",
                enum_name_from_i32(f, "n")
            )
        }
        FieldType::Message | FieldType::Group => format!("{expr}.to_json_value()?"),
    }
}

fn json_decode_leaf(f: &FieldDescriptor, val: &str) -> String {
    match f.field_type {
        FieldType::Bool => format!("pbrs::json::as_bool({val})?"),
        FieldType::Int32 | FieldType::Sint32 | FieldType::Sfixed32 => {
            format!("pbrs::json::as_i32({val})?")
        }
        FieldType::Int64 | FieldType::Sint64 | FieldType::Sfixed64 => {
            format!("pbrs::json::as_i64({val})?")
        }
        FieldType::Uint32 | FieldType::Fixed32 => format!("pbrs::json::as_u32({val})?"),
        FieldType::Uint64 | FieldType::Fixed64 => format!("pbrs::json::as_u64({val})?"),
        FieldType::Float => format!("pbrs::json::as_f32({val})?"),
        FieldType::Double => format!("pbrs::json::as_f64({val})?"),
        FieldType::String => format!("pbrs::json::as_str({val})?"),
        FieldType::Bytes => format!("pbrs::json::as_bytes({val})?"),
        FieldType::Enum => format!(
            "pbrs::json::as_enum({val}, ignore, {})?",
            enum_lookup_closure(f)
        ),
        FieldType::Message | FieldType::Group => {
            format!("{}::from_json_value({val}, ignore)?", scalar_type(f))
        }
    }
}

fn json_map_key_encode(ty: FieldType, k: &str) -> String {
    if ty == FieldType::String {
        format!("String::from_utf8_lossy({k}.as_bytes()).into_owned()")
    } else {
        format!("{k}.to_string()")
    }
}

fn json_map_key_decode(ty: FieldType, k: &str) -> String {
    match ty {
        FieldType::String => format!("{k}.as_str()"),
        FieldType::Bool => format!("pbrs::json::map_key_bool({k})?"),
        FieldType::Int32 | FieldType::Sint32 | FieldType::Sfixed32 => {
            format!("pbrs::json::map_key_i32({k})?")
        }
        FieldType::Int64 | FieldType::Sint64 | FieldType::Sfixed64 => {
            format!("pbrs::json::map_key_i64({k})?")
        }
        FieldType::Uint32 | FieldType::Fixed32 => format!("pbrs::json::map_key_u32({k})?"),
        FieldType::Uint64 | FieldType::Fixed64 => format!("pbrs::json::map_key_u64({k})?"),
        _ => format!("{k}.as_str()"),
    }
}

fn typed_present_expr(f: &FieldDescriptor, getter: &str) -> String {
    if is_option(f) {
        return format!("self.has_{}()", field_raw(f));
    }
    match f.field_type {
        FieldType::Bool => getter.to_string(),
        FieldType::Float | FieldType::Double => format!("{getter}.to_bits() != 0"),
        FieldType::String => format!("!{getter}.as_bytes().is_empty()"),
        FieldType::Bytes => format!("!{getter}.is_empty()"),
        FieldType::Enum => format!("i32::from({getter}) != 0"),
        _ => format!("{getter} != 0"),
    }
}

fn text_write_named(f: &FieldDescriptor, name: &str, expr: &str, view: bool) -> String {
    match f.field_type {
        FieldType::Bool => format!("pbrs::text::write_named_bool(out, indent, {name}, {expr})"),
        FieldType::Int32 | FieldType::Sint32 | FieldType::Sfixed32 => {
            format!("pbrs::text::write_named_int32(out, indent, {name}, {expr})")
        }
        FieldType::Int64 | FieldType::Sint64 | FieldType::Sfixed64 => {
            format!("pbrs::text::write_named_int64(out, indent, {name}, {expr})")
        }
        FieldType::Uint32 | FieldType::Fixed32 => {
            format!("pbrs::text::write_named_uint32(out, indent, {name}, {expr})")
        }
        FieldType::Uint64 | FieldType::Fixed64 => {
            format!("pbrs::text::write_named_uint64(out, indent, {name}, {expr})")
        }
        FieldType::Float => format!("pbrs::text::write_named_float(out, indent, {name}, {expr})"),
        FieldType::Double => format!("pbrs::text::write_named_double(out, indent, {name}, {expr})"),
        FieldType::String => {
            format!("pbrs::text::write_named_string(out, indent, {name}, {expr}.as_bytes())")
        }
        FieldType::Bytes if view => {
            format!("pbrs::text::write_named_string(out, indent, {name}, {expr}.as_bytes())")
        }
        FieldType::Bytes => {
            format!("pbrs::text::write_named_string(out, indent, {name}, {expr})")
        }
        FieldType::Enum => {
            let n = if view {
                expr.to_string()
            } else {
                format!("i32::from({expr})")
            };
            format!(
                "{{ let n = {n}; pbrs::text::write_named_enum(out, indent, {name}, {}, n); }}",
                enum_name_from_i32(f, "n")
            )
        }
        _ => format!("pbrs::text::write_named_int32(out, indent, {name}, {expr})"),
    }
}

fn text_write_value(f: &FieldDescriptor, expr: &str, view: bool) -> String {
    match f.field_type {
        FieldType::Bool => format!("out.push_str(if {expr} {{ \"true\" }} else {{ \"false\" }})"),
        FieldType::Float => format!("pbrs::text::write_float_lit(out, {expr})"),
        FieldType::Double => format!("pbrs::text::write_double_lit(out, {expr})"),
        FieldType::String => format!("pbrs::text::write_bytes_lit({expr}.as_bytes(), out)"),
        FieldType::Bytes if view => format!("pbrs::text::write_bytes_lit({expr}.as_bytes(), out)"),
        FieldType::Bytes => format!("pbrs::text::write_bytes_lit({expr}, out)"),
        FieldType::Enum => {
            let n = if view {
                expr.to_string()
            } else {
                format!("i32::from({expr})")
            };
            format!(
                "{{ let n = {n}; pbrs::text::write_enum_lit(out, {}, n); }}",
                enum_name_from_i32(f, "n")
            )
        }
        FieldType::Int32
        | FieldType::Sint32
        | FieldType::Sfixed32
        | FieldType::Int64
        | FieldType::Sint64
        | FieldType::Sfixed64
        | FieldType::Uint32
        | FieldType::Fixed32
        | FieldType::Uint64
        | FieldType::Fixed64 => format!("out.push_str(&{expr}.to_string())"),
        _ => format!("out.push_str(&{expr}.to_string())"),
    }
}

fn text_decode_leaf(f: &FieldDescriptor, val: &str) -> String {
    match f.field_type {
        FieldType::Bool => format!("{val}.as_bool()?"),
        FieldType::Int32 | FieldType::Sint32 | FieldType::Sfixed32 => format!("{val}.as_i32()?"),
        FieldType::Int64 | FieldType::Sint64 | FieldType::Sfixed64 => format!("{val}.as_i64()?"),
        FieldType::Uint32 | FieldType::Fixed32 => format!("{val}.as_u32()?"),
        FieldType::Uint64 | FieldType::Fixed64 => format!("{val}.as_u64()?"),
        FieldType::Float => format!("{val}.as_f32()?"),
        FieldType::Double => format!("{val}.as_f64()?"),
        FieldType::String => format!("{val}.as_str()?"),
        FieldType::Bytes => format!("{val}.as_bytes()?"),
        FieldType::Enum => format!("{val}.as_enum({})?", enum_lookup_closure(f)),
        FieldType::Message | FieldType::Group => {
            format!("{}::from_text_value({val}.as_message()?)?", scalar_type(f))
        }
    }
}

fn text_map_key_write(ty: FieldType, k: &str) -> String {
    match ty {
        FieldType::String => format!("pbrs::text::write_bytes_lit({k}.as_bytes(), out)"),
        FieldType::Bool => format!("out.push_str(if {k} {{ \"true\" }} else {{ \"false\" }})"),
        _ => format!("out.push_str(&{k}.to_string())"),
    }
}

fn text_map_key_default(ty: FieldType) -> &'static str {
    match ty {
        FieldType::String => "\"\"",
        FieldType::Bool => "false",
        FieldType::Int64 | FieldType::Sint64 | FieldType::Sfixed64 => "0i64",
        FieldType::Uint32 | FieldType::Fixed32 => "0u32",
        FieldType::Uint64 | FieldType::Fixed64 => "0u64",
        _ => "0i32",
    }
}

fn text_map_val_default(f: &FieldDescriptor) -> String {
    match f.field_type {
        FieldType::String => "\"\"".into(),
        FieldType::Bytes => "Vec::<u8>::new()".into(),
        FieldType::Bool => "false".into(),
        FieldType::Float => "0.0f32".into(),
        FieldType::Double => "0.0f64".into(),
        FieldType::Int64 | FieldType::Sint64 | FieldType::Sfixed64 => "0i64".into(),
        FieldType::Uint32 | FieldType::Fixed32 => "0u32".into(),
        FieldType::Uint64 | FieldType::Fixed64 => "0u64".into(),
        FieldType::Message | FieldType::Group => format!("{}::new()", scalar_type(f)),
        _ => "0i32".into(),
    }
}

fn map_value_field(f: &FieldDescriptor) -> Option<&FieldDescriptor> {
    f.message.as_ref().and_then(|e| e.field(2))
}

fn can_typed_json(desc: &MessageDescriptor) -> bool {
    desc.fields
        .values()
        .all(|f| field_supports_typed_json(f, 0))
}

/// Reject a second JSON member of the same real oneof (official proto3 JSON).
fn emit_json_oneof_guard(src: &mut String, desc: &MessageDescriptor, f: &FieldDescriptor) {
    if !is_real_oneof(desc, f) {
        return;
    }
    let Some(idx) = f.oneof_index else {
        return;
    };
    let Some(members) = desc.oneofs.get(idx as usize) else {
        return;
    };
    for n in members {
        if *n == f.number {
            continue;
        }
        let Some(sib) = desc.field(*n) else {
            continue;
        };
        let raw = field_raw(sib);
        let _ = writeln!(
            src,
            "                    if msg.has_{raw}() {{ return Err(ParseError::new(\"duplicate oneof member\")); }}"
        );
    }
}

fn emit_json_text(src: &mut String, desc: &MessageDescriptor) {
    match desc.full_name.trim_start_matches('.') {
        "google.protobuf.Timestamp" => {
            emit_wkt_string_json(src, "timestamp");
            emit_typed_text(src, desc);
        }
        "google.protobuf.Duration" => {
            emit_wkt_string_json(src, "duration");
            emit_typed_text(src, desc);
        }
        "google.protobuf.Empty" => {
            emit_wkt_empty_json(src);
            emit_typed_text(src, desc);
        }
        name if wrapper_json_spec(name).is_some() => {
            let (encode, decode, value_expr) = wrapper_json_spec(name).expect("wrapper");
            emit_wkt_wrapper_json(src, encode, decode, value_expr);
            emit_typed_text(src, desc);
        }
        name if name.starts_with("google.protobuf.") => {
            // Struct / Value / ListValue / Any / FieldMask keep official
            // JSON via DynamicMessage. Field-wise object JSON for those
            // would disagree with the official mapping.
            emit_dynamic_json(src, &desc.full_name);
            emit_dynamic_text(src, &desc.full_name);
        }
        _ if can_typed_json(desc) => {
            emit_typed_json(src, desc);
            emit_typed_text(src, desc);
        }
        _ => {
            emit_dynamic_json(src, &desc.full_name);
            emit_dynamic_text(src, &desc.full_name);
        }
    }
}

/// Official proto3 JSON string mapping for Timestamp / Duration.
fn emit_wkt_string_json(src: &mut String, helper: &str) {
    let _ = writeln!(
        src,
        "    pub fn to_json(&self) -> Result<String, SerializeError> {{"
    );
    let _ = writeln!(src, "        Ok(self.to_json_value()?.to_string())");
    let _ = writeln!(src, "    }}");
    let _ = writeln!(
        src,
        "    pub fn from_json(json: &str) -> Result<Self, ParseError> {{ Self::from_json_ignore(json, false) }}"
    );
    let _ = writeln!(
        src,
        "    pub fn from_json_ignore(json: &str, ignore: bool) -> Result<Self, ParseError> {{"
    );
    let _ = writeln!(src, "        let v = pbrs::json::parse(json)?;");
    let _ = writeln!(src, "        Self::from_json_value(&v, ignore)");
    let _ = writeln!(src, "    }}");
    let _ = writeln!(
        src,
        "    fn to_json_value(&self) -> Result<pbrs::json::Json, SerializeError> {{"
    );
    let _ = writeln!(
        src,
        "        pbrs::json::{helper}(self.seconds(), self.nanos())"
    );
    let _ = writeln!(src, "    }}");
    let _ = writeln!(
        src,
        "    fn from_json_value(v: &pbrs::json::Json, _ignore: bool) -> Result<Self, ParseError> {{"
    );
    let _ = writeln!(
        src,
        "        let (seconds, nanos) = pbrs::json::as_{helper}(v)?;"
    );
    let _ = writeln!(src, "        let mut msg = Self::new();");
    let _ = writeln!(src, "        msg.set_seconds(seconds);");
    let _ = writeln!(src, "        msg.set_nanos(nanos);");
    let _ = writeln!(src, "        Ok(msg)");
    let _ = writeln!(src, "    }}");
}

/// Official proto3 JSON for Empty (`{}`).
fn emit_wkt_empty_json(src: &mut String) {
    let _ = writeln!(
        src,
        "    pub fn to_json(&self) -> Result<String, SerializeError> {{"
    );
    let _ = writeln!(src, "        Ok(self.to_json_value()?.to_string())");
    let _ = writeln!(src, "    }}");
    let _ = writeln!(
        src,
        "    pub fn from_json(json: &str) -> Result<Self, ParseError> {{ Self::from_json_ignore(json, false) }}"
    );
    let _ = writeln!(
        src,
        "    pub fn from_json_ignore(json: &str, ignore: bool) -> Result<Self, ParseError> {{"
    );
    let _ = writeln!(src, "        let v = pbrs::json::parse(json)?;");
    let _ = writeln!(src, "        Self::from_json_value(&v, ignore)");
    let _ = writeln!(src, "    }}");
    let _ = writeln!(
        src,
        "    fn to_json_value(&self) -> Result<pbrs::json::Json, SerializeError> {{"
    );
    let _ = writeln!(src, "        Ok(pbrs::json::empty())");
    let _ = writeln!(src, "    }}");
    let _ = writeln!(
        src,
        "    fn from_json_value(v: &pbrs::json::Json, ignore: bool) -> Result<Self, ParseError> {{"
    );
    let _ = writeln!(src, "        pbrs::json::as_empty(v, ignore)?;");
    let _ = writeln!(src, "        Ok(Self::new())");
    let _ = writeln!(src, "    }}");
}

/// Official proto3 JSON for a wrapper: the wrapped value, not an object.
fn emit_wkt_wrapper_json(src: &mut String, encode: &str, decode: &str, value_expr: &str) {
    let _ = writeln!(
        src,
        "    pub fn to_json(&self) -> Result<String, SerializeError> {{"
    );
    let _ = writeln!(src, "        Ok(self.to_json_value()?.to_string())");
    let _ = writeln!(src, "    }}");
    let _ = writeln!(
        src,
        "    pub fn from_json(json: &str) -> Result<Self, ParseError> {{ Self::from_json_ignore(json, false) }}"
    );
    let _ = writeln!(
        src,
        "    pub fn from_json_ignore(json: &str, ignore: bool) -> Result<Self, ParseError> {{"
    );
    let _ = writeln!(src, "        let v = pbrs::json::parse(json)?;");
    let _ = writeln!(src, "        Self::from_json_value(&v, ignore)");
    let _ = writeln!(src, "    }}");
    let _ = writeln!(
        src,
        "    fn to_json_value(&self) -> Result<pbrs::json::Json, SerializeError> {{"
    );
    let _ = writeln!(src, "        Ok(pbrs::json::{encode}({value_expr}))");
    let _ = writeln!(src, "    }}");
    let _ = writeln!(
        src,
        "    fn from_json_value(v: &pbrs::json::Json, _ignore: bool) -> Result<Self, ParseError> {{"
    );
    let _ = writeln!(src, "        let mut msg = Self::new();");
    let _ = writeln!(src, "        msg.set_value(pbrs::json::{decode}(v)?);");
    let _ = writeln!(src, "        Ok(msg)");
    let _ = writeln!(src, "    }}");
}

fn emit_typed_json(src: &mut String, desc: &MessageDescriptor) {
    let _ = writeln!(
        src,
        "    pub fn to_json(&self) -> Result<String, SerializeError> {{"
    );
    let _ = writeln!(src, "        Ok(self.to_json_value()?.to_string())");
    let _ = writeln!(src, "    }}");
    let _ = writeln!(
        src,
        "    pub fn from_json(json: &str) -> Result<Self, ParseError> {{ Self::from_json_ignore(json, false) }}"
    );
    let _ = writeln!(
        src,
        "    pub fn from_json_ignore(json: &str, ignore: bool) -> Result<Self, ParseError> {{"
    );
    let _ = writeln!(src, "        let v = pbrs::json::parse(json)?;");
    let _ = writeln!(src, "        Self::from_json_value(&v, ignore)");
    let _ = writeln!(src, "    }}");
    emit_to_json_value(src, desc);
    emit_from_json_value(src, desc);
}

fn emit_to_json_value(src: &mut String, desc: &MessageDescriptor) {
    let _ = writeln!(
        src,
        "    fn to_json_value(&self) -> Result<pbrs::json::Json, SerializeError> {{"
    );
    let _ = writeln!(src, "        let mut map = pbrs::json::JsonMap::new();");
    for f in desc.fields.values() {
        let id = field_id(f);
        let m = field_raw(f);
        let key = rust_str(&f.json_name);
        let getter = format!("self.{id}()");
        if f.is_map {
            let kt = map_key_ty(f);
            let vf = map_value_field(f);
            let val = vf
                .map(|v| json_encode_leaf(v, "v", true))
                .unwrap_or_else(|| json_encode_leaf(f, "v", true));
            let _ = writeln!(src, "        if !self.{id}().is_empty() {{");
            let _ = writeln!(src, "            let mut obj = pbrs::json::JsonMap::new();");
            let _ = writeln!(src, "            for (k, v) in self.{id}() {{");
            let _ = writeln!(
                src,
                "                let _ = obj.insert({}, {val});",
                json_map_key_encode(kt, "k")
            );
            let _ = writeln!(src, "            }}");
            let _ = writeln!(
                src,
                "            let _ = map.insert({key}.into(), pbrs::json::Json::Object(obj));"
            );
            let _ = writeln!(src, "        }}");
        } else if f.cardinality == Cardinality::Repeated {
            let item = json_encode_leaf(f, "item", true);
            let _ = writeln!(src, "        if !self.{id}().is_empty() {{");
            let _ = writeln!(src, "            let mut arr = Vec::new();");
            let _ = writeln!(src, "            for item in self.{id}() {{");
            let _ = writeln!(src, "                arr.push({item});");
            let _ = writeln!(src, "            }}");
            let _ = writeln!(
                src,
                "            let _ = map.insert({key}.into(), pbrs::json::Json::Array(arr));"
            );
            let _ = writeln!(src, "        }}");
        } else if f.field_type == FieldType::Message {
            let _ = writeln!(src, "        if self.has_{m}() {{");
            let _ = writeln!(
                src,
                "            let _ = map.insert({key}.into(), {});",
                json_encode_leaf(f, &getter, false)
            );
            let _ = writeln!(src, "        }}");
        } else {
            let _ = writeln!(src, "        if {} {{", typed_present_expr(f, &getter));
            let _ = writeln!(
                src,
                "            let _ = map.insert({key}.into(), {});",
                json_encode_leaf(f, &getter, false)
            );
            let _ = writeln!(src, "        }}");
        }
    }
    let _ = writeln!(src, "        Ok(pbrs::json::Json::Object(map))");
    let _ = writeln!(src, "    }}");
}

fn emit_from_json_value(src: &mut String, desc: &MessageDescriptor) {
    let _ = writeln!(
        src,
        "    fn from_json_value(v: &pbrs::json::Json, ignore: bool) -> Result<Self, ParseError> {{"
    );
    let _ = writeln!(
        src,
        "        let obj = v.as_object().ok_or_else(|| ParseError::new(\"json message must be an object\"))?;"
    );
    let _ = writeln!(src, "        let mut msg = Self::new();");
    let _ = writeln!(
        src,
        "        let mut seen = std::collections::BTreeSet::<u32>::new();"
    );
    let _ = writeln!(src, "        for (key, val) in obj {{");
    let _ = writeln!(src, "            match key.as_str() {{");
    for f in desc.fields.values() {
        let id = field_id(f);
        let m = field_raw(f);
        let keys = json_match_keys(f);
        let num = f.number;
        let _ = writeln!(src, "                {keys} => {{");
        let _ = writeln!(
            src,
            "                    if !seen.insert({num}u32) {{ return Err(ParseError::owned(format!(\"duplicate json field {{key}}\"))); }}"
        );
        let _ = writeln!(src, "                    if val.is_null() {{ continue; }}");
        emit_json_oneof_guard(src, desc, f);
        if f.is_map {
            let kt = map_key_ty(f);
            let vf = map_value_field(f);
            let val_ty = vf.map(|v| v.field_type);
            let _ = writeln!(
                src,
                "                    let obj = val.as_object().ok_or_else(|| ParseError::new(\"json map must be an object\"))?;"
            );
            let _ = writeln!(src, "                    for (k, v) in obj {{");
            if val_ty == Some(FieldType::Enum) {
                let Some(vf) = vf else { continue };
                let _ = writeln!(
                    src,
                    "                        if let Some(val) = {} {{",
                    json_decode_leaf(vf, "v")
                );
                let _ = writeln!(
                    src,
                    "                            msg.{id}_mut().insert({}, val);",
                    json_map_key_decode(kt, "k")
                );
                let _ = writeln!(src, "                        }}");
            } else {
                let decode = vf
                    .map(|v| json_decode_leaf(v, "v"))
                    .unwrap_or_else(|| "pbrs::json::as_i32(v)?".into());
                let _ = writeln!(
                    src,
                    "                        msg.{id}_mut().insert({}, {decode});",
                    json_map_key_decode(kt, "k")
                );
            }
            let _ = writeln!(src, "                    }}");
        } else if f.cardinality == Cardinality::Repeated {
            let _ = writeln!(
                src,
                "                    let arr = val.as_array().ok_or_else(|| ParseError::new(\"json repeated must be an array\"))?;"
            );
            let _ = writeln!(src, "                    for item in arr {{");
            if f.field_type == FieldType::Enum {
                let _ = writeln!(
                    src,
                    "                        if let Some(item) = {} {{",
                    json_decode_leaf(f, "item")
                );
                let _ = writeln!(
                    src,
                    "                            msg.{id}_mut().push(item);"
                );
                let _ = writeln!(src, "                        }}");
            } else {
                let _ = writeln!(
                    src,
                    "                        msg.{id}_mut().push({});",
                    json_decode_leaf(f, "item")
                );
            }
            let _ = writeln!(src, "                    }}");
        } else if f.field_type == FieldType::Enum {
            let _ = writeln!(
                src,
                "                    if let Some(n) = {} {{",
                json_decode_leaf(f, "val")
            );
            let _ = writeln!(src, "                        msg.set_{m}(n);");
            let _ = writeln!(src, "                    }}");
        } else {
            let _ = writeln!(
                src,
                "                    msg.set_{m}({});",
                json_decode_leaf(f, "val")
            );
        }
        let _ = writeln!(src, "                }}");
    }
    let _ = writeln!(src, "                _ => {{");
    let _ = writeln!(
        src,
        "                    if !ignore {{ return Err(ParseError::owned(format!(\"unknown json field {{key}}\"))); }}"
    );
    let _ = writeln!(src, "                }}");
    let _ = writeln!(src, "            }}");
    let _ = writeln!(src, "        }}");
    let _ = writeln!(src, "        Ok(msg)");
    let _ = writeln!(src, "    }}");
}

fn emit_typed_text(src: &mut String, desc: &MessageDescriptor) {
    let _ = writeln!(
        src,
        "    pub fn to_text(&self) -> Result<String, SerializeError> {{"
    );
    let _ = writeln!(src, "        let mut out = String::new();");
    let _ = writeln!(src, "        self.write_text(&mut out, 0)?;");
    let _ = writeln!(src, "        Ok(out)");
    let _ = writeln!(src, "    }}");
    let _ = writeln!(
        src,
        "    pub fn to_text_with_unknown(&self) -> Result<String, SerializeError> {{"
    );
    let _ = writeln!(src, "        let mut out = String::new();");
    let _ = writeln!(src, "        self.write_text(&mut out, 0)?;");
    let _ = writeln!(
        src,
        "        pbrs::text::write_unknown_fields(&self.unknown, &mut out, 0);"
    );
    let _ = writeln!(src, "        Ok(out)");
    let _ = writeln!(src, "    }}");
    let _ = writeln!(
        src,
        "    pub fn from_text(text: &str) -> Result<Self, ParseError> {{"
    );
    let _ = writeln!(
        src,
        "        Self::from_text_value(&pbrs::text::parse(text)?)"
    );
    let _ = writeln!(src, "    }}");
    emit_write_text(src, desc);
    emit_from_text_value(src, desc);
}

fn emit_write_text(src: &mut String, desc: &MessageDescriptor) {
    let _ = writeln!(
        src,
        "    fn write_text(&self, out: &mut String, indent: usize) -> Result<(), SerializeError> {{"
    );
    for f in desc.fields.values() {
        let id = field_id(f);
        let m = field_raw(f);
        let name = rust_str(&f.name);
        let getter = format!("self.{id}()");
        if f.is_map {
            let kt = map_key_ty(f);
            let vf = map_value_field(f);
            let value_is_msg = vf.is_some_and(|v| v.field_type == FieldType::Message);
            let sort = if kt == FieldType::String {
                "items.sort_by(|(a, _), (b, _)| a.as_bytes().cmp(b.as_bytes()))"
            } else {
                "items.sort_by(|(a, _), (b, _)| a.cmp(b))"
            };
            let _ = writeln!(src, "        if !self.{id}().is_empty() {{");
            let _ = writeln!(
                src,
                "            let mut items: Vec<_> = self.{id}().iter().collect();"
            );
            let _ = writeln!(src, "            {sort};");
            let _ = writeln!(src, "            for (k, v) in items {{");
            let key_write = text_map_key_write(kt, "k");
            if value_is_msg {
                let _ = writeln!(
                    src,
                    "                pbrs::text::write_map_entry(out, indent, {name}, |out| {{ {key_write}; }}, |out| {{"
                );
                let _ = writeln!(src, "                    out.push_str(\"{{\\n\");");
                let _ = writeln!(src, "                    v.write_text(out, indent + 4)?;");
                let _ = writeln!(src, "                    pbrs::text::pad(out, indent + 2);");
                let _ = writeln!(src, "                    out.push_str(\"}}\\n\");");
                let _ = writeln!(src, "                    Ok(())");
                let _ = writeln!(src, "                }}, true)?;");
            } else {
                let val_write = vf
                    .map(|v| text_write_value(v, "v", true))
                    .unwrap_or_else(|| "out.push_str(&v.to_string())".into());
                let _ = writeln!(
                    src,
                    "                pbrs::text::write_map_entry(out, indent, {name}, |out| {{ {key_write}; }}, |out| {{ {val_write}; Ok(()) }}, false)?;"
                );
            }
            let _ = writeln!(src, "            }}");
            let _ = writeln!(src, "        }}");
        } else if f.cardinality == Cardinality::Repeated {
            if f.field_type == FieldType::Message {
                let _ = writeln!(src, "        for item in self.{id}() {{");
                let _ = writeln!(src, "            pbrs::text::pad(out, indent);");
                let _ = writeln!(src, "            out.push_str({name});");
                let _ = writeln!(src, "            out.push_str(\" {{\\n\");");
                let _ = writeln!(src, "            item.write_text(out, indent + 2)?;");
                let _ = writeln!(src, "            pbrs::text::pad(out, indent);");
                let _ = writeln!(src, "            out.push_str(\"}}\\n\");");
                let _ = writeln!(src, "        }}");
            } else {
                let _ = writeln!(src, "        for item in self.{id}() {{");
                let _ = writeln!(
                    src,
                    "            {};",
                    text_write_named(f, &name, "item", true)
                );
                let _ = writeln!(src, "        }}");
            }
        } else if f.field_type == FieldType::Message {
            let _ = writeln!(src, "        if self.has_{m}() {{");
            let _ = writeln!(src, "            pbrs::text::pad(out, indent);");
            let _ = writeln!(src, "            out.push_str({name});");
            let _ = writeln!(src, "            out.push_str(\" {{\\n\");");
            let _ = writeln!(src, "            self.{id}().write_text(out, indent + 2)?;");
            let _ = writeln!(src, "            pbrs::text::pad(out, indent);");
            let _ = writeln!(src, "            out.push_str(\"}}\\n\");");
            let _ = writeln!(src, "        }}");
        } else {
            let _ = writeln!(src, "        if {} {{", typed_present_expr(f, &getter));
            let _ = writeln!(
                src,
                "            {};",
                text_write_named(f, &name, &getter, false)
            );
            let _ = writeln!(src, "        }}");
        }
    }
    let _ = writeln!(src, "        Ok(())");
    let _ = writeln!(src, "    }}");
}

fn emit_from_text_value(src: &mut String, desc: &MessageDescriptor) {
    let _ = writeln!(
        src,
        "    fn from_text_value(fields: &[(String, pbrs::text::TextValue)]) -> Result<Self, ParseError> {{"
    );
    let _ = writeln!(src, "        let mut msg = Self::new();");
    let _ = writeln!(src, "        for (key, val) in fields {{");
    let _ = writeln!(src, "            match key.as_str() {{");
    for f in desc.fields.values() {
        let id = field_id(f);
        let m = field_raw(f);
        let name = rust_str(&f.name);
        let _ = writeln!(src, "                {name} => {{");
        if f.is_map {
            let kt = map_key_ty(f);
            let vf = map_value_field(f);
            let key_decode = match kt {
                FieldType::String => "ev.as_str()?".to_string(),
                FieldType::Bool => "ev.as_bool()?".to_string(),
                FieldType::Int64 | FieldType::Sint64 | FieldType::Sfixed64 => {
                    "ev.as_i64()?".to_string()
                }
                FieldType::Uint32 | FieldType::Fixed32 => "ev.as_u32()?".to_string(),
                FieldType::Uint64 | FieldType::Fixed64 => "ev.as_u64()?".to_string(),
                _ => "ev.as_i32()?".to_string(),
            };
            let val_decode = match vf {
                Some(v) if v.field_type == FieldType::Bytes => "ev.as_bytes()?.to_vec()".into(),
                Some(v) => text_decode_leaf(v, "ev"),
                None => "ev.as_i32()?".into(),
            };
            let _ = writeln!(src, "                    let entry = val.as_message()?;");
            let _ = writeln!(
                src,
                "                    let mut k = {};",
                text_map_key_default(kt)
            );
            let _ = writeln!(
                src,
                "                    let mut v = {};",
                vf.map(text_map_val_default)
                    .unwrap_or_else(|| "0i32".into())
            );
            let _ = writeln!(src, "                    for (ek, ev) in entry {{");
            let _ = writeln!(src, "                        match ek.as_str() {{");
            let _ = writeln!(
                src,
                "                            \"key\" => k = {key_decode},"
            );
            let _ = writeln!(
                src,
                "                            \"value\" => v = {val_decode},"
            );
            let _ = writeln!(
                src,
                "                            _ => return Err(ParseError::owned(format!(\"unknown field {{ek}}\"))),"
            );
            let _ = writeln!(src, "                        }}");
            let _ = writeln!(src, "                    }}");
            let _ = writeln!(src, "                    msg.{id}_mut().insert(k, v);");
        } else if f.cardinality == Cardinality::Repeated {
            let item = text_decode_leaf(f, "item");
            let one = text_decode_leaf(f, "val");
            let _ = writeln!(
                src,
                "                    if let Some(items) = val.as_list() {{"
            );
            let _ = writeln!(src, "                        for item in items {{");
            let _ = writeln!(
                src,
                "                            msg.{id}_mut().push({item});"
            );
            let _ = writeln!(src, "                        }}");
            let _ = writeln!(src, "                    }} else {{");
            let _ = writeln!(src, "                        msg.{id}_mut().push({one});");
            let _ = writeln!(src, "                    }}");
        } else if f.field_type == FieldType::Message {
            let ty = scalar_type(f);
            let _ = writeln!(
                src,
                "                    msg.{id}_mut().merge_from({ty}::from_text_value(val.as_message()?)?);"
            );
        } else {
            let _ = writeln!(
                src,
                "                    msg.set_{m}({});",
                text_decode_leaf(f, "val")
            );
        }
        let _ = writeln!(src, "                }}");
    }
    let _ = writeln!(
        src,
        "                _ => return Err(ParseError::owned(format!(\"unknown field {{key}}\"))),"
    );
    let _ = writeln!(src, "            }}");
    let _ = writeln!(src, "        }}");
    let _ = writeln!(src, "        Ok(msg)");
    let _ = writeln!(src, "    }}");
}

fn emit_dynamic_json(src: &mut String, full_name: &str) {
    let _ = writeln!(
        src,
        "    pub fn to_json(&self) -> Result<String, SerializeError> {{"
    );
    let _ = writeln!(src, "        let b = pbrs::Serialize::serialize(self).map_err(|_| SerializeError::new(\"json\"))?;");
    let _ = writeln!(src, "        let pool = generated_pool();");
    let _ = writeln!(
        src,
        "        let desc = pool.get_message(\"{full_name}\").ok_or_else(|| SerializeError::new(\"missing desc\"))?;"
    );
    let _ = writeln!(src, "        pbrs::DynamicMessage::parse_with_pool(desc, Some(pool), &b).map_err(|_| SerializeError::new(\"json\"))?.to_json()");
    let _ = writeln!(src, "    }}");
    let _ = writeln!(
        src,
        "    pub fn from_json(json: &str) -> Result<Self, ParseError> {{ Self::from_json_ignore(json, false) }}"
    );
    let _ = writeln!(
        src,
        "    pub fn from_json_ignore(json: &str, ignore: bool) -> Result<Self, ParseError> {{"
    );
    let _ = writeln!(src, "        let pool = generated_pool();");
    let _ = writeln!(
        src,
        "        let desc = pool.get_message(\"{full_name}\").ok_or_else(|| ParseError::owned(\"missing desc\".into()))?;"
    );
    let _ = writeln!(src, "        let d = pbrs::DynamicMessage::from_json_with_pool(desc, Some(pool), json, ignore)?;");
    let _ = writeln!(src, "        let b = pbrs::Serialize::serialize(&d).map_err(|e| ParseError::owned(e.to_string()))?;");
    let _ = writeln!(src, "        <Self as pbrs::Parse>::parse(&b)");
    let _ = writeln!(src, "    }}");
}

fn emit_dynamic_text(src: &mut String, full_name: &str) {
    let _ = writeln!(
        src,
        "    pub fn to_text(&self) -> Result<String, SerializeError> {{"
    );
    let _ = writeln!(src, "        let b = pbrs::Serialize::serialize(self).map_err(|_| SerializeError::new(\"text\"))?;");
    let _ = writeln!(src, "        let pool = generated_pool();");
    let _ = writeln!(
        src,
        "        let desc = pool.get_message(\"{full_name}\").ok_or_else(|| SerializeError::new(\"missing desc\"))?;"
    );
    let _ = writeln!(src, "        pbrs::DynamicMessage::parse_with_pool(desc, Some(pool), &b).map_err(|_| SerializeError::new(\"text\"))?.to_text()");
    let _ = writeln!(src, "    }}");
    let _ = writeln!(
        src,
        "    pub fn to_text_with_unknown(&self) -> Result<String, SerializeError> {{"
    );
    let _ = writeln!(src, "        let b = pbrs::Serialize::serialize(self).map_err(|_| SerializeError::new(\"text\"))?;");
    let _ = writeln!(src, "        let pool = generated_pool();");
    let _ = writeln!(
        src,
        "        let desc = pool.get_message(\"{full_name}\").ok_or_else(|| SerializeError::new(\"missing desc\"))?;"
    );
    let _ = writeln!(src, "        pbrs::DynamicMessage::parse_with_pool(desc, Some(pool), &b).map_err(|_| SerializeError::new(\"text\"))?.to_text_with_unknown()");
    let _ = writeln!(src, "    }}");
    let _ = writeln!(
        src,
        "    pub fn from_text(text: &str) -> Result<Self, ParseError> {{"
    );
    let _ = writeln!(src, "        let pool = generated_pool();");
    let _ = writeln!(
        src,
        "        let desc = pool.get_message(\"{full_name}\").ok_or_else(|| ParseError::owned(\"missing desc\".into()))?;"
    );
    let _ = writeln!(
        src,
        "        let d = pbrs::DynamicMessage::from_text_with_pool(desc, Some(pool), text)?;"
    );
    let _ = writeln!(src, "        let b = pbrs::Serialize::serialize(&d).map_err(|e| ParseError::owned(e.to_string()))?;");
    let _ = writeln!(src, "        <Self as pbrs::Parse>::parse(&b)");
    let _ = writeln!(src, "    }}");
}

fn wire_const(ty: FieldType) -> &'static str {
    match ty {
        FieldType::Fixed64 | FieldType::Sfixed64 | FieldType::Double => "pbrs::rt::WIRE_I64",
        FieldType::Fixed32 | FieldType::Sfixed32 | FieldType::Float => "pbrs::rt::WIRE_I32",
        FieldType::String | FieldType::Bytes | FieldType::Message | FieldType::Group => {
            "pbrs::rt::WIRE_LEN"
        }
        _ => "pbrs::rt::WIRE_VARINT",
    }
}

fn implicit_present(path: &str, ty: FieldType) -> String {
    match ty {
        FieldType::Bool => path.to_string(),
        FieldType::Float | FieldType::Double => format!("{path}.to_bits() != 0"),
        FieldType::String | FieldType::Bytes => format!("!{path}.is_empty()"),
        _ => format!("{path} != 0"),
    }
}

fn map_key_ty(field: &FieldDescriptor) -> FieldType {
    field
        .message
        .as_ref()
        .and_then(|e| e.field(1))
        .map(|f| f.field_type)
        .unwrap_or(FieldType::String)
}

fn map_val_ty(field: &FieldDescriptor) -> FieldType {
    field
        .message
        .as_ref()
        .and_then(|e| e.field(2))
        .map(|f| f.field_type)
        .unwrap_or(FieldType::Int32)
}

fn emit_codec(src: &mut String, desc: &MessageDescriptor) {
    let required: Vec<_> = desc
        .fields
        .values()
        .filter(|f| f.cardinality == Cardinality::Required)
        .collect();
    let _ = writeln!(
        src,
        "    #[inline(always)] fn check_required(&self) -> Result<(), ParseError> {{"
    );
    for f in &required {
        let id = field_id(f);
        if is_option(f) {
            let _ = writeln!(
                src,
                "        if self.{id}.is_none() {{ return Err(ParseError::new(\"missing required field\")); }}"
            );
        }
    }
    let _ = writeln!(src, "        Ok(())");
    let _ = writeln!(src, "    }}");
    let _ = writeln!(
        src,
        "    fn merge_bytes(&mut self, data: &[u8], depth: u32) -> Result<(), ParseError> {{ if data.is_empty() {{ return self.check_required(); }} let mut pos = 0; let mut wire = None; self.merge_inner(data, &mut wire, &mut pos, depth, true, None) }}"
    );
    let _ = writeln!(
        src,
        "    fn merge_bytes_dont_enforce(&mut self, data: &[u8], depth: u32) -> Result<(), ParseError> {{ if data.is_empty() {{ return Ok(()); }} let mut pos = 0; let mut wire = None; self.merge_inner(data, &mut wire, &mut pos, depth, false, None) }}"
    );
    let _ = writeln!(
        src,
        "    fn merge_group(&mut self, data: &[u8], wire: &mut Option<pbrs::rt::Wire>, pos: &mut usize, num: u32, depth: u32) -> Result<(), ParseError> {{ self.merge_inner(data, wire, pos, depth, false, Some(num)) }}"
    );
    let _ = writeln!(
        src,
        "    fn merge_inner(&mut self, data: &[u8], wire: &mut Option<pbrs::rt::Wire>, pos: &mut usize, depth: u32, enforce: bool, until: Option<u32>) -> Result<(), ParseError> {{"
    );
    let _ = writeln!(
        src,
        "        if depth > pbrs::RECURSION_LIMIT {{ return Err(ParseError::new(\"recursion limit exceeded\")); }} let _ = enforce;"
    );
    let _ = writeln!(src, "        self.cached_size.dirty();");
    let lights: Vec<_> = desc
        .fields
        .values()
        .filter(|f| is_light_merge_field(f))
        .collect();
    let heavies: Vec<_> = desc
        .fields
        .values()
        .filter(|f| !is_light_merge_field(f))
        .collect();
    let split_heavy =
        !desc.message_set_wire_format && (1..=4).contains(&lights.len()) && !heavies.is_empty();
    let _ = writeln!(src, "        while *pos < data.len() {{");
    let _ = writeln!(
        src,
        "            let (n, w) = pbrs::rt::decode_tag(data, pos)?;"
    );
    let _ = writeln!(src, "            if let Some(g) = until {{ if w == pbrs::rt::WIRE_EGROUP {{ if n != g {{ return Err(ParseError::new(\"mismatched end-group\")); }} return Ok(()); }} }}");
    if split_heavy {
        for f in &lights {
            let _ = writeln!(src, "            if n == {} {{", f.number);
            let _ = writeln!(src, "                match w {{");
            emit_merge_arm(src, desc, f);
            let _ = writeln!(
                src,
                "                    _ => self.unknown.fields.push(pbrs::rt::capture_unknown(data, pos, n, w)?),"
            );
            let _ = writeln!(src, "                }}");
            let _ = writeln!(src, "                continue;");
            let _ = writeln!(src, "            }}");
        }
        let _ = writeln!(
            src,
            "            self.merge_heavy(n, w, data, wire, pos, depth)?;"
        );
    } else {
        let _ = writeln!(src, "            match n {{");
        if desc.message_set_wire_format {
            emit_message_set_merge(src, desc);
        }
        for f in desc.fields.values() {
            if desc.message_set_wire_format && f.number == 1 {
                continue;
            }
            let _ = writeln!(src, "            {} => match w {{", f.number);
            emit_merge_arm(src, desc, f);
            let _ = writeln!(
                src,
                "                _ => self.unknown.fields.push(pbrs::rt::capture_unknown(data, pos, n, w)?),"
            );
            let _ = writeln!(src, "            }}");
        }
        let _ = writeln!(
            src,
            "                _ => self.unknown.fields.push(pbrs::rt::capture_unknown(data, pos, n, w)?),"
        );
        let _ = writeln!(src, "            }}");
    }
    let _ = writeln!(src, "        }}");
    let _ = writeln!(
        src,
        "        if until.is_some() {{ return Err(ParseError::new(\"truncated group\")); }}"
    );
    let _ = writeln!(src, "        if enforce {{ self.check_required()?; }}");
    let _ = writeln!(src, "        Ok(())");
    let _ = writeln!(src, "    }}");
    if split_heavy {
        let _ = writeln!(src, "    #[inline(never)]");
        let _ = writeln!(
            src,
            "    fn merge_heavy(&mut self, n: u32, w: u32, data: &[u8], wire: &mut Option<pbrs::rt::Wire>, pos: &mut usize, depth: u32) -> Result<(), ParseError> {{"
        );
        let _ = writeln!(src, "        match n {{");
        for f in &heavies {
            let _ = writeln!(src, "            {} => match w {{", f.number);
            emit_merge_arm(src, desc, f);
            let _ = writeln!(
                src,
                "                _ => self.unknown.fields.push(pbrs::rt::capture_unknown(data, pos, n, w)?),"
            );
            let _ = writeln!(src, "            }}");
        }
        let _ = writeln!(
            src,
            "            _ => self.unknown.fields.push(pbrs::rt::capture_unknown(data, pos, n, w)?),"
        );
        let _ = writeln!(src, "        }}");
        let _ = writeln!(src, "        Ok(())");
        let _ = writeln!(src, "    }}");
    }

    let _ = writeln!(
        src,
        "    fn validate_inner(wire: &pbrs::rt::Wire, pos: &mut usize, depth: u32) -> Result<(), ParseError> {{ Self::validate_until(wire, pos, depth, None) }}"
    );
    let _ = writeln!(
        src,
        "    fn validate_until(wire: &pbrs::rt::Wire, pos: &mut usize, depth: u32, until: Option<u32>) -> Result<(), ParseError> {{"
    );
    let _ = writeln!(
        src,
        "        if depth > pbrs::RECURSION_LIMIT {{ return Err(ParseError::new(\"recursion limit exceeded\")); }}"
    );
    let _ = writeln!(src, "        let data = wire.as_slice();");
    if !required.is_empty() {
        let _ = writeln!(src, "        let mut seen = 0u64;");
    }
    let _ = writeln!(src, "        while *pos < data.len() {{");
    let _ = writeln!(
        src,
        "            let (n, w) = pbrs::rt::decode_tag(data, pos)?;"
    );
    let _ = writeln!(src, "            if let Some(g) = until {{ if w == pbrs::rt::WIRE_EGROUP {{ if n != g {{ return Err(ParseError::new(\"mismatched end-group\")); }} return Ok(()); }} }}");
    let _ = writeln!(src, "            match n {{");
    for f in desc.fields.values() {
        let bit = required.iter().position(|r| r.number == f.number);
        let _ = writeln!(src, "            {} => match w {{", f.number);
        emit_validate_arm(src, f, bit);
        let _ = writeln!(
            src,
            "                _ => pbrs::rt::skip_field(data, pos, w)?,"
        );
        let _ = writeln!(src, "            }}");
    }
    let _ = writeln!(
        src,
        "                _ => pbrs::rt::skip_field(data, pos, w)?,"
    );
    let _ = writeln!(src, "            }}");
    let _ = writeln!(src, "        }}");
    let _ = writeln!(
        src,
        "        if until.is_some() {{ return Err(ParseError::new(\"truncated group\")); }}"
    );
    if !required.is_empty() {
        let all = (1u64 << required.len()) - 1;
        let _ = writeln!(
            src,
            "        if seen != {all} {{ return Err(ParseError::new(\"missing required field\")); }}"
        );
    }
    let _ = writeln!(src, "        Ok(())");
    let _ = writeln!(src, "    }}");

    let _ = writeln!(src, "    fn compute_size(&self) -> u64 {{");
    let _ = writeln!(
        src,
        "        if let Some(n) = self.cached_size.get() {{ return n; }}"
    );
    let _ = writeln!(src, "        let mut n = self.unknown.encoded_len();");
    if desc.message_set_wire_format {
        emit_message_set_size(src, desc);
    } else {
        for f in desc.fields.values().filter(|f| stored_hot(desc, f)) {
            emit_size(src, f, "self");
        }
        if uses_cold_storage(desc) {
            let _ = writeln!(src, "        if let Some(c) = self.cold.as_deref() {{");
            for f in desc.fields.values().filter(|f| stored_cold(desc, f)) {
                emit_size(src, f, "c");
            }
            let _ = writeln!(src, "        }}");
        }
    }
    let _ = writeln!(src, "        self.cached_size.set(n);");
    let _ = writeln!(src, "        n");
    let _ = writeln!(src, "    }}");

    let _ = writeln!(
        src,
        "    fn write_to(&self, out: &mut impl pbrs::rt::WireOut) {{"
    );
    if desc.message_set_wire_format {
        emit_message_set_write(src, desc);
    } else {
        for f in desc.fields.values().filter(|f| stored_hot(desc, f)) {
            emit_write(src, f, "self");
        }
        if uses_cold_storage(desc) {
            let _ = writeln!(src, "        if let Some(c) = self.cold.as_deref() {{");
            for f in desc.fields.values().filter(|f| stored_cold(desc, f)) {
                emit_write(src, f, "c");
            }
            let _ = writeln!(src, "        }}");
        }
    }
    let _ = writeln!(src, "        self.unknown.encode(out);");
    let _ = writeln!(src, "    }}");
}

fn emit_message_set_merge(src: &mut String, desc: &MessageDescriptor) {
    let _ = writeln!(
        src,
        "            1 => match w {{ pbrs::rt::WIRE_SGROUP | pbrs::rt::WIRE_LEN => {{"
    );
    let _ = writeln!(
        src,
        "                    let mut type_id = 0u32; let mut payload: Vec<u8> = Vec::new();"
    );
    let _ = writeln!(src, "                    if w == pbrs::rt::WIRE_LEN {{ let inner = pbrs::rt::read_len_bytes(data, pos)?; let mut p = 0; while p < inner.len() {{ let (n, ww) = pbrs::rt::decode_tag(inner, &mut p)?; match (n, ww) {{ (2, pbrs::rt::WIRE_VARINT) => type_id = pbrs::rt::decode_varint(inner, &mut p)? as u32, (3, pbrs::rt::WIRE_LEN) => payload = pbrs::rt::read_len_bytes(inner, &mut p)?.to_vec(), _ => pbrs::rt::skip_field(inner, &mut p, ww)?, }} }} }} else {{ loop {{ let (n, ww) = pbrs::rt::decode_tag(data, pos)?; if ww == pbrs::rt::WIRE_EGROUP && n == 1 {{ break; }} match (n, ww) {{ (2, pbrs::rt::WIRE_VARINT) => type_id = pbrs::rt::decode_varint(data, pos)? as u32, (3, pbrs::rt::WIRE_LEN) => payload = pbrs::rt::read_len_bytes(data, pos)?.to_vec(), _ => pbrs::rt::skip_field(data, pos, ww)?, }} }} }}");
    let _ = writeln!(src, "                    match type_id {{");
    for f in desc.fields.values() {
        let id = field_id(f);
        let t = scalar_type(f);
        let num = f.number;
        if is_lazy_msg(f) {
            let _ = writeln!(src, "                        {num} => {{ if self.{id}.is_some() {{ self.{id}.get_or_insert().merge_bytes(&payload, depth + 1)?; }} else {{ let mut inner = {t}::default(); inner.merge_bytes(&payload, depth + 1)?; self.{id} = pbrs::rt::LazyMsg::from_owned(inner); }} }}");
        } else {
            let _ = writeln!(src, "                        {num} => {{ match &mut self.{id} {{ Some(existing) => existing.merge_bytes(&payload, depth + 1)?, None => {{ let mut inner = {t}::default(); inner.merge_bytes(&payload, depth + 1)?; self.{id} = Some(Box::new(inner)); }} }} }}");
        }
    }
    let _ = writeln!(src, "                        _ => {{ let mut u = pbrs::UnknownFields::default(); u.fields.push(pbrs::rt::UnknownField::Varint {{ number: 2, value: u64::from(type_id) }}); u.fields.push(pbrs::rt::UnknownField::LengthDelimited {{ number: 3, value: payload }}); self.unknown.fields.push(pbrs::rt::UnknownField::Group {{ number: 1, fields: u }}); }}");
    let _ = writeln!(src, "                    }}");
    let _ = writeln!(src, "                }}");
    let _ = writeln!(
        src,
        "                _ => self.unknown.fields.push(pbrs::rt::capture_unknown(data, pos, n, w)?),"
    );
    let _ = writeln!(src, "            }}");
}

fn emit_message_set_size(src: &mut String, desc: &MessageDescriptor) {
    for f in desc.fields.values() {
        let id = field_id(f);
        let num = f.number;
        let _ = writeln!(src, "        if let Some(m) = self.{id}.as_deref() {{ let inner = m.compute_size(); let item = pbrs::rt::tag_len(2, pbrs::rt::WIRE_VARINT) + pbrs::rt::varint_len({num} as u64) + pbrs::rt::key_len_value_len(3, inner); n += pbrs::rt::tag_len(1, pbrs::rt::WIRE_SGROUP) + item + pbrs::rt::tag_len(1, pbrs::rt::WIRE_EGROUP); }}");
    }
}

fn emit_message_set_write(src: &mut String, desc: &MessageDescriptor) {
    for f in desc.fields.values() {
        let id = field_id(f);
        let num = f.number;
        let _ = writeln!(src, "        if let Some(m) = self.{id}.as_deref() {{ pbrs::rt::encode_tag(out, 1, pbrs::rt::WIRE_SGROUP); pbrs::rt::encode_tag(out, 2, pbrs::rt::WIRE_VARINT); pbrs::rt::encode_varint(out, {num} as u64); pbrs::rt::encode_len_header(out, 3, m.compute_size()); m.write_to(out); pbrs::rt::encode_tag(out, 1, pbrs::rt::WIRE_EGROUP); }}");
    }
}

fn read_scalar_expr(ty: FieldType, buf: &str, pos: &str) -> String {
    match ty {
        FieldType::Double => format!("f64::from_bits(pbrs::rt::read_fixed64({buf}, {pos})?)"),
        FieldType::Float => format!("f32::from_bits(pbrs::rt::read_fixed32({buf}, {pos})?)"),
        FieldType::Fixed64 => format!("pbrs::rt::read_fixed64({buf}, {pos})?"),
        FieldType::Sfixed64 => format!("pbrs::rt::read_fixed64({buf}, {pos})? as i64"),
        FieldType::Fixed32 => format!("pbrs::rt::read_fixed32({buf}, {pos})?"),
        FieldType::Sfixed32 => format!("pbrs::rt::read_fixed32({buf}, {pos})? as i32"),
        FieldType::Sint32 => {
            format!("pbrs::rt::decode_zigzag32(pbrs::rt::decode_varint({buf}, {pos})?)")
        }
        FieldType::Sint64 => {
            format!("pbrs::rt::decode_zigzag64(pbrs::rt::decode_varint({buf}, {pos})?)")
        }
        FieldType::Bool => format!("pbrs::rt::decode_varint({buf}, {pos})? != 0"),
        FieldType::Int64 => format!("pbrs::rt::decode_varint({buf}, {pos})? as i64"),
        FieldType::Uint64 => format!("pbrs::rt::decode_varint({buf}, {pos})?"),
        FieldType::Uint32 => format!("pbrs::rt::decode_varint({buf}, {pos})? as u32"),
        _ => format!("pbrs::rt::decode_varint({buf}, {pos})? as i32"),
    }
}

fn emit_validate_arm(src: &mut String, f: &FieldDescriptor, req_bit: Option<usize>) {
    let num = f.number;
    let mark = if let Some(b) = req_bit {
        format!(" seen |= 1 << {b};")
    } else {
        String::new()
    };
    if f.is_map {
        let _ = writeln!(src, "                pbrs::rt::WIRE_LEN => {{ let (s, e) = pbrs::rt::read_len_span(data, pos)?; let mut ip = 0; let w = wire.window(s, e); let d = w.as_slice(); while ip < d.len() {{ let (_, ww) = pbrs::rt::decode_tag(d, &mut ip)?; pbrs::rt::skip_field(d, &mut ip, ww)?; }}{mark} }}");
        return;
    }
    if f.cardinality == Cardinality::Repeated {
        if f.field_type == FieldType::String {
            let utf = if f.utf8_validate {
                "pbrs::rt::require_utf8(&data[s..e])?;"
            } else {
                ""
            };
            let _ = writeln!(src, "                pbrs::rt::WIRE_LEN => {{ let (s, e) = pbrs::rt::read_len_span(data, pos)?; {utf}{mark} }}");
        } else if f.field_type == FieldType::Bytes {
            let _ = writeln!(src, "                pbrs::rt::WIRE_LEN => {{ pbrs::rt::read_len_span(data, pos)?;{mark} }}");
        } else if f.field_type == FieldType::Message {
            let t = scalar_type(f);
            let _ = writeln!(src, "                pbrs::rt::WIRE_LEN => {{ let (s, e) = pbrs::rt::read_len_span(data, pos)?; let mut ip = 0; {t}::validate_inner(&wire.window(s, e), &mut ip, depth + 1)?;{mark} }}");
        } else if f.field_type == FieldType::Group || f.delimited {
            let t = scalar_type(f);
            let _ = writeln!(src, "                pbrs::rt::WIRE_SGROUP => {{ {t}::validate_until(wire, pos, depth + 1, Some({num}))?;{mark} }}");
        } else if f.field_type.is_packable() {
            let packed_ty = packed_storage_ty(f);
            let unpacked = read_scalar_expr(f.field_type, "data", "pos");
            let packed_wire = wire_const(f.field_type);
            let _ = writeln!(src, "                pbrs::rt::WIRE_LEN => {{ let (s, e) = pbrs::rt::read_len_span(data, pos)?; {packed_ty}::validate_bytes(&data[s..e])?;{mark} }}");
            let _ = writeln!(
                src,
                "                {packed_wire} => {{ let _ = {unpacked};{mark} }}"
            );
        } else {
            let unpacked = read_scalar_expr(f.field_type, "data", "pos");
            let w = wire_const(f.field_type);
            let _ = writeln!(
                src,
                "                {w} => {{ let _ = {unpacked};{mark} }}"
            );
        }
        return;
    }
    if f.field_type == FieldType::Message {
        let t = scalar_type(f);
        if f.field_type == FieldType::Group || f.delimited {
            let _ = writeln!(src, "                pbrs::rt::WIRE_SGROUP => {{ {t}::validate_until(wire, pos, depth + 1, Some({num}))?;{mark} }}");
        } else {
            let _ = writeln!(src, "                pbrs::rt::WIRE_LEN => {{ let (s, e) = pbrs::rt::read_len_span(data, pos)?; let mut ip = 0; {t}::validate_inner(&wire.window(s, e), &mut ip, depth + 1)?;{mark} }}");
        }
        return;
    }
    if f.field_type == FieldType::Group || f.delimited {
        let t = scalar_type(f);
        let _ = writeln!(src, "                pbrs::rt::WIRE_SGROUP => {{ {t}::validate_until(wire, pos, depth + 1, Some({num}))?;{mark} }}");
        return;
    }
    if f.field_type == FieldType::String {
        let utf = if f.utf8_validate {
            "pbrs::rt::require_utf8(&data[s..e])?;"
        } else {
            ""
        };
        let _ = writeln!(src, "                pbrs::rt::WIRE_LEN => {{ let (s, e) = pbrs::rt::read_len_span(data, pos)?; {utf}{mark} }}");
        return;
    }
    if f.field_type == FieldType::Bytes {
        let _ = writeln!(src, "                pbrs::rt::WIRE_LEN => {{ pbrs::rt::read_len_span(data, pos)?;{mark} }}");
        return;
    }
    let expr = read_scalar_expr(f.field_type, "data", "pos");
    let w = wire_const(f.field_type);
    let _ = writeln!(src, "                {w} => {{ let _ = {expr};{mark} }}");
}

fn lazy_str_from_parse(f: &FieldDescriptor) -> &'static str {
    if f.utf8_validate {
        "pbrs::rt::LazyStr::from_parse_span(wire, data, s, e)?"
    } else {
        "pbrs::rt::LazyStr::from_parse_span_unchecked(wire, data, s, e)"
    }
}

/// Same-tag run for repeated length-delimited items (strings/bytes).
/// Reserve from remaining bytes so `tags_32` does not grow the Vec 5×.
fn emit_repeated_len_run(src: &mut String, num: u32, st: &str, push: &str) {
    let _ = writeln!(
        src,
        "                pbrs::rt::WIRE_LEN => {{ let (s, e) = pbrs::rt::read_len_span(data, pos)?; let rest = data.len().saturating_sub(e); if rest > 2 {{ {st}.reserve(1 + rest / (e - s + 2).max(1)); }} {st}.push({push}); while *pos < data.len() {{ let save = *pos; match pbrs::rt::decode_tag(data, pos) {{ Ok((n2, w2)) if n2 == {num} && w2 == pbrs::rt::WIRE_LEN => {{ let (s, e) = pbrs::rt::read_len_span(data, pos)?; {st}.push({push}); }} Ok(_) => {{ *pos = save; break; }} Err(e) => return Err(e), }} }} }}"
    );
}

fn emit_merge_arm(src: &mut String, desc: &MessageDescriptor, f: &FieldDescriptor) {
    let st = store_mut(desc, f);
    let num = f.number;
    if f.is_map {
        let _ = writeln!(src, "                pbrs::rt::WIRE_LEN => {{");
        let _ = writeln!(
            src,
            "                    let (s, e) = pbrs::rt::read_len_span(data, pos)?;"
        );
        let _ = writeln!(
            src,
            "                    let (kk, vv) = decode_map_entry_{}_{}_{num}(&pbrs::rt::Wire::ensure(wire, data).window(s, e), depth + 1)?;",
            rust_ident(&desc.full_name),
            field_id(f).replace("r#", "")
        );
        let _ = writeln!(src, "                    {st}.push_entry(kk, vv);");
        let _ = writeln!(src, "                }}");
        return;
    }
    if f.cardinality == Cardinality::Repeated {
        if f.field_type == FieldType::String {
            emit_repeated_len_run(src, num, &st, lazy_str_from_parse(f));
        } else if f.field_type == FieldType::Bytes {
            emit_repeated_len_run(
                src,
                num,
                &st,
                "pbrs::rt::LazyBytes::from_wire(pbrs::rt::Wire::ensure(wire, data).window(s, e))",
            );
        } else if f.field_type == FieldType::Message || f.field_type == FieldType::Group {
            let t = scalar_type(f);
            if f.field_type == FieldType::Group || f.delimited {
                let _ = writeln!(src, "                pbrs::rt::WIRE_SGROUP => {{ let mut inner = {t}::default(); inner.merge_group(data, wire, pos, {num}, depth + 1)?; {st}.push(inner); }}");
            } else {
                let _ = writeln!(
                    src,
                    "                pbrs::rt::WIRE_LEN => {{ let (s, e) = pbrs::rt::read_len_span(data, pos)?; let rest = data.len().saturating_sub(e); if rest > 2 {{ {st}.reserve(1 + rest / (e - s + 4).max(1)); }} let mut inner = {t}::default(); let mut ip = 0; let mut sw = None; inner.merge_inner(&data[s..e], &mut sw, &mut ip, depth + 1, true, None)?; {st}.push(inner); while *pos < data.len() {{ let save = *pos; match pbrs::rt::decode_tag(data, pos) {{ Ok((n2, w2)) if n2 == {num} && w2 == pbrs::rt::WIRE_LEN => {{ let (s, e) = pbrs::rt::read_len_span(data, pos)?; let mut inner = {t}::default(); let mut ip = 0; let mut sw = None; inner.merge_inner(&data[s..e], &mut sw, &mut ip, depth + 1, true, None)?; {st}.push(inner); }} Ok(_) => {{ *pos = save; break; }} Err(e) => return Err(e), }} }} }}"
                );
            }
        } else if f.field_type.is_packable() {
            let unpacked = read_scalar_expr(f.field_type, "data", "pos");
            let packed_wire = wire_const(f.field_type);
            if f.packed {
                if is_memcpy_packed(f) {
                    // Payload-only Arc. Do not Arc the parent message and do
                    // not eager-decode the Vec on parse.
                    let _ = writeln!(src, "                pbrs::rt::WIRE_LEN => {{ let (s, e) = pbrs::rt::read_len_span(data, pos)?; {st}.append_wire(pbrs::rt::Wire::from_slice(&data[s..e]))?; }}");
                } else {
                    let _ = writeln!(src, "                pbrs::rt::WIRE_LEN => {{ let (s, e) = pbrs::rt::read_len_span(data, pos)?; {st}.append_wire(pbrs::rt::Wire::ensure(wire, data).window(s, e))?; }}");
                }
            } else {
                let expr = read_scalar_expr(f.field_type, "p", "&mut i");
                let _ = writeln!(src, "                pbrs::rt::WIRE_LEN => {{ let p = pbrs::rt::read_len_bytes(data, pos)?; let mut i = 0; while i < p.len() {{ {st}.push({expr}); }} }}");
            }
            let _ = writeln!(
                src,
                "                {packed_wire} => {{ {st}.push({unpacked}); let rest = data.len().saturating_sub(*pos); if rest > 2 {{ {st}.reserve(rest / 3); }} while *pos < data.len() {{ let save = *pos; match pbrs::rt::decode_tag(data, pos) {{ Ok((n2, w2)) if n2 == {num} && w2 == {packed_wire} => {st}.push({unpacked}), Ok(_) => {{ *pos = save; break; }} Err(e) => return Err(e), }} }} }}"
            );
        } else {
            let unpacked = read_scalar_expr(f.field_type, "data", "pos");
            let w = wire_const(f.field_type);
            let _ = writeln!(src, "                {w} => {st}.push({unpacked}),");
        }
        return;
    }
    if f.field_type == FieldType::Message || f.field_type == FieldType::Group {
        let t = scalar_type(f);
        if f.field_type == FieldType::Group || f.delimited {
            let _ = writeln!(src, "                pbrs::rt::WIRE_SGROUP => {{");
            emit_oneof_clear(src, desc, f);
            let _ = writeln!(src, "                    match &mut {st} {{ Some(existing) => existing.merge_group(data, wire, pos, {num}, depth + 1)?, None => {{ let mut inner = {t}::default(); inner.merge_group(data, wire, pos, {num}, depth + 1)?; {st} = Some(Box::new(inner)); }} }}");
            let _ = writeln!(src, "                    }}");
        } else {
            let _ = writeln!(src, "                pbrs::rt::WIRE_LEN => {{");
            emit_oneof_clear(src, desc, f);
            let _ = writeln!(
                src,
                "                    let (s, e) = pbrs::rt::read_len_span(data, pos)?;"
            );
            if is_lazy_msg(f) {
                let _ = writeln!(src, "                    if {st}.is_some() {{ let mut ip = 0; let mut sw = None; {st}.get_or_insert().merge_inner(&data[s..e], &mut sw, &mut ip, depth + 1, true, None)?; }} else {{ let mut ip = 0; {t}::validate_inner(&pbrs::rt::Wire::ensure(wire, data).window(s, e), &mut ip, depth + 1)?; {st} = pbrs::rt::LazyMsg::from_wire(pbrs::rt::Wire::ensure(wire, data).window(s, e)); }}");
            } else {
                let _ = writeln!(src, "                    match &mut {st} {{ Some(existing) => {{ let mut ip = 0; let mut sw = None; existing.merge_inner(&data[s..e], &mut sw, &mut ip, depth + 1, true, None)?; }} None => {{ let mut inner = {t}::default(); let mut ip = 0; let mut sw = None; inner.merge_inner(&data[s..e], &mut sw, &mut ip, depth + 1, true, None)?; {st} = Some(Box::new(inner)); }} }}");
            }
            let _ = writeln!(src, "                    }}");
        }
        return;
    }
    if f.field_type == FieldType::String {
        let parse = lazy_str_from_parse(f);
        let assign = if is_option(f) {
            format!("{st} = Some(Box::new({parse}))")
        } else {
            format!("{st} = {parse}")
        };
        let _ = writeln!(src, "                pbrs::rt::WIRE_LEN => {{");
        emit_oneof_clear(src, desc, f);
        let _ = writeln!(
            src,
            "                    let (s, e) = pbrs::rt::read_len_span(data, pos)?; {assign};"
        );
        let _ = writeln!(src, "                    }}");
        return;
    }
    if f.field_type == FieldType::Bytes {
        let assign = if is_option(f) {
            format!("{st} = Some(Box::new(pbrs::rt::LazyBytes::from_wire(pbrs::rt::Wire::ensure(wire, data).window(s, e))))")
        } else {
            format!("{st} = pbrs::rt::LazyBytes::from_wire(pbrs::rt::Wire::ensure(wire, data).window(s, e))")
        };
        let _ = writeln!(src, "                pbrs::rt::WIRE_LEN => {{");
        emit_oneof_clear(src, desc, f);
        let _ = writeln!(
            src,
            "                    let (s, e) = pbrs::rt::read_len_span(data, pos)?; {assign};"
        );
        let _ = writeln!(src, "                    }}");
        return;
    }
    let expr = read_scalar_expr(f.field_type, "data", "pos");
    let w = wire_const(f.field_type);
    let assign = if is_option(f) && f.field_type == FieldType::Bool {
        format!("{st} = pbrs::rt::OptBool::some({expr})")
    } else if is_option(f) {
        format!("{st} = Some({expr})")
    } else {
        format!("{st} = {expr}")
    };
    let _ = writeln!(src, "                {w} => {{");
    emit_oneof_clear(src, desc, f);
    let _ = writeln!(src, "                    {assign};");
    let _ = writeln!(src, "                    }}");
}

fn packed_len_expr(v: &str, ty: FieldType) -> String {
    match ty {
        FieldType::Fixed64 | FieldType::Sfixed64 | FieldType::Double => "8".into(),
        FieldType::Fixed32 | FieldType::Sfixed32 | FieldType::Float => "4".into(),
        FieldType::Sint32 => {
            format!("pbrs::rt::varint_len(pbrs::rt::encode_zigzag32({v}))")
        }
        FieldType::Sint64 => {
            format!("pbrs::rt::varint_len(pbrs::rt::encode_zigzag64({v}))")
        }
        FieldType::Bool => format!("pbrs::rt::varint_len(u64::from({v}))"),
        _ => format!("pbrs::rt::varint_len(({v}) as u64)"),
    }
}

fn write_packed_stmt(out: &str, v: &str, ty: FieldType) -> String {
    match ty {
        FieldType::Double => format!("({out}).extend_from_slice(&({v}).to_bits().to_le_bytes())"),
        FieldType::Float => format!("({out}).extend_from_slice(&({v}).to_bits().to_le_bytes())"),
        FieldType::Fixed64 => format!("({out}).extend_from_slice(&({v}).to_le_bytes())"),
        FieldType::Sfixed64 => format!("({out}).extend_from_slice(&(({v}) as u64).to_le_bytes())"),
        FieldType::Fixed32 => format!("({out}).extend_from_slice(&({v}).to_le_bytes())"),
        FieldType::Sfixed32 => format!("({out}).extend_from_slice(&(({v}) as u32).to_le_bytes())"),
        FieldType::Sint32 => {
            format!("pbrs::rt::encode_varint({out}, pbrs::rt::encode_zigzag32({v}))")
        }
        FieldType::Sint64 => {
            format!("pbrs::rt::encode_varint({out}, pbrs::rt::encode_zigzag64({v}))")
        }
        FieldType::Bool => format!("pbrs::rt::encode_varint({out}, u64::from({v}))"),
        _ => format!("pbrs::rt::encode_varint({out}, {v} as u64)"),
    }
}

fn emit_size(src: &mut String, f: &FieldDescriptor, p: &str) {
    let id = field_id(f);
    let fld = format!("{p}.{id}");
    let num = f.number;
    if f.is_map {
        let _ = writeln!(src, "        if !{fld}.is_empty() {{");
        let _ = writeln!(src, "        for (k, v) in {fld}.pairs() {{");
        let key_sz = map_key_size(map_key_ty(f), "k");
        let val_sz = map_val_size(map_val_ty(f), "v");
        let _ = writeln!(src, "            let inner = {key_sz} + {val_sz};");
        let _ = writeln!(
            src,
            "            n += pbrs::rt::key_len_value_len({num}, inner);"
        );
        let _ = writeln!(src, "        }}");
        let _ = writeln!(src, "        }}");
        return;
    }
    if f.cardinality == Cardinality::Repeated {
        if f.field_type == FieldType::String || f.field_type == FieldType::Bytes {
            let _ = writeln!(src, "        for t in {fld}.iter() {{ n += pbrs::rt::key_len_value_len({num}, t.as_bytes().len() as u64); }}");
        } else if f.field_type == FieldType::Group || f.delimited {
            let _ = writeln!(src, "        for t in {fld}.iter() {{ n += pbrs::rt::tag_len({num}, pbrs::rt::WIRE_SGROUP) + t.compute_size() + pbrs::rt::tag_len({num}, pbrs::rt::WIRE_EGROUP); }}");
        } else if f.field_type == FieldType::Message {
            let _ = writeln!(src, "        for t in {fld}.iter() {{ n += pbrs::rt::key_len_value_len({num}, t.compute_size()); }}");
        } else if f.packed && f.field_type.is_packable() {
            let plen = packed_len_expr("*t", f.field_type);
            let _ = writeln!(src, "        if let Some(p) = {fld}.packed_bytes() {{ n += pbrs::rt::key_len_value_len({num}, p.len() as u64); }} else if !{fld}.is_empty() {{ let mut payload = 0u64; for t in {fld}.iter() {{ payload += {plen}; }} n += pbrs::rt::key_len_value_len({num}, payload); }}");
        } else {
            let plen = packed_len_expr("*t", f.field_type);
            let w = wire_const(f.field_type);
            let _ = writeln!(
                src,
                "        for t in {fld}.iter() {{ n += pbrs::rt::tag_len({num}, {w}) + {plen}; }}"
            );
        }
        return;
    }
    if f.field_type == FieldType::Group || f.delimited {
        let _ = writeln!(src, "        if let Some(m) = &{fld} {{ n += pbrs::rt::tag_len({num}, pbrs::rt::WIRE_SGROUP) + m.compute_size() + pbrs::rt::tag_len({num}, pbrs::rt::WIRE_EGROUP); }}");
        return;
    }
    if f.field_type == FieldType::Message {
        if is_lazy_msg(f) {
            let _ = writeln!(src, "        if let Some(p) = {fld}.wire_bytes() {{ n += pbrs::rt::key_len_value_len({num}, p.len() as u64); }} else if let Some(m) = {fld}.as_deref() {{ n += pbrs::rt::key_len_value_len({num}, m.compute_size()); }}");
        } else {
            let _ = writeln!(src, "        if let Some(m) = &{fld} {{ n += pbrs::rt::key_len_value_len({num}, m.compute_size()); }}");
        }
        return;
    }
    if f.field_type == FieldType::String || f.field_type == FieldType::Bytes {
        if is_option(f) {
            let _ = writeln!(src, "        if let Some(s) = &{fld} {{ n += pbrs::rt::key_len_value_len({num}, s.as_bytes().len() as u64); }}");
        } else {
            let _ = writeln!(src, "        if !{fld}.is_empty() {{ n += pbrs::rt::key_len_value_len({num}, {fld}.as_bytes().len() as u64); }}");
        }
        return;
    }
    let sz = packed_len_expr("v", f.field_type);
    let w = wire_const(f.field_type);
    if is_option(f) && f.field_type == FieldType::Bool {
        let _ = writeln!(
            src,
            "        if let Some(v) = {fld}.get() {{ n += pbrs::rt::tag_len({num}, {w}) + {sz}; }}"
        );
    } else if is_option(f) {
        let _ = writeln!(
            src,
            "        if let Some(v) = {fld} {{ n += pbrs::rt::tag_len({num}, {w}) + {sz}; }}"
        );
    } else {
        let pred = implicit_present(&fld, f.field_type);
        let sz2 = packed_len_expr(&fld, f.field_type);
        let _ = writeln!(
            src,
            "        if {pred} {{ n += pbrs::rt::tag_len({num}, {w}) + {sz2}; }}"
        );
    }
}

fn map_key_size(ty: FieldType, var: &str) -> String {
    match ty {
        FieldType::String => {
            format!("pbrs::rt::key_len_value_len(1, {var}.as_bytes().len() as u64)")
        }
        FieldType::Bool => format!(
            "pbrs::rt::tag_len(1, pbrs::rt::WIRE_VARINT) + pbrs::rt::varint_len(u64::from(*{var}))"
        ),
        FieldType::Fixed32 | FieldType::Sfixed32 => {
            "pbrs::rt::tag_len(1, pbrs::rt::WIRE_I32) + 4".into()
        }
        FieldType::Fixed64 | FieldType::Sfixed64 => {
            "pbrs::rt::tag_len(1, pbrs::rt::WIRE_I64) + 8".into()
        }
        FieldType::Sint32 => format!(
            "pbrs::rt::tag_len(1, pbrs::rt::WIRE_VARINT) + pbrs::rt::varint_len(pbrs::rt::encode_zigzag32(*{var}))"
        ),
        FieldType::Sint64 => format!(
            "pbrs::rt::tag_len(1, pbrs::rt::WIRE_VARINT) + pbrs::rt::varint_len(pbrs::rt::encode_zigzag64(*{var}))"
        ),
        FieldType::Uint32 => format!(
            "pbrs::rt::tag_len(1, pbrs::rt::WIRE_VARINT) + pbrs::rt::varint_len(u64::from(*{var}))"
        ),
        FieldType::Uint64 => format!(
            "pbrs::rt::tag_len(1, pbrs::rt::WIRE_VARINT) + pbrs::rt::varint_len(*{var})"
        ),
        _ => format!(
            "pbrs::rt::tag_len(1, pbrs::rt::WIRE_VARINT) + pbrs::rt::varint_len((*{var}) as u64)"
        ),
    }
}

fn map_val_size(ty: FieldType, var: &str) -> String {
    match ty {
        FieldType::String | FieldType::Bytes => {
            format!("pbrs::rt::key_len_value_len(2, {var}.as_bytes().len() as u64)")
        }
        FieldType::Message | FieldType::Group => {
            format!("pbrs::rt::key_len_value_len(2, {var}.compute_size())")
        }
        FieldType::Bool => {
            format!("pbrs::rt::tag_len(2, pbrs::rt::WIRE_VARINT) + pbrs::rt::varint_len(u64::from(*{var}))")
        }
        FieldType::Float => "pbrs::rt::tag_len(2, pbrs::rt::WIRE_I32) + 4".into(),
        FieldType::Double => "pbrs::rt::tag_len(2, pbrs::rt::WIRE_I64) + 8".into(),
        FieldType::Fixed32 | FieldType::Sfixed32 => {
            "pbrs::rt::tag_len(2, pbrs::rt::WIRE_I32) + 4".into()
        }
        FieldType::Fixed64 | FieldType::Sfixed64 => {
            "pbrs::rt::tag_len(2, pbrs::rt::WIRE_I64) + 8".into()
        }
        FieldType::Sint32 => format!(
            "pbrs::rt::tag_len(2, pbrs::rt::WIRE_VARINT) + pbrs::rt::varint_len(pbrs::rt::encode_zigzag32(*{var}))"
        ),
        FieldType::Sint64 => format!(
            "pbrs::rt::tag_len(2, pbrs::rt::WIRE_VARINT) + pbrs::rt::varint_len(pbrs::rt::encode_zigzag64(*{var}))"
        ),
        FieldType::Uint32 => format!(
            "pbrs::rt::tag_len(2, pbrs::rt::WIRE_VARINT) + pbrs::rt::varint_len(u64::from(*{var}))"
        ),
        FieldType::Uint64 => format!(
            "pbrs::rt::tag_len(2, pbrs::rt::WIRE_VARINT) + pbrs::rt::varint_len(*{var})"
        ),
        _ => format!(
            "pbrs::rt::tag_len(2, pbrs::rt::WIRE_VARINT) + pbrs::rt::varint_len((*{var}) as u64)"
        ),
    }
}

fn emit_write(src: &mut String, f: &FieldDescriptor, p: &str) {
    let id = field_id(f);
    let fld = format!("{p}.{id}");
    let num = f.number;
    if f.is_map {
        let kty = map_key_ty(f);
        let vty = map_val_ty(f);
        let _ = writeln!(src, "        if !{fld}.is_empty() {{");
        let _ = writeln!(src, "        for (k, v) in {fld}.pairs() {{");
        let _ = writeln!(
            src,
            "            let inner = {} + {};",
            map_key_size(kty, "k"),
            map_val_size(vty, "v")
        );
        let _ = writeln!(src, "            pbrs::rt::encode_tag(out, {num}, pbrs::rt::WIRE_LEN); pbrs::rt::encode_varint(out, inner);");
        emit_map_key_write(src, kty);
        emit_map_val_write(src, vty);
        let _ = writeln!(src, "        }}");
        let _ = writeln!(src, "        }}");
        return;
    }
    if f.cardinality == Cardinality::Repeated {
        if f.field_type == FieldType::String || f.field_type == FieldType::Bytes {
            let _ = writeln!(src, "        for t in {fld}.iter() {{ pbrs::rt::encode_len_field(out, {num}, t.as_bytes()); }}");
        } else if f.field_type == FieldType::Group || f.delimited {
            let _ = writeln!(src, "        for t in {fld}.iter() {{ pbrs::rt::encode_tag(out, {num}, pbrs::rt::WIRE_SGROUP); t.write_to(out); pbrs::rt::encode_tag(out, {num}, pbrs::rt::WIRE_EGROUP); }}");
        } else if f.field_type == FieldType::Message {
            let _ = writeln!(src, "        for t in {fld}.iter() {{ pbrs::rt::encode_len_header(out, {num}, t.compute_size()); t.write_to(out); }}");
        } else if f.packed && f.field_type.is_packable() {
            let plen = packed_len_expr("*t", f.field_type);
            let stmt = write_packed_stmt("out", "*t", f.field_type);
            let _ = writeln!(src, "        if let Some(p) = {fld}.packed_bytes() {{ pbrs::rt::encode_len_header(out, {num}, p.len() as u64); out.extend_from_slice(p); }} else if !{fld}.is_empty() {{ let mut payload = 0u64; for t in {fld}.iter() {{ payload += {plen}; }} pbrs::rt::encode_len_header(out, {num}, payload); for t in {fld}.iter() {{ {stmt}; }} }}");
        } else {
            let w = wire_const(f.field_type);
            let stmt = write_packed_stmt("out", "*t", f.field_type);
            let _ = writeln!(src, "        for t in {fld}.iter() {{ pbrs::rt::encode_tag(out, {num}, {w}); {stmt}; }}");
        }
        return;
    }
    if f.field_type == FieldType::Group || f.delimited {
        let _ = writeln!(src, "        if let Some(m) = &{fld} {{ pbrs::rt::encode_tag(out, {num}, pbrs::rt::WIRE_SGROUP); m.write_to(out); pbrs::rt::encode_tag(out, {num}, pbrs::rt::WIRE_EGROUP); }}");
        return;
    }
    if f.field_type == FieldType::Message {
        if is_lazy_msg(f) {
            let _ = writeln!(src, "        if let Some(p) = {fld}.wire_bytes() {{ pbrs::rt::encode_len_header(out, {num}, p.len() as u64); out.extend_from_slice(p); }} else if let Some(m) = {fld}.as_deref() {{ pbrs::rt::encode_len_header(out, {num}, m.compute_size()); m.write_to(out); }}");
        } else {
            let _ = writeln!(src, "        if let Some(m) = &{fld} {{ pbrs::rt::encode_len_header(out, {num}, m.compute_size()); m.write_to(out); }}");
        }
        return;
    }
    if f.field_type == FieldType::String || f.field_type == FieldType::Bytes {
        if is_option(f) {
            let _ = writeln!(src, "        if let Some(s) = &{fld} {{ pbrs::rt::encode_len_field(out, {num}, s.as_bytes()); }}");
        } else {
            let _ = writeln!(src, "        if !{fld}.is_empty() {{ pbrs::rt::encode_len_field(out, {num}, {fld}.as_bytes()); }}");
        }
        return;
    }
    let w = wire_const(f.field_type);
    if is_option(f) && f.field_type == FieldType::Bool {
        let stmt = write_packed_stmt("out", "v", f.field_type);
        let _ = writeln!(src, "        if let Some(v) = {fld}.get() {{ pbrs::rt::encode_tag(out, {num}, {w}); {stmt}; }}");
    } else if is_option(f) {
        let stmt = write_packed_stmt("out", "v", f.field_type);
        let _ = writeln!(
            src,
            "        if let Some(v) = {fld} {{ pbrs::rt::encode_tag(out, {num}, {w}); {stmt}; }}"
        );
    } else {
        let pred = implicit_present(&fld, f.field_type);
        let stmt = write_packed_stmt("out", &fld, f.field_type);
        let _ = writeln!(
            src,
            "        if {pred} {{ pbrs::rt::encode_tag(out, {num}, {w}); {stmt}; }}"
        );
    }
}

fn emit_map_key_write(src: &mut String, ty: FieldType) {
    emit_map_scalar_write(src, 1, "k", ty);
}

fn emit_map_val_write(src: &mut String, ty: FieldType) {
    emit_map_scalar_write(src, 2, "v", ty);
}

fn emit_map_scalar_write(src: &mut String, n: u32, var: &str, ty: FieldType) {
    match ty {
        FieldType::String | FieldType::Bytes => {
            let _ = writeln!(
                src,
                "            pbrs::rt::encode_len_field(out, {n}, {var}.as_bytes());"
            );
        }
        FieldType::Message | FieldType::Group => {
            let _ = writeln!(src, "            pbrs::rt::encode_len_header(out, {n}, {var}.compute_size()); {var}.write_to(out);");
        }
        FieldType::Float => {
            let _ = writeln!(src, "            pbrs::rt::encode_tag(out, {n}, pbrs::rt::WIRE_I32); out.extend_from_slice(&{var}.to_bits().to_le_bytes());");
        }
        FieldType::Double => {
            let _ = writeln!(src, "            pbrs::rt::encode_tag(out, {n}, pbrs::rt::WIRE_I64); out.extend_from_slice(&{var}.to_bits().to_le_bytes());");
        }
        FieldType::Fixed32 => {
            let _ = writeln!(src, "            pbrs::rt::encode_tag(out, {n}, pbrs::rt::WIRE_I32); out.extend_from_slice(&{var}.to_le_bytes());");
        }
        FieldType::Sfixed32 => {
            let _ = writeln!(src, "            pbrs::rt::encode_tag(out, {n}, pbrs::rt::WIRE_I32); out.extend_from_slice(&(*{var} as u32).to_le_bytes());");
        }
        FieldType::Fixed64 => {
            let _ = writeln!(src, "            pbrs::rt::encode_tag(out, {n}, pbrs::rt::WIRE_I64); out.extend_from_slice(&{var}.to_le_bytes());");
        }
        FieldType::Sfixed64 => {
            let _ = writeln!(src, "            pbrs::rt::encode_tag(out, {n}, pbrs::rt::WIRE_I64); out.extend_from_slice(&(*{var} as u64).to_le_bytes());");
        }
        FieldType::Sint32 => {
            let _ = writeln!(src, "            pbrs::rt::encode_tag(out, {n}, pbrs::rt::WIRE_VARINT); pbrs::rt::encode_varint(out, pbrs::rt::encode_zigzag32(*{var}));");
        }
        FieldType::Sint64 => {
            let _ = writeln!(src, "            pbrs::rt::encode_tag(out, {n}, pbrs::rt::WIRE_VARINT); pbrs::rt::encode_varint(out, pbrs::rt::encode_zigzag64(*{var}));");
        }
        FieldType::Bool => {
            let _ = writeln!(src, "            pbrs::rt::encode_tag(out, {n}, pbrs::rt::WIRE_VARINT); pbrs::rt::encode_varint(out, u64::from(*{var}));");
        }
        FieldType::Uint64 => {
            let _ = writeln!(src, "            pbrs::rt::encode_tag(out, {n}, pbrs::rt::WIRE_VARINT); pbrs::rt::encode_varint(out, *{var});");
        }
        _ => {
            let _ = writeln!(src, "            pbrs::rt::encode_tag(out, {n}, pbrs::rt::WIRE_VARINT); pbrs::rt::encode_varint(out, *{var} as u64);");
        }
    }
}

fn emit_map_decoders(src: &mut String, desc: &MessageDescriptor) {
    bind_field_idents(desc);
    let msg = rust_ident(&desc.full_name);
    for f in desc.fields.values() {
        if !f.is_map {
            continue;
        }
        let id = field_id(f).replace("r#", "");
        let num = f.number;
        let (k, v) = map_kv(f);
        let kty = map_key_ty(f);
        let vty = map_val_ty(f);
        let _ = writeln!(
            src,
            "fn decode_map_entry_{msg}_{id}_{num}(wire: &pbrs::rt::Wire, depth: u32) -> Result<({k}, {v}), ParseError> {{"
        );
        let _ = writeln!(src, "    let _ = depth; let data = wire.as_slice(); let mut key = {k}::default(); let mut val = {v}::default(); let mut pos = 0;");
        let _ = writeln!(src, "    while pos < data.len() {{ let (n, w) = pbrs::rt::decode_tag(data, &mut pos)?; match (n, w) {{");
        emit_map_scalar_decode(src, 1, "key", kty, f.utf8_validate);
        emit_map_scalar_decode(src, 2, "val", vty, f.utf8_validate);
        let _ = writeln!(
            src,
            "        _ => pbrs::rt::skip_field(data, &mut pos, w)?,"
        );
        let _ = writeln!(src, "    }} }}");
        let _ = writeln!(src, "    Ok((key, val))");
        let _ = writeln!(src, "}}");
    }
}

fn emit_map_scalar_decode(src: &mut String, n: u32, var: &str, ty: FieldType, utf8: bool) {
    match ty {
        FieldType::String => {
            if utf8 {
                let _ = writeln!(src, "        ({n}, pbrs::rt::WIRE_LEN) => {{ let (s, e) = pbrs::rt::read_len_span(data, &mut pos)?; pbrs::rt::require_utf8(&data[s..e])?; {var} = pbrs::rt::LazyStr::from_span(wire, s, e); }},");
            } else {
                let _ = writeln!(src, "        ({n}, pbrs::rt::WIRE_LEN) => {{ let (s, e) = pbrs::rt::read_len_span(data, &mut pos)?; {var} = pbrs::rt::LazyStr::from_span(wire, s, e); }},");
            }
        }
        FieldType::Bytes => {
            let _ = writeln!(src, "        ({n}, pbrs::rt::WIRE_LEN) => {{ let (s, e) = pbrs::rt::read_len_span(data, &mut pos)?; {var} = pbrs::rt::LazyBytes::from_wire(wire.window(s, e)); }},");
        }
        FieldType::Message | FieldType::Group => {
            let _ = writeln!(src, "        ({n}, pbrs::rt::WIRE_LEN) => {{ let (s, e) = pbrs::rt::read_len_span(data, &mut pos)?; let mut ip = 0; let mut sw = None; {var}.merge_inner(&data[s..e], &mut sw, &mut ip, depth, true, None)?; }},");
        }
        FieldType::Float => {
            let _ = writeln!(src, "        ({n}, pbrs::rt::WIRE_I32) => {var} = f32::from_bits(pbrs::rt::read_fixed32(data, &mut pos)?),");
        }
        FieldType::Double => {
            let _ = writeln!(src, "        ({n}, pbrs::rt::WIRE_I64) => {var} = f64::from_bits(pbrs::rt::read_fixed64(data, &mut pos)?),");
        }
        FieldType::Fixed32 => {
            let _ = writeln!(src, "        ({n}, pbrs::rt::WIRE_I32) => {var} = pbrs::rt::read_fixed32(data, &mut pos)?,");
        }
        FieldType::Sfixed32 => {
            let _ = writeln!(src, "        ({n}, pbrs::rt::WIRE_I32) => {var} = pbrs::rt::read_fixed32(data, &mut pos)? as i32,");
        }
        FieldType::Fixed64 => {
            let _ = writeln!(src, "        ({n}, pbrs::rt::WIRE_I64) => {var} = pbrs::rt::read_fixed64(data, &mut pos)?,");
        }
        FieldType::Sfixed64 => {
            let _ = writeln!(src, "        ({n}, pbrs::rt::WIRE_I64) => {var} = pbrs::rt::read_fixed64(data, &mut pos)? as i64,");
        }
        FieldType::Sint32 => {
            let _ = writeln!(src, "        ({n}, pbrs::rt::WIRE_VARINT) => {var} = pbrs::rt::decode_zigzag32(pbrs::rt::decode_varint(data, &mut pos)?),");
        }
        FieldType::Sint64 => {
            let _ = writeln!(src, "        ({n}, pbrs::rt::WIRE_VARINT) => {var} = pbrs::rt::decode_zigzag64(pbrs::rt::decode_varint(data, &mut pos)?),");
        }
        FieldType::Bool => {
            let _ = writeln!(src, "        ({n}, pbrs::rt::WIRE_VARINT) => {var} = pbrs::rt::decode_varint(data, &mut pos)? != 0,");
        }
        FieldType::Uint64 => {
            let _ = writeln!(src, "        ({n}, pbrs::rt::WIRE_VARINT) => {var} = pbrs::rt::decode_varint(data, &mut pos)?,");
        }
        FieldType::Int64 => {
            let _ = writeln!(src, "        ({n}, pbrs::rt::WIRE_VARINT) => {var} = pbrs::rt::decode_varint(data, &mut pos)? as i64,");
        }
        FieldType::Uint32 => {
            let _ = writeln!(src, "        ({n}, pbrs::rt::WIRE_VARINT) => {var} = pbrs::rt::decode_varint(data, &mut pos)? as u32,");
        }
        _ => {
            let _ = writeln!(src, "        ({n}, pbrs::rt::WIRE_VARINT) => {var} = pbrs::rt::decode_varint(data, &mut pos)? as i32,");
        }
    }
}

fn enum_api_ty(f: &FieldDescriptor) -> Option<String> {
    if f.field_type != FieldType::Enum {
        return None;
    }
    let name = f
        .enum_ty
        .as_ref()
        .map(|e| e.full_name.as_str())
        .or(f.type_name.as_deref())
        .unwrap_or("UnknownEnum");
    Some(rust_ident(name))
}

fn enum_first_number(f: &FieldDescriptor) -> i32 {
    f.enum_ty
        .as_ref()
        .and_then(|e| e.listed.first().map(|x| x.0))
        .unwrap_or(0)
}

fn scalar_default_expr(f: &FieldDescriptor) -> String {
    if f.field_type == FieldType::Enum {
        if let Some(d) = f.default.as_deref() {
            if let Ok(n) = d.parse::<i32>() {
                return n.to_string();
            }
            if let Some(n) = f.enum_ty.as_ref().and_then(|e| e.names.get(d)) {
                return n.to_string();
            }
        }
        return enum_first_number(f).to_string();
    }
    let Some(d) = f.default.as_deref() else {
        return match f.field_type {
            FieldType::Bool => "false".into(),
            FieldType::Float | FieldType::Double => "0.0".into(),
            _ => "0".into(),
        };
    };
    match f.field_type {
        FieldType::Bool => {
            if d.eq_ignore_ascii_case("true") {
                "true".into()
            } else {
                "false".into()
            }
        }
        FieldType::Float | FieldType::Double => {
            let f32ty = f.field_type == FieldType::Float;
            let inf = if f32ty {
                "f32::INFINITY"
            } else {
                "f64::INFINITY"
            };
            let ninf = if f32ty {
                "f32::NEG_INFINITY"
            } else {
                "f64::NEG_INFINITY"
            };
            let nan = if f32ty { "f32::NAN" } else { "f64::NAN" };
            let lower = d.to_ascii_lowercase();
            if lower == "inf" || lower == "infinity" || lower == "+inf" || lower == "+infinity" {
                inf.into()
            } else if lower == "-inf" || lower == "-infinity" {
                ninf.into()
            } else if lower == "nan" {
                nan.into()
            } else if d.contains('.') || d.contains('e') || d.contains('E') {
                d.to_string()
            } else {
                format!("{d}.0")
            }
        }
        _ => d.to_string(),
    }
}

fn rust_byte_lit(bytes: &[u8]) -> String {
    let mut out = String::from("b\"");
    for &b in bytes {
        match b {
            b'\\' => out.push_str("\\\\"),
            b'"' => out.push_str("\\\""),
            b'\n' => out.push_str("\\n"),
            b'\r' => out.push_str("\\r"),
            b'\t' => out.push_str("\\t"),
            0x20..=0x7e => out.push(b as char),
            _ => {
                let _ = write!(out, "\\x{b:02x}");
            }
        }
    }
    out.push('"');
    out
}

fn unescape_c_bytes(s: &str) -> Vec<u8> {
    let mut out = Vec::new();
    let b = s.as_bytes();
    let mut i = 0;
    while i < b.len() {
        if b[i] == b'\\' && i + 1 < b.len() {
            i += 1;
            match b[i] {
                b'n' => out.push(b'\n'),
                b'r' => out.push(b'\r'),
                b't' => out.push(b'\t'),
                b'\\' => out.push(b'\\'),
                b'"' => out.push(b'"'),
                b'\'' => out.push(b'\''),
                b'x' if i + 2 < b.len() => {
                    let h = std::str::from_utf8(&b[i + 1..i + 3]).unwrap_or("00");
                    out.push(u8::from_str_radix(h, 16).unwrap_or(0));
                    i += 2;
                }
                c if c.is_ascii_digit() => {
                    let mut v = 0u8;
                    let mut n = 0;
                    while i < b.len() && n < 3 && b[i].is_ascii_digit() {
                        v = v.saturating_mul(8).saturating_add(b[i] - b'0');
                        i += 1;
                        n += 1;
                    }
                    out.push(v);
                    continue;
                }
                c => out.push(c),
            }
            i += 1;
        } else {
            out.push(b[i]);
            i += 1;
        }
    }
    out
}

fn string_default_lit(f: &FieldDescriptor) -> String {
    match f.default.as_deref() {
        Some(d) => rust_byte_lit(&unescape_c_bytes(d)),
        None => "b\"\"".into(),
    }
}

fn bytes_default_lit(f: &FieldDescriptor) -> String {
    string_default_lit(f)
}

fn camel_to_snake_name(s: &str) -> String {
    to_snake(s)
}

fn screaming_snake_to_upper_camel(s: &str) -> String {
    let mut out = String::new();
    let mut cap = true;
    for c in s.chars() {
        if c.is_ascii_alphabetic() {
            if cap {
                out.extend(c.to_uppercase());
            } else {
                out.extend(c.to_lowercase());
            }
            cap = false;
        } else if c.is_ascii_digit() {
            out.push(c);
            cap = true;
        } else {
            cap = true;
        }
    }
    out
}

fn starts_with_ignore_ascii(name: &str, prefix: &str) -> bool {
    name.len() >= prefix.len() && name[..prefix.len()].eq_ignore_ascii_case(prefix)
}

fn strip_enum_prefix<'a>(enum_name: &str, value_name: &'a str) -> &'a str {
    let prefixes = [
        enum_name.to_string(),
        screaming_snake_to_upper_camel(enum_name),
        camel_to_snake_name(enum_name),
    ];
    for p in &prefixes {
        if starts_with_ignore_ascii(value_name, p) {
            let mut rest = &value_name[p.len()..];
            rest = rest.strip_prefix('_').unwrap_or(rest);
            if !rest.is_empty() {
                return rest;
            }
            break;
        }
    }
    value_name
}

fn enum_value_rs_name(enum_name: &str, value_name: &str) -> String {
    let stripped = strip_enum_prefix(enum_name, value_name);
    let mut name = screaming_snake_to_upper_camel(stripped);
    if name.is_empty() {
        name = screaming_snake_to_upper_camel(value_name);
    }
    if name.chars().next().is_some_and(|c| c.is_ascii_digit()) {
        name = format!("_{name}");
    }
    if is_rust_keyword(&name) {
        format!("r#{name}")
    } else {
        name
    }
}

struct RustEnumValue {
    name: String,
    number: i32,
    aliases: Vec<String>,
}

fn rust_enum_values(enum_name: &str, listed: &[(i32, String)]) -> Vec<RustEnumValue> {
    let mut seen_name = std::collections::BTreeSet::new();
    let mut by_number: std::collections::BTreeMap<i32, usize> = std::collections::BTreeMap::new();
    let mut out: Vec<RustEnumValue> = Vec::new();
    for (number, proto_name) in listed {
        let rust_name = enum_value_rs_name(enum_name, proto_name);
        if !seen_name.insert(rust_name.clone()) {
            continue;
        }
        if let Some(&idx) = by_number.get(number) {
            out[idx].aliases.push(rust_name);
        } else {
            by_number.insert(*number, out.len());
            out.push(RustEnumValue {
                name: rust_name,
                number: *number,
                aliases: Vec::new(),
            });
        }
    }
    out
}

fn emit_enum(src: &mut String, ed: &crate::dynamic::EnumDescriptor) {
    let name = rust_ident(&ed.full_name);
    let values = rust_enum_values(&ed.name, &ed.listed);
    let first = ed.listed.first().map(|x| x.0).unwrap_or(0);
    let known = values
        .iter()
        .map(|v| v.number.to_string())
        .collect::<Vec<_>>()
        .join(" | ");
    let _ = writeln!(src, "#[repr(transparent)]");
    let _ = writeln!(
        src,
        "#[derive(Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]"
    );
    let _ = writeln!(src, "pub struct {name}(i32);");
    let _ = writeln!(
        src,
        "    #[allow(non_upper_case_globals, reason = \"protobuf enum names\")]"
    );
    let _ = writeln!(src, "impl {name} {{");
    for v in &values {
        let _ = writeln!(
            src,
            "    pub const {}: {name} = {name}({});",
            v.name, v.number
        );
        for a in &v.aliases {
            let _ = writeln!(src, "    pub const {a}: {name} = {name}({});", v.number);
        }
    }
    let _ = writeln!(src, "    fn constant_name(self) -> Option<&'static str> {{");
    let _ = writeln!(
        src,
        "        #[allow(unreachable_patterns, reason = \"open enums\")]"
    );
    let _ = writeln!(src, "        Some(match self.0 {{");
    for v in &values {
        let _ = writeln!(src, "            {} => \"{}\",", v.number, v.name);
    }
    let _ = writeln!(src, "            _ => return None,");
    let _ = writeln!(src, "        }})");
    let _ = writeln!(src, "    }}");
    let _ = writeln!(src, "}}");
    let _ = writeln!(src, "impl From<{name}> for i32 {{");
    let _ = writeln!(src, "    fn from(v: {name}) -> i32 {{ v.0 }}");
    let _ = writeln!(src, "}}");
    if ed.closed {
        let _ = writeln!(src, "impl TryFrom<i32> for {name} {{");
        let _ = writeln!(src, "    type Error = UnknownEnumValue<Self>;");
        let _ = writeln!(
            src,
            "    fn try_from(val: i32) -> Result<Self, Self::Error> {{"
        );
        let _ = writeln!(
            src,
            "        if <Self as Enum>::is_known(val) {{ Ok(Self(val)) }} else {{ Err(UnknownEnumValue::new(pbrs::__internal::Private, val)) }}"
        );
        let _ = writeln!(src, "    }}");
        let _ = writeln!(src, "}}");
    } else {
        let _ = writeln!(src, "impl From<i32> for {name} {{");
        let _ = writeln!(src, "    fn from(val: i32) -> Self {{ Self(val) }}");
        let _ = writeln!(src, "}}");
    }
    let _ = writeln!(src, "impl Default for {name} {{");
    let _ = writeln!(src, "    fn default() -> Self {{ Self({first}) }}");
    let _ = writeln!(src, "}}");
    let _ = writeln!(src, "impl std::fmt::Debug for {name} {{");
    let _ = writeln!(
        src,
        "    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {{"
    );
    let _ = writeln!(
        src,
        "        if let Some(n) = self.constant_name() {{ write!(f, \"{name}::{{n}}\") }} else {{ write!(f, \"{name}::from({{}})\", self.0) }}"
    );
    let _ = writeln!(src, "    }}");
    let _ = writeln!(src, "}}");
    let _ = writeln!(src, "impl pbrs::__internal::SealedInternal for {name} {{}}");
    let _ = writeln!(
        src,
        "impl pbrs::Proxied for {name} {{ type View<'msg> = {name}; }}"
    );
    let _ = writeln!(src, "impl pbrs::AsView for {name} {{");
    let _ = writeln!(src, "    type Proxied = Self;");
    let _ = writeln!(src, "    fn as_view(&self) -> Self {{ *self }}");
    let _ = writeln!(src, "}}");
    let _ = writeln!(src, "impl<'msg> pbrs::IntoView<'msg> for {name} {{");
    let _ = writeln!(
        src,
        "    fn into_view<'shorter>(self) -> Self where 'msg: 'shorter {{ self }}"
    );
    let _ = writeln!(src, "}}");
    let _ = writeln!(src, "impl Enum for {name} {{");
    let _ = writeln!(src, "    const NAME: &'static str = \"{name}\";");
    if known.is_empty() {
        let _ = writeln!(src, "    fn is_known(_: i32) -> bool {{ false }}");
    } else {
        let _ = writeln!(
            src,
            "    fn is_known(value: i32) -> bool {{ matches!(value, {known}) }}"
        );
    }
    let _ = writeln!(src, "}}");
}

fn emit_nested_mods(src: &mut String, messages: &[String], enums: &[String]) {
    let msg_set: std::collections::BTreeSet<&str> = messages.iter().map(|s| s.as_str()).collect();
    #[derive(Default)]
    struct Node {
        children: std::collections::BTreeMap<String, Node>,
        items: Vec<(String, String)>,
    }
    let mut root = Node::default();
    let mut add = |full: &str| {
        let ident = rust_ident(full);
        let last = ident_last(full);
        let mut cur = full;
        let mut path = Vec::new();
        while let Some((parent, _)) = cur.rsplit_once('.') {
            if !msg_set.contains(parent) {
                break;
            }
            path.push(to_snake(&ident_last(parent)));
            cur = parent;
        }
        if path.is_empty() {
            return;
        }
        path.reverse();
        let mut node = &mut root;
        for p in &path {
            node = node.children.entry(p.clone()).or_default();
        }
        node.items.push((last, ident));
    };
    for m in messages {
        add(m);
    }
    for e in enums {
        add(e);
    }
    fn emit_node(src: &mut String, name: &str, node: &Node, depth: usize) {
        let _ = writeln!(src, "pub mod {name} {{");
        let prefix = "super::".repeat(depth);
        for (export, ident) in &node.items {
            if export == ident {
                let _ = writeln!(src, "    pub use {prefix}{ident};");
            } else {
                let _ = writeln!(src, "    pub use {prefix}{ident} as {export};");
            }
        }
        for (child_name, child) in &node.children {
            emit_node(src, child_name, child, depth + 1);
        }
        let _ = writeln!(src, "}}");
    }
    for (name, node) in &root.children {
        emit_node(src, name, node, 1);
    }
}

fn to_snake(s: &str) -> String {
    let mut out = String::new();
    for (i, c) in s.chars().enumerate() {
        if c.is_uppercase() {
            if i > 0 {
                out.push('_');
            }
            out.extend(c.to_lowercase());
        } else {
            out.push(c);
        }
    }
    if is_rust_keyword(&out) {
        format!("r#{out}")
    } else {
        out
    }
}

/// Fully qualified path to the kernel crate. Generated code never relies on a
/// `use` the surrounding module might not have.
const G: &str = "::pbrs_grpc";

/// The four gRPC call shapes, as they appear in generated signatures.
struct Shape {
    /// Request envelope contents in the service trait.
    trait_request: String,
    /// Response envelope contents in the service trait.
    trait_response: String,
    /// [`Rpc`] method the server dispatches through.
    dispatch: &'static str,
    /// Client method signature and body.
    client_arg: String,
    client_return: String,
    client_call: &'static str,
    /// Human description for the emitted doc comment.
    description: &'static str,
}

fn shape_of(m: &MethodDescriptor) -> Shape {
    let req = rust_ident(&m.input_type);
    let resp = rust_ident(&m.output_type);
    match (m.client_streaming, m.server_streaming) {
        (false, false) => Shape {
            trait_request: req.clone(),
            trait_response: resp.clone(),
            dispatch: "unary",
            client_arg: format!("{G}::Request<{req}>"),
            client_return: format!("{G}::Call<{G}::Response<{resp}>>"),
            client_call: "unary",
            description: "Unary",
        },
        (true, false) => Shape {
            trait_request: format!("{G}::Streaming<{req}>"),
            trait_response: resp.clone(),
            dispatch: "client_streaming",
            client_arg: format!("{G}::Request<()>"),
            client_return: format!("({G}::StreamSender<{req}>, {G}::Call<{G}::Response<{resp}>>)"),
            client_call: "client_streaming",
            description: "Client-streaming",
        },
        (false, true) => Shape {
            trait_request: req.clone(),
            trait_response: format!("{G}::Streaming<{resp}>"),
            dispatch: "server_streaming",
            client_arg: format!("{G}::Request<{req}>"),
            client_return: format!("{G}::Call<{G}::Response<{G}::Streaming<{resp}>>>"),
            client_call: "server_streaming",
            description: "Server-streaming",
        },
        (true, true) => Shape {
            trait_request: format!("{G}::Streaming<{req}>"),
            trait_response: format!("{G}::Streaming<{resp}>"),
            dispatch: "bidi_streaming",
            client_arg: format!("{G}::Request<()>"),
            client_return: format!(
                "({G}::StreamSender<{req}>, {G}::Call<{G}::Response<{G}::Streaming<{resp}>>>)"
            ),
            client_call: "bidi",
            description: "Bidirectional-streaming",
        },
    }
}

/// Emit `pbrs-grpc` stubs for one service: the handler trait, a server that
/// implements [`Service`](../../pbrs_grpc/trait.Service.html), and a client.
fn emit_kernel_service(src: &mut String, svc: &ServiceDescriptor) {
    let svc_ty = rust_ident(&svc.full_name);
    let trait_name = svc_ty.clone();
    let server = format!("{svc_ty}Server");
    let client = format!("{svc_ty}Client");
    let full_name = &svc.full_name;

    emit_kernel_trait(src, &trait_name, svc);
    emit_kernel_server(src, &trait_name, &server, full_name, svc);
    emit_kernel_client(src, &trait_name, &client, full_name, svc);
}

fn emit_kernel_trait(src: &mut String, trait_name: &str, svc: &ServiceDescriptor) {
    let _ = writeln!(src, "/// The `{}` service.", svc.full_name);
    let _ = writeln!(src, "///");
    let _ = writeln!(
        src,
        "/// Implement this, then serve it with [`{trait_name}Server`]."
    );
    let _ = writeln!(src, "///");
    let _ = writeln!(
        src,
        "/// Methods you omit return [`{G}::Status::unimplemented`]."
    );
    let _ = writeln!(src, "pub trait {trait_name}: Send + Sync + 'static {{");
    for m in &svc.methods {
        let shape = shape_of(m);
        let fn_name = to_snake(&m.name);
        let _ = writeln!(src, "    /// {} `{}`.", shape.description, m.name);
        let _ = writeln!(src, "    ///");
        let _ = writeln!(
            src,
            "    /// The default answers [`{G}::Status::unimplemented`]."
        );
        let _ = writeln!(
            src,
            "    /// Inbound `grpc-encoding` is [`{G}::Request::encoding`] (`None` for identity)."
        );
        let _ = writeln!(
            src,
            "    /// Spawned work should await [`{G}::Request::cancelled`]; the kernel drops a still-pending handler on client RST."
        );
        let _ = writeln!(
            src,
            "    /// [`{G}::Status::from_error_details`] is the typed bag after a generated handler Err; those trailers reach the client."
        );
        let _ = writeln!(
            src,
            "    /// Distinct from a generated server intercept Err: that is trailers without reading the body; this generated handler Err is after the handler ran."
        );
        let _ = writeln!(
            src,
            "    /// Distinct from a generated intercept Err: that is a local reject never opens a stream; this generated handler Err is after the handler ran."
        );
        let _ = writeln!(
            src,
            "    /// Distinct from a generated server on_response Err: that is trailers-only after handler Ok; this generated handler Err is after the handler ran."
        );
        let _ = writeln!(
            src,
            "    /// Distinct from a generated client on_response Err: that fails the Call after a successful receive; this generated handler Err is after the handler ran."
        );
        let _ = writeln!(
            src,
            "    /// Distinct from a generated StreamSender fail: that is trailers after any messages already sent; this generated handler Err is after the handler ran."
        );
        if m.server_streaming {
            let _ = writeln!(
                src,
                "    /// Spawned producers should select on [`{G}::Request::cancelled`] and [`{G}::StreamSender::closed`]; drain aborts on client RST."
            );
            let _ = writeln!(
                src,
                "    /// [`{G}::StreamSender::fail`] after a message ships trailing metadata and `grpc-status-details-bin` the same as a handler `Err`."
            );
            let _ = writeln!(
                src,
                "    /// [`{G}::Status::from_error_details`] is the typed bag after a generated StreamSender fail on a server response producer; those trailers ship after any messages already sent."
            );
            let _ = writeln!(
                src,
                "    /// Distinct from a generated handler Err: that is after the handler ran; this generated StreamSender fail is trailers after any messages already sent."
            );
            let _ = writeln!(
                src,
                "    /// Distinct from a generated server intercept Err: that is trailers without reading the body; this generated StreamSender fail is trailers after any messages already sent."
            );
            let _ = writeln!(
                src,
                "    /// Distinct from a generated server on_response Err: that is trailers-only after handler Ok; this generated StreamSender fail is trailers after any messages already sent."
            );
            let _ = writeln!(
                src,
                "    /// Distinct from a generated intercept Err: that is a local reject never opens a stream; this generated StreamSender fail is trailers after any messages already sent."
            );
            let _ = writeln!(
                src,
                "    /// Distinct from a generated client on_response Err: that fails the Call after a successful receive; this generated StreamSender fail is trailers after any messages already sent."
            );
        }
        if m.client_streaming && !m.server_streaming {
            let _ = writeln!(
                src,
                "    /// That signal still fires after the client half-closes the request stream."
            );
        }
        let _ = writeln!(src, "    fn {fn_name}(");
        let _ = writeln!(src, "        &self,");
        let _ = writeln!(
            src,
            "        request: {G}::Request<{}>,",
            shape.trait_request
        );
        let _ = writeln!(
            src,
            "    ) -> impl ::core::future::Future<Output = ::core::result::Result<{G}::Response<{}>, {G}::Status>> + Send {{",
            shape.trait_response
        );
        let _ = writeln!(src, "        async move {{");
        let _ = writeln!(src, "            drop(request);");
        let _ = writeln!(
            src,
            "            ::core::result::Result::Err({G}::Status::unimplemented(\"method {}/{} not implemented\"))",
            svc.full_name,
            m.name
        );
        let _ = writeln!(src, "        }}");
        let _ = writeln!(src, "    }}");
    }
    let _ = writeln!(src, "}}");
}

fn emit_kernel_server(
    src: &mut String,
    trait_name: &str,
    server: &str,
    full_name: &str,
    svc: &ServiceDescriptor,
) {
    let _ = writeln!(src, "/// Serves an implementation of [`{trait_name}`].");
    let _ = writeln!(src, "///");
    let _ = writeln!(
        src,
        "/// [`Self::rpc_timeout`], [`Self::compresses_outbound`], [`Self::gzip_level`], [`Self::accepts_compressed`], [`Self::concurrent_rpc_limit`], [`Self::send_buffer_size`], and [`Self::limits`] read the server overlay without colliding with the setters. Same getters as [`{G}::Server`] / [`{G}::Router`]."
    );
    let _ = writeln!(src, "pub struct {server}<T> {{");
    let _ = writeln!(src, "    inner: ::std::sync::Arc<T>,");
    let _ = writeln!(src, "    config: {G}::ServerConfig,");
    let _ = writeln!(src, "}}");

    let _ = writeln!(src, "impl<T> ::core::clone::Clone for {server}<T> {{");
    let _ = writeln!(src, "    fn clone(&self) -> Self {{");
    let _ = writeln!(src, "        Self {{");
    let _ = writeln!(
        src,
        "            inner: ::std::sync::Arc::clone(&self.inner),"
    );
    let _ = writeln!(src, "            config: self.config,");
    let _ = writeln!(src, "        }}");
    let _ = writeln!(src, "    }}");
    let _ = writeln!(src, "}}");

    let _ = writeln!(src, "impl<T> {server}<T> {{");
    let _ = writeln!(
        src,
        "    /// Wrap an existing `Arc` without adding another layer."
    );
    let _ = writeln!(src, "    #[must_use]");
    let _ = writeln!(
        src,
        "    pub fn from_arc(inner: ::std::sync::Arc<T>) -> Self {{"
    );
    let _ = writeln!(src, "        Self {{");
    let _ = writeln!(src, "            inner,");
    let _ = writeln!(
        src,
        "            config: <{G}::ServerConfig as ::core::default::Default>::default(),"
    );
    let _ = writeln!(src, "        }}");
    let _ = writeln!(src, "    }}");
    let _ = writeln!(src, "    /// Take the inner `Arc` back.");
    let _ = writeln!(src, "    #[must_use]");
    let _ = writeln!(
        src,
        "    pub fn into_inner(self) -> ::std::sync::Arc<T> {{ self.inner }}"
    );
    let _ = writeln!(src, "}}");

    let _ = writeln!(src, "impl<T> ::core::fmt::Debug for {server}<T> {{");
    let _ = writeln!(
        src,
        "    fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {{"
    );
    let _ = writeln!(
        src,
        "        f.debug_struct(\"{server}\").field(\"service\", &\"{full_name}\").field(\"config\", &self.config).finish()"
    );
    let _ = writeln!(src, "    }}");
    let _ = writeln!(src, "}}");

    let _ = writeln!(src, "impl<T: {trait_name}> {server}<T> {{");
    let _ = writeln!(src, "    /// Fully qualified proto service name.");
    let _ = writeln!(src, "    pub const NAME: &'static str = \"{full_name}\";");
    let _ = writeln!(
        src,
        "    /// Wrap an implementation with default configuration."
    );
    let _ = writeln!(src, "    #[must_use]");
    let _ = writeln!(src, "    pub fn new(inner: T) -> Self {{");
    let _ = writeln!(src, "        Self {{");
    let _ = writeln!(src, "            inner: ::std::sync::Arc::new(inner),");
    let _ = writeln!(
        src,
        "            config: <{G}::ServerConfig as ::core::default::Default>::default(),"
    );
    let _ = writeln!(src, "        }}");
    let _ = writeln!(src, "    }}");
    let _ = writeln!(
        src,
        "    /// Replace the transport and limit configuration."
    );
    let _ = writeln!(src, "    #[must_use]");
    let _ = writeln!(
        src,
        "    pub fn config(mut self, config: {G}::ServerConfig) -> Self {{ self.config = config; self }}"
    );
    let _ = writeln!(
        src,
        "    /// The configuration in effect. Distinct from [`Self::config`], which replaces it. See [`{G}::Server::server_config`]."
    );
    let _ = writeln!(src, "    #[must_use]");
    let _ = writeln!(
        src,
        "    pub fn server_config(&self) -> {G}::ServerConfig {{ self.config }}"
    );
    let _ = writeln!(
        src,
        "    /// Cap inbound messages at `limit` bytes. Default 4 MiB. Applies to every call shape, including over TLS, mTLS, Unix, and [`{G}::Server::serve_connection`]."
    );
    let _ = writeln!(src, "    #[must_use]");
    let _ = writeln!(
        src,
        "    pub fn max_decoding_message_size(mut self, limit: usize) -> Self {{ self.config = self.config.max_decoding_message_size(limit); self }}"
    );
    let _ = writeln!(
        src,
        "    /// Cap outbound messages at `limit` bytes. Default unlimited. Applies to every call shape."
    );
    let _ = writeln!(src, "    #[must_use]");
    let _ = writeln!(
        src,
        "    pub fn max_encoding_message_size(mut self, limit: usize) -> Self {{ self.config = self.config.max_encoding_message_size(limit); self }}"
    );
    let _ = writeln!(
        src,
        "    /// Replace both message caps at once. Applies to every call shape. See [`{G}::ServerConfig::message_limits`]."
    );
    let _ = writeln!(
        src,
        "    /// Distinct from [`Self::max_decoding_message_size`] / [`Self::max_encoding_message_size`]. Oversize inbound or outbound is `RESOURCE_EXHAUSTED` on every call shape, including over TLS, mTLS, Unix, and [`{G}::Server::serve_connection`]."
    );
    let _ = writeln!(src, "    #[must_use]");
    let _ = writeln!(
        src,
        "    pub fn message_limits(mut self, limits: {G}::MessageLimits) -> Self {{ self.config = self.config.message_limits(limits); self }}"
    );
    let _ = writeln!(
        src,
        "    /// Configured message caps. Distinct from [`Self::message_limits`], which sets them. Distinct from [`Self::send_buffer_size`]: that is the HTTP/2 send buffer, not uncompressed protobuf bytes. See [`{G}::Server::limits`]. Applies to every call shape."
    );
    let _ = writeln!(src, "    #[must_use]");
    let _ = writeln!(
        src,
        "    pub fn limits(&self) -> {G}::MessageLimits {{ self.config.limits() }}"
    );
    let _ = writeln!(
        src,
        "    /// Cap how many RPCs the process will run at once. Applies to every call shape, including over TLS, mTLS, Unix, and [`{G}::Server::serve_connection`]. See [`{G}::ServerConfig::max_concurrent_rpcs`]."
    );
    let _ = writeln!(src, "    #[must_use]");
    let _ = writeln!(
        src,
        "    pub fn max_concurrent_rpcs(mut self, n: usize) -> Self {{ self.config = self.config.max_concurrent_rpcs(n); self }}"
    );
    let _ = writeln!(
        src,
        "    /// Configured process-wide RPC cap, if any. Distinct from [`Self::max_concurrent_rpcs`], which sets it. See [`{G}::Server::concurrent_rpc_limit`]. Applies to every call shape."
    );
    let _ = writeln!(src, "    #[must_use]");
    let _ = writeln!(
        src,
        "    pub fn concurrent_rpc_limit(&self) -> ::core::option::Option<usize> {{ self.config.concurrent_rpc_limit() }}"
    );
    let _ = writeln!(
        src,
        "    /// Cap how many TCP/Unix connections the accept loop will serve at once, including TLS and mTLS listeners. Applies to every call shape. See [`{G}::ServerConfig::max_concurrent_connections`]."
    );
    let _ = writeln!(src, "    #[must_use]");
    let _ = writeln!(
        src,
        "    pub fn max_concurrent_connections(mut self, n: usize) -> Self {{ self.config = self.config.max_concurrent_connections(n); self }}"
    );
    let _ = writeln!(
        src,
        "    /// Concurrent RPCs allowed per HTTP/2 connection. Applies to every call shape. See [`{G}::ServerConfig::max_concurrent_streams`]."
    );
    let _ = writeln!(
        src,
        "    /// HTTP/2 `SETTINGS_MAX_CONCURRENT_STREAMS`. Distinct from [`Self::max_concurrent_rpcs`], which refuses extras as `RESOURCE_EXHAUSTED`. A well-behaved client waits; both RPCs still complete, including over TLS, mTLS, Unix, and [`{G}::Server::serve_connection`]."
    );
    let _ = writeln!(src, "    #[must_use]");
    let _ = writeln!(
        src,
        "    pub fn max_concurrent_streams(mut self, streams: u32) -> Self {{ self.config = self.config.max_concurrent_streams(streams); self }}"
    );
    let _ = writeln!(
        src,
        "    /// HTTP/2 per-stream receive window. Applies to every call shape. See [`{G}::ServerConfig::initial_stream_window_size`]."
    );
    let _ = writeln!(
        src,
        "    /// A well-behaved client still completes every call shape, including over TLS, mTLS, Unix, and [`{G}::Server::serve_connection`]. Distinct from [`Self::max_frame_size`], which still serves at the 16 KiB SETTINGS minimum, and from [`Self::max_concurrent_streams`], which serializes extra RPCs."
    );
    let _ = writeln!(src, "    #[must_use]");
    let _ = writeln!(
        src,
        "    pub fn initial_stream_window_size(mut self, bytes: u32) -> Self {{ self.config = self.config.initial_stream_window_size(bytes); self }}"
    );
    let _ = writeln!(
        src,
        "    /// HTTP/2 per-connection receive window. Applies to every call shape. See [`{G}::ServerConfig::initial_connection_window_size`]."
    );
    let _ = writeln!(
        src,
        "    /// A well-behaved client still completes every call shape, including over TLS, mTLS, Unix, and [`{G}::Server::serve_connection`]. Distinct from [`Self::max_frame_size`], which still serves at the 16 KiB SETTINGS minimum, and from [`Self::max_concurrent_streams`], which serializes extra RPCs."
    );
    let _ = writeln!(src, "    #[must_use]");
    let _ = writeln!(
        src,
        "    pub fn initial_connection_window_size(mut self, bytes: u32) -> Self {{ self.config = self.config.initial_connection_window_size(bytes); self }}"
    );
    let _ = writeln!(
        src,
        "    /// HTTP/2 `SETTINGS_MAX_FRAME_SIZE`. Applies to every call shape. See [`{G}::ServerConfig::max_frame_size`]."
    );
    let _ = writeln!(
        src,
        "    /// A well-behaved client splits DATA; every call shape still completes, including over TLS, mTLS, Unix, and [`{G}::Server::serve_connection`]. Distinct from [`Self::max_header_list_size`], which refuses oversize metadata, and from [`Self::max_concurrent_streams`], which serializes extra RPCs."
    );
    let _ = writeln!(src, "    #[must_use]");
    let _ = writeln!(
        src,
        "    pub fn max_frame_size(mut self, bytes: u32) -> Self {{ self.config = self.config.max_frame_size(bytes); self }}"
    );
    let _ = writeln!(
        src,
        "    /// HTTP/2 `SETTINGS_MAX_HEADER_LIST_SIZE`. Applies to every call shape. See [`{G}::ServerConfig::max_header_list_size`]."
    );
    let _ = writeln!(
        src,
        "    /// Oversize metadata is refused, including over TLS, mTLS, Unix, and [`{G}::Server::serve_connection`]. Distinct from a raw HTTP/2 peer."
    );
    let _ = writeln!(src, "    #[must_use]");
    let _ = writeln!(
        src,
        "    pub fn max_header_list_size(mut self, bytes: u32) -> Self {{ self.config = self.config.max_header_list_size(bytes); self }}"
    );
    let _ = writeln!(
        src,
        "    /// HTTP/2 `SETTINGS_HEADER_TABLE_SIZE` (HPACK dynamic table). Default 4096. Applies to every call shape. Distinct from [`Self::max_header_list_size`], which caps uncompressed header-block bytes (`SETTINGS_MAX_HEADER_LIST_SIZE`). A well-behaved client still completes every call shape at this table size, including over TLS, mTLS, Unix, and [`{G}::Server::serve_connection`]. See [`{G}::ServerConfig::header_table_size`]."
    );
    let _ = writeln!(src, "    #[must_use]");
    let _ = writeln!(
        src,
        "    pub fn header_table_size(mut self, bytes: u32) -> Self {{ self.config = self.config.header_table_size(bytes); self }}"
    );
    let _ = writeln!(
        src,
        "    /// HTTP/2 small-DATA framing budget. Default 25600. Applies to every call shape. Caps extra memory from tiny DATA frames. Exceeding this is `ENHANCE_YOUR_CALM` (`too_many_data_frames`). Distinct from [`Self::initial_connection_window_size`], which is flow-control bytes, and from [`Self::max_frame_size`], which caps one DATA payload. h2 Auto (half the connection window) is not exposed. A well-behaved client still completes every call shape at this framing budget, including over TLS, mTLS, Unix, and [`{G}::Server::serve_connection`]. See [`{G}::ServerConfig::data_frame_budget`]."
    );
    let _ = writeln!(src, "    #[must_use]");
    let _ = writeln!(
        src,
        "    pub fn data_frame_budget(mut self, bytes: usize) -> Self {{ self.config = self.config.data_frame_budget(bytes); self }}"
    );
    let _ = writeln!(
        src,
        "    /// Per-connection HTTP/2 send buffer. Applies to every call shape. See [`{G}::ServerConfig::max_send_buffer_size`]."
    );
    let _ = writeln!(
        src,
        "    /// Write backpressure still completes every call shape, including over TLS, mTLS, Unix, and [`{G}::Server::serve_connection`]. Distinct from [`Self::max_frame_size`], which still serves at the 16 KiB SETTINGS minimum, and from [`Self::initial_stream_window_size`], which still serves at a small receive window."
    );
    let _ = writeln!(src, "    #[must_use]");
    let _ = writeln!(
        src,
        "    pub fn max_send_buffer_size(mut self, bytes: usize) -> Self {{ self.config = self.config.max_send_buffer_size(bytes); self }}"
    );
    let _ = writeln!(
        src,
        "    /// Configured write-time HTTP/2 send buffer. Distinct from [`Self::max_send_buffer_size`], which sets it. Distinct from [`Self::message_limits`]: that is uncompressed protobuf bytes, not this send buffer. See [`{G}::Server::send_buffer_size`]. Applies to every call shape."
    );
    let _ = writeln!(src, "    #[must_use]");
    let _ = writeln!(
        src,
        "    pub fn send_buffer_size(&self) -> usize {{ self.config.send_buffer_size() }}"
    );
    let _ = writeln!(
        src,
        "    /// Cap remotely-reset HTTP/2 streams waiting in the accept queue. Applies to every call shape. See [`{G}::ServerConfig::max_pending_accept_reset_streams`]."
    );
    let _ = writeln!(
        src,
        "    /// A well-behaved client never fills that queue; every call shape still completes, including over TLS, mTLS, Unix, and [`{G}::Server::serve_connection`]. Distinct from a raw HTTP/2 peer."
    );
    let _ = writeln!(src, "    #[must_use]");
    let _ = writeln!(
        src,
        "    pub fn max_pending_accept_reset_streams(mut self, n: usize) -> Self {{ self.config = self.config.max_pending_accept_reset_streams(n); self }}"
    );
    let _ = writeln!(
        src,
        "    /// Cap locally-reset HTTP/2 streams caused by a peer protocol error. Default 1024. Exceeding this is `ENHANCE_YOUR_CALM`. Distinct from [`Self::max_pending_accept_reset_streams`], which caps remotely-reset streams (rapid reset). This caps RSTs we send after an invalid frame. A well-behaved client never triggers one; every call shape still completes, including over TLS, mTLS, Unix, and [`{G}::Server::serve_connection`]. See [`{G}::ServerConfig::max_local_error_reset_streams`]."
    );
    let _ = writeln!(src, "    #[must_use]");
    let _ = writeln!(
        src,
        "    pub fn max_local_error_reset_streams(mut self, n: usize) -> Self {{ self.config = self.config.max_local_error_reset_streams(n); self }}"
    );
    let _ = writeln!(
        src,
        "    /// Cap remembered locally-reset HTTP/2 stream IDs. Default 50. When the cap is reached, the oldest ID is purged from memory, not `ENHANCE_YOUR_CALM`. Frames on a purged ID are a connection `PROTOCOL_ERROR`. Distinct from [`Self::max_pending_accept_reset_streams`] (rapid-reset GOAWAY) and [`Self::max_local_error_reset_streams`] (protocol-error RST GOAWAY). A well-behaved client still completes every call shape at this memory cap, including over TLS, mTLS, Unix, and [`{G}::Server::serve_connection`]. See [`{G}::ServerConfig::max_concurrent_reset_streams`]."
    );
    let _ = writeln!(src, "    #[must_use]");
    let _ = writeln!(
        src,
        "    pub fn max_concurrent_reset_streams(mut self, n: usize) -> Self {{ self.config = self.config.max_concurrent_reset_streams(n); self }}"
    );
    let _ = writeln!(
        src,
        "    /// How long locally-reset HTTP/2 stream IDs are remembered. Default 1 s. After this duration the ID is forgotten, not `ENHANCE_YOUR_CALM`. Frames on a forgotten ID are a connection `PROTOCOL_ERROR`. Distinct from [`Self::max_concurrent_reset_streams`], which is how many IDs are remembered (count). This is how long (time). A well-behaved client still completes every call shape at this reset duration, including over TLS, mTLS, Unix, and [`{G}::Server::serve_connection`]. See [`{G}::ServerConfig::reset_stream_duration`]."
    );
    let _ = writeln!(src, "    #[must_use]");
    let _ = writeln!(
        src,
        "    pub fn reset_stream_duration(mut self, dur: ::std::time::Duration) -> Self {{ self.config = self.config.reset_stream_duration(dur); self }}"
    );
    let _ = writeln!(
        src,
        "    /// Cap every RPC even when the client omits `grpc-timeout`. Applies to every call shape, including over TLS, mTLS, Unix, and [`{G}::Server::serve_connection`]. See [`{G}::ServerConfig::timeout`]."
    );
    let _ = writeln!(src, "    #[must_use]");
    let _ = writeln!(
        src,
        "    pub fn timeout(mut self, timeout: ::std::time::Duration) -> Self {{ self.config = self.config.timeout(timeout); self }}"
    );
    let _ = writeln!(
        src,
        "    /// gzip responses when the client advertises gzip. Applies to every call shape, including over TLS, mTLS, Unix, and [`{G}::Server::serve_connection`]. See [`{G}::ServerConfig::send_compressed`]."
    );
    let _ = writeln!(src, "    #[must_use]");
    let _ = writeln!(
        src,
        "    pub fn send_compressed(mut self) -> Self {{ self.config = self.config.send_compressed(true); self }}"
    );
    let _ = writeln!(
        src,
        "    /// Deflate effort for outbound gzip. Default 1 (`flate2` fast). Applies to every call shape. Distinct from [`Self::send_compressed`], which is on or off. 0 stores; 9 is best. A well-behaved client still completes every call shape, including over TLS, mTLS, Unix, and [`{G}::Server::serve_connection`]. See [`{G}::ServerConfig::gzip_compression_level`]."
    );
    let _ = writeln!(src, "    #[must_use]");
    let _ = writeln!(
        src,
        "    pub fn gzip_compression_level(mut self, level: u32) -> Self {{ self.config = self.config.gzip_compression_level(level); self }}"
    );
    let _ = writeln!(
        src,
        "    /// Inflate inbound gzip. Default `true`. Applies to every call shape, including over TLS, mTLS, Unix, and [`{G}::Server::serve_connection`]. Passing `false` refuses `grpc-encoding: gzip` as `UNIMPLEMENTED` before the handler. Distinct from [`Self::send_compressed`], which is outbound. See [`{G}::ServerConfig::accept_compressed`]."
    );
    let _ = writeln!(src, "    #[must_use]");
    let _ = writeln!(
        src,
        "    pub fn accept_compressed(mut self, accept: bool) -> Self {{ self.config = self.config.accept_compressed(accept); self }}"
    );
    let _ = writeln!(
        src,
        "    /// Cap every RPC even when the client omits `grpc-timeout`. Distinct from [`Self::timeout`], which sets it. Interceptors and handlers read the same overlay on [`{G}::Rpc::rpc_timeout`] / [`{G}::Request::rpc_timeout`]. See [`{G}::Server::rpc_timeout`]."
    );
    let _ = writeln!(src, "    #[must_use]");
    let _ = writeln!(
        src,
        "    pub fn rpc_timeout(&self) -> ::core::option::Option<::std::time::Duration> {{ self.config.rpc_timeout() }}"
    );
    let _ = writeln!(
        src,
        "    /// Whether responses are gzipped when the client accepts gzip. Distinct from [`Self::send_compressed`], which enables it. See [`{G}::Server::compresses_outbound`]."
    );
    let _ = writeln!(src, "    #[must_use]");
    let _ = writeln!(
        src,
        "    pub fn compresses_outbound(&self) -> bool {{ self.config.compresses_outbound() }}"
    );
    let _ = writeln!(
        src,
        "    /// Configured outbound gzip deflate level. Distinct from [`Self::gzip_compression_level`], which sets it. See [`{G}::Server::gzip_level`]."
    );
    let _ = writeln!(src, "    #[must_use]");
    let _ = writeln!(
        src,
        "    pub fn gzip_level(&self) -> u32 {{ self.config.gzip_level() }}"
    );
    let _ = writeln!(
        src,
        "    /// Whether inbound gzip is inflated. Default `true`. Distinct from [`Self::accept_compressed`], which sets it. Distinct from [`{G}::Rpc::accepts_gzip`], which is the peer's `grpc-accept-encoding`. See [`{G}::Server::accepts_compressed`]."
    );
    let _ = writeln!(src, "    #[must_use]");
    let _ = writeln!(
        src,
        "    pub fn accepts_compressed(&self) -> bool {{ self.config.accepts_compressed() }}"
    );
    let _ = writeln!(
        src,
        "    /// HTTP/2 PING keepalive. Applies to every call shape. See [`{G}::ServerConfig::keep_alive_interval`]."
    );
    let _ = writeln!(src, "    #[must_use]");
    let _ = writeln!(
        src,
        "    pub fn keep_alive_interval(mut self, interval: ::std::time::Duration) -> Self {{ self.config = self.config.keep_alive_interval(interval); self }}"
    );
    let _ = writeln!(
        src,
        "    /// How long to wait for a PING acknowledgement. Applies to every call shape. See [`{G}::ServerConfig::keep_alive_timeout`]."
    );
    let _ = writeln!(src, "    #[must_use]");
    let _ = writeln!(
        src,
        "    pub fn keep_alive_timeout(mut self, timeout: ::std::time::Duration) -> Self {{ self.config = self.config.keep_alive_timeout(timeout); self }}"
    );
    let _ = writeln!(
        src,
        "    /// TCP `SO_KEEPALIVE`. Applies to every call shape. See [`{G}::ServerConfig::tcp_keepalive`]."
    );
    let _ = writeln!(src, "    #[must_use]");
    let _ = writeln!(
        src,
        "    pub fn tcp_keepalive(mut self, time: ::std::time::Duration) -> Self {{ self.config = self.config.tcp_keepalive(time); self }}"
    );
    let _ = writeln!(
        src,
        "    /// TCP `TCP_KEEPINTVL` probe interval. Applies only when [`Self::tcp_keepalive`] is set; this does not turn `SO_KEEPALIVE` on by itself. Distinct from [`Self::keep_alive_interval`] (HTTP/2 PING). See [`{G}::ServerConfig::tcp_keepalive_interval`]."
    );
    let _ = writeln!(src, "    #[must_use]");
    let _ = writeln!(
        src,
        "    pub fn tcp_keepalive_interval(mut self, interval: ::std::time::Duration) -> Self {{ self.config = self.config.tcp_keepalive_interval(interval); self }}"
    );
    let _ = writeln!(
        src,
        "    /// TCP `TCP_KEEPCNT` probe count. Applies only when [`Self::tcp_keepalive`] is set; this does not turn `SO_KEEPALIVE` on by itself. Distinct from [`Self::tcp_keepalive_interval`] (`TCP_KEEPINTVL` time, not count). See [`{G}::ServerConfig::tcp_keepalive_retries`]."
    );
    let _ = writeln!(src, "    #[must_use]");
    let _ = writeln!(
        src,
        "    pub fn tcp_keepalive_retries(mut self, retries: u32) -> Self {{ self.config = self.config.tcp_keepalive_retries(retries); self }}"
    );
    let _ = writeln!(
        src,
        "    /// Send GOAWAY this long after accept. The next RPC of every call shape redials, including over TLS, mTLS, and Unix; transparent retry of the same in-flight RPC is unary and server-streaming after request bytes, client-streaming and bidi before HEADERS. See [`{G}::ServerConfig::max_connection_age`]."
    );
    let _ = writeln!(src, "    #[must_use]");
    let _ = writeln!(
        src,
        "    pub fn max_connection_age(mut self, age: ::std::time::Duration) -> Self {{ self.config = self.config.max_connection_age(age); self }}"
    );
    let _ = writeln!(
        src,
        "    /// Send GOAWAY after this long with no outstanding RPCs. The next RPC of every call shape redials, including over TLS, mTLS, and Unix. See [`{G}::ServerConfig::max_connection_idle`]."
    );
    let _ = writeln!(src, "    #[must_use]");
    let _ = writeln!(
        src,
        "    pub fn max_connection_idle(mut self, idle: ::std::time::Duration) -> Self {{ self.config = self.config.max_connection_idle(idle); self }}"
    );
    let _ = writeln!(
        src,
        "    /// After age or idle fires, wait this long for in-flight RPCs, including over TLS, mTLS, Unix, and [`{G}::Server::serve_connection`]. Applies to every call shape. See [`{G}::ServerConfig::max_connection_age_grace`]."
    );
    let _ = writeln!(src, "    #[must_use]");
    let _ = writeln!(
        src,
        "    pub fn max_connection_age_grace(mut self, grace: ::std::time::Duration) -> Self {{ self.config = self.config.max_connection_age_grace(grace); self }}"
    );
    let _ = writeln!(
        src,
        "    /// Drop a client that never finishes TLS or the HTTP/2 preface. Applies to every call shape, including over TLS, mTLS, and Unix. See [`{G}::ServerConfig::handshake_timeout`]."
    );
    let _ = writeln!(src, "    #[must_use]");
    let _ = writeln!(
        src,
        "    pub fn handshake_timeout(mut self, timeout: ::std::time::Duration) -> Self {{ self.config = self.config.handshake_timeout(timeout); self }}"
    );
    let _ = writeln!(
        src,
        "    /// Run `interceptor` before `{trait_name}` methods. It may mutate metadata, cap the deadline (`set_timeout` / `deadline` Instant), inspect path / service / method, `:authority` / `:scheme` / `peer_timeout` / `rpc_timeout` / `effective_timeout` / `local_addr` / `remote_addr` / `peer_identity` / `peer_cred` / message caps / gzip accept and encoding / `compresses_outbound` / `gzip_level` / `accepts_compressed` / `concurrent_rpc_limit` / `send_buffer_size`, attach extensions, or reject. Generated handlers see the same values on [`{G}::Request`]. Calling this twice stacks: the first interceptor runs first. Applies to every call shape; `Err` rejects before the body is read, including over TLS, mTLS, Unix, and [`{G}::Server::serve_connection`]. `Err` may carry [`{G}::Status::with_error_details`]; those trailers reach the client."
    );
    let _ = writeln!(
        src,
        "    /// [`{G}::Status::from_error_details`] is the typed bag after a generated server intercept Err; those trailers reach the client without reading the body."
    );
    let _ = writeln!(
        src,
        "    /// Distinct from a generated handler Err: that is after the handler ran; this generated server intercept Err is trailers without reading the body."
    );
    let _ = writeln!(
        src,
        "    /// Distinct from a generated server on_response Err: that is trailers-only after handler Ok; this generated server intercept Err is trailers without reading the body."
    );
    let _ = writeln!(
        src,
        "    /// Distinct from a generated intercept Err: that is a local reject never opens a stream; this generated server intercept Err is trailers without reading the body."
    );
    let _ = writeln!(
        src,
        "    /// Distinct from a generated client on_response Err: that fails the Call after a successful receive; this generated server intercept Err is trailers without reading the body."
    );
    let _ = writeln!(
        src,
        "    /// Distinct from a generated StreamSender fail: that is trailers after any messages already sent; this generated server intercept Err is trailers without reading the body."
    );
    let _ = writeln!(
        src,
        "    /// Distinct from [`{G}::Channel::intercept`]: that runs on the outbound call before the stream opens; this generated server intercept runs on the inbound RPC before the handler."
    );
    let _ = writeln!(
        src,
        "    /// Distinct from [`Self::on_response`]: that runs after the handler returns Ok; this generated server intercept runs on the inbound RPC before the handler."
    );
    let _ = writeln!(
        src,
        "    /// Compiling overlay dumps live on [`{G}::hello`] (`GreeterServer::new(Svc).intercept`)."
    );
    let _ = writeln!(src, "    #[must_use]");
    let _ = writeln!(
        src,
        "    pub fn intercept<I>(self, interceptor: I) -> {G}::Server<Self>"
    );
    let _ = writeln!(src, "    where");
    let _ = writeln!(src, "        I: {G}::Interceptor,");
    let _ = writeln!(src, "    {{");
    let _ = writeln!(src, "        self.into_server().intercept(interceptor)");
    let _ = writeln!(src, "    }}");
    let _ = writeln!(
        src,
        "    /// Run `interceptor` after `{trait_name}` methods return `Ok`. It may mutate headers, trailers, compress, and local extensions (`Response::extensions` is not on the wire; stamp metadata here to send it). Calling this twice stacks: the first interceptor runs first. Applies to every call shape, including over TLS, mTLS, Unix, and [`{G}::Server::serve_connection`]. `Err` after the handler already ran; that status is sent trailers-only instead of the response, including [`{G}::Status::with_error_details`]. A handler `Err` skips this hook. [`{G}::ResponseParts::path`] is kernel-stamped. Distinct from [`{G}::Request::path`]. [`{G}::ResponseParts::gzip_level`] is the server encode overlay. Distinct from [`{G}::ResponseParts::compress`]. [`{G}::ResponseParts::compresses_outbound`] is the server encode overlay. Distinct from [`{G}::ResponseParts::compress`], which is the per-RPC Compressed-Flag. [`{G}::ResponseParts::accepts_gzip`] is the peer `grpc-accept-encoding` advertisement. Distinct from [`{G}::ResponseParts::encoding`]. [`{G}::ResponseParts::deadline`] is kernel-stamped when writing. Distinct from [`{G}::Request::deadline`]. Distinct from [`{G}::Rpc::deadline`]. [`{G}::ResponseParts::timeout`] is the duration stamped at dispatch. Distinct from [`{G}::ResponseParts::deadline`]. [`{G}::ResponseParts::limits`] is the encode cap when writing. Distinct from [`{G}::Request::limits`]. Distinct from [`{G}::Rpc::limits`]. [`{G}::ResponseParts::peer_timeout`] is the client's `grpc-timeout`. Distinct from [`{G}::ResponseParts::timeout`]. [`{G}::ResponseParts::rpc_timeout`] is the server overlay. Distinct from [`{G}::ResponseParts::timeout`]. Distinct from [`{G}::ResponseParts::peer_timeout`]. [`{G}::ResponseParts::accepts_compressed`] is the inbound gzip overlay. Distinct from [`{G}::ResponseParts::accepts_gzip`]. [`{G}::ResponseParts::send_buffer_size`] is the write-time HTTP/2 send buffer overlay. Distinct from [`{G}::ResponseParts::limits`]."
    );
    let _ = writeln!(
        src,
        "    /// [`{G}::ResponseParts::compress_is_set`] is occupancy after a generated server on_response, so a later interceptor can fill compress only when unset."
    );
    let _ = writeln!(
        src,
        "    /// [`{G}::ResponseParts::clear_compress`] restores the server gzip overlay after a generated server on_response."
    );
    let _ = writeln!(
        src,
        "    /// [`{G}::Status::from_error_details`] is the typed bag after a generated server on_response Err; a local reject is trailers-only after handler Ok."
    );
    let _ = writeln!(
        src,
        "    /// Distinct from a generated handler Err: that is after the handler ran; this generated server on_response Err is trailers-only after handler Ok."
    );
    let _ = writeln!(
        src,
        "    /// Distinct from a generated server intercept Err: that is trailers without reading the body; this generated server on_response Err is trailers-only after handler Ok."
    );
    let _ = writeln!(
        src,
        "    /// Distinct from a generated client on_response Err: that fails the Call after a successful receive; this generated server on_response Err is trailers-only after handler Ok."
    );
    let _ = writeln!(
        src,
        "    /// Distinct from a generated StreamSender fail: that is trailers after any messages already sent; this generated server on_response Err is trailers-only after handler Ok."
    );
    let _ = writeln!(
        src,
        "    /// Distinct from [`Self::intercept`]: that runs on the inbound RPC before the handler; this generated server on_response runs after the handler returns Ok."
    );
    let _ = writeln!(
        src,
        "    /// Compiling overlay dumps live on [`{G}::hello`] (`GreeterServer::new(Svc).on_response`)."
    );
    let _ = writeln!(src, "    #[must_use]");
    let _ = writeln!(
        src,
        "    pub fn on_response<I>(self, interceptor: I) -> {G}::Server<Self>"
    );
    let _ = writeln!(src, "    where");
    let _ = writeln!(src, "        I: {G}::ResponseInterceptor,");
    let _ = writeln!(src, "    {{");
    let _ = writeln!(src, "        self.into_server().on_response(interceptor)");
    let _ = writeln!(src, "    }}");
    let _ = writeln!(
        src,
        "    /// Mount alongside another service. [`Self::max_decoding_message_size`] and [`Self::max_encoding_message_size`] stay in effect on every mounted service, on every call shape of those mounts, including over TLS, mTLS, Unix, and [`{G}::Server::serve_connection`]."
    );
    let _ = writeln!(src, "    #[must_use]");
    let _ = writeln!(
        src,
        "    pub fn add_service<S: {G}::Service>(self, service: S) -> {G}::Router {{ self.into_server().add_service(service) }}"
    );
    let _ = writeln!(
        src,
        "    /// Mount `service` when `Some`. `None` is a no-op. Distinct from [`Self::add_service`], which always mounts. `None` does not replace a service already there. Services that stay mounted still complete every call shape, including over TLS, mTLS, Unix, and [`{G}::Server::serve_connection`]."
    );
    let _ = writeln!(src, "    #[must_use]");
    let _ = writeln!(
        src,
        "    pub fn add_optional_service<S: {G}::Service>(self, service: ::core::option::Option<S>) -> {G}::Router {{ self.into_server().add_optional_service(service) }}"
    );
    let _ = writeln!(
        src,
        "    /// Move this service into a [`{G}::Router`], keeping the configuration."
    );
    let _ = writeln!(src, "    #[must_use]");
    let _ = writeln!(
        src,
        "    pub fn into_router(self) -> {G}::Router {{ self.into_server().into_router() }}"
    );
    let _ = writeln!(
        src,
        "    /// Bind `addr` and serve until the listener fails. Applies to every call shape."
    );
    let _ = writeln!(
        src,
        "    pub async fn serve(self, addr: ::std::net::SocketAddr) -> ::core::result::Result<(), {G}::Status> {{ self.into_server().serve(addr).await }}"
    );
    let _ = writeln!(
        src,
        "    /// Serve on an existing listener until it fails. Applies to every call shape."
    );
    let _ = writeln!(
        src,
        "    pub async fn serve_listener(self, listener: {G}::codegen_support::TcpListener) -> ::core::result::Result<(), {G}::Status> {{ self.into_server().serve_listener(listener).await }}"
    );
    let _ = writeln!(
        src,
        "    /// Serve until `shutdown` resolves, then drain. Applies to every call shape. In-flight RPCs finish; new connections are refused. TLS and Unix drain the same way (`serve_tls_with_shutdown`, `serve_unix_with_shutdown`)."
    );
    let _ = writeln!(
        src,
        "    pub async fn serve_with_shutdown(self, listener: {G}::codegen_support::TcpListener, shutdown: impl ::core::future::Future<Output = ()> + Send) -> ::core::result::Result<(), {G}::Status> {{ self.into_server().serve_with_shutdown(listener, shutdown).await }}"
    );
    let _ = writeln!(
        src,
        "    /// Bind `addr` and serve until `shutdown` resolves, then drain. Applies to every call shape."
    );
    let _ = writeln!(
        src,
        "    pub async fn serve_until_shutdown(self, addr: ::std::net::SocketAddr, shutdown: impl ::core::future::Future<Output = ()> + Send) -> ::core::result::Result<(), {G}::Status> {{ self.into_server().serve_until_shutdown(addr, shutdown).await }}"
    );
    let _ = writeln!(
        src,
        "    /// Bind `path` and serve h2c over a Unix domain socket. Applies to every call shape."
    );
    let _ = writeln!(src, "    #[cfg(unix)]");
    let _ = writeln!(
        src,
        "    pub async fn serve_unix(self, path: impl AsRef<::std::path::Path>) -> ::core::result::Result<(), {G}::Status> {{ self.into_server().serve_unix(path).await }}"
    );
    let _ = writeln!(
        src,
        "    /// Bind `path` after unlinking a crash leftover. A live listener is left alone and this fails with UNAVAILABLE. Applies to every call shape."
    );
    let _ = writeln!(src, "    #[cfg(unix)]");
    let _ = writeln!(
        src,
        "    pub async fn serve_unix_unlink(self, path: impl AsRef<::std::path::Path>) -> ::core::result::Result<(), {G}::Status> {{ self.into_server().serve_unix_unlink(path).await }}"
    );
    let _ = writeln!(
        src,
        "    /// Serve h2c on an existing Unix listener. Applies to every call shape."
    );
    let _ = writeln!(src, "    #[cfg(unix)]");
    let _ = writeln!(
        src,
        "    pub async fn serve_unix_listener(self, listener: {G}::codegen_support::UnixListener) -> ::core::result::Result<(), {G}::Status> {{ self.into_server().serve_unix_listener(listener).await }}"
    );
    let _ = writeln!(
        src,
        "    /// Serve h2c on a Unix listener until `shutdown` resolves, then drain. Applies to every call shape. In-flight RPCs finish; new connections are refused. See [`{G}::Server::serve_with_shutdown`]."
    );
    let _ = writeln!(src, "    #[cfg(unix)]");
    let _ = writeln!(
        src,
        "    pub async fn serve_unix_with_shutdown(self, listener: {G}::codegen_support::UnixListener, shutdown: impl ::core::future::Future<Output = ()> + Send) -> ::core::result::Result<(), {G}::Status> {{ self.into_server().serve_unix_with_shutdown(listener, shutdown).await }}"
    );
    let _ = writeln!(
        src,
        "    /// Bind `path` and serve h2c until `shutdown` resolves, then drain. Applies to every call shape."
    );
    let _ = writeln!(src, "    #[cfg(unix)]");
    let _ = writeln!(
        src,
        "    pub async fn serve_unix_until_shutdown(self, path: impl AsRef<::std::path::Path>, shutdown: impl ::core::future::Future<Output = ()> + Send) -> ::core::result::Result<(), {G}::Status> {{ self.into_server().serve_unix_until_shutdown(path, shutdown).await }}"
    );
    let _ = writeln!(
        src,
        "    /// Bind `path` until `shutdown` after unlinking a crash leftover. A live listener is left alone. Applies to every call shape."
    );
    let _ = writeln!(src, "    #[cfg(unix)]");
    let _ = writeln!(
        src,
        "    pub async fn serve_unix_unlink_until_shutdown(self, path: impl AsRef<::std::path::Path>, shutdown: impl ::core::future::Future<Output = ()> + Send) -> ::core::result::Result<(), {G}::Status> {{ self.into_server().serve_unix_unlink_until_shutdown(path, shutdown).await }}"
    );
    let _ = writeln!(
        src,
        "    /// Bind `addr` and serve over TLS until the listener fails. Applies to every call shape."
    );
    let _ = writeln!(
        src,
        "    pub async fn serve_tls(self, addr: ::std::net::SocketAddr, tls: {G}::ServerTls) -> ::core::result::Result<(), {G}::Status> {{ self.into_server().serve_tls(addr, tls).await }}"
    );
    let _ = writeln!(
        src,
        "    /// Serve over TLS until `shutdown` resolves, then drain. Applies to every call shape, including mTLS. In-flight RPCs finish; new connections are refused."
    );
    let _ = writeln!(
        src,
        "    pub async fn serve_tls_with_shutdown(self, listener: {G}::codegen_support::TcpListener, shutdown: impl ::core::future::Future<Output = ()> + Send, tls: {G}::ServerTls) -> ::core::result::Result<(), {G}::Status> {{ self.into_server().serve_tls_with_shutdown(listener, shutdown, tls).await }}"
    );
    let _ = writeln!(
        src,
        "    /// Bind `addr` and serve over TLS until `shutdown` resolves, then drain. Applies to every call shape."
    );
    let _ = writeln!(
        src,
        "    pub async fn serve_tls_until_shutdown(self, addr: ::std::net::SocketAddr, shutdown: impl ::core::future::Future<Output = ()> + Send, tls: {G}::ServerTls) -> ::core::result::Result<(), {G}::Status> {{ self.into_server().serve_tls_until_shutdown(addr, shutdown, tls).await }}"
    );
    let _ = writeln!(
        src,
        "    /// Serve a single already-accepted byte stream until it closes. Applies to every call shape. Generated handlers see empty peer facts on [`{G}::Request`] / [`{G}::Parts`]; [`{G}::Rpc::scheme`] is the peer's `:scheme`. See [`{G}::Server::serve_connection`]."
    );
    let _ = writeln!(
        src,
        "    pub async fn serve_connection<IO>(self, io: IO) -> ::core::result::Result<(), {G}::Status>"
    );
    let _ = writeln!(src, "    where");
    let _ = writeln!(
        src,
        "        IO: {G}::codegen_support::AsyncRead + {G}::codegen_support::AsyncWrite + Unpin + Send + 'static,"
    );
    let _ = writeln!(src, "    {{");
    let _ = writeln!(src, "        self.into_server().serve_connection(io).await");
    let _ = writeln!(src, "    }}");
    let _ = writeln!(
        src,
        "    /// Serve connections from `incoming` until it is exhausted. Applies to every call shape. Override [`{G}::Incoming::peer`] to stamp connection facts."
    );
    let _ = writeln!(
        src,
        "    pub async fn serve_with_incoming<I: {G}::Incoming>(self, incoming: I) -> ::core::result::Result<(), {G}::Status> {{ self.into_server().serve_with_incoming(incoming).await }}"
    );
    let _ = writeln!(
        src,
        "    /// Serve from `incoming` until `shutdown` resolves, then drain. Applies to every call shape."
    );
    let _ = writeln!(
        src,
        "    pub async fn serve_with_incoming_shutdown<I: {G}::Incoming>(self, incoming: I, shutdown: impl ::core::future::Future<Output = ()> + Send) -> ::core::result::Result<(), {G}::Status> {{ self.into_server().serve_with_incoming_shutdown(incoming, shutdown).await }}"
    );
    let _ = writeln!(
        src,
        "    fn into_server(self) -> {G}::Server<Self> {{ let config = self.config; {G}::Server::new(self).config(config) }}"
    );
    let _ = writeln!(src, "}}");

    let _ = writeln!(src, "impl<T: {trait_name}> {G}::Service for {server}<T> {{");
    let _ = writeln!(src, "    const NAME: &'static str = \"{full_name}\";");
    if full_name == "grpc.reflection.v1.ServerReflection" {
        let _ = writeln!(
            src,
            "    /// Path alias for older grpcurl that falls back to `grpc.reflection.v1alpha`."
        );
        let _ = writeln!(
            src,
            "    /// Same handler as [`Self::NAME`]. Not a second proto."
        );
        let _ = writeln!(
            src,
            "    const ALIASES: &'static [&'static str] = &[\"grpc.reflection.v1alpha.ServerReflection\"];"
        );
    }
    let _ = writeln!(
        src,
        "    fn call(&self, rpc: {G}::Rpc) -> impl ::core::future::Future<Output = ()> + Send {{"
    );
    let _ = writeln!(
        src,
        "        let inner = ::std::sync::Arc::clone(&self.inner);"
    );
    let _ = writeln!(src, "        async move {{");
    let _ = writeln!(src, "            match rpc.method() {{");
    for m in &svc.methods {
        let shape = shape_of(m);
        let fn_name = to_snake(&m.name);
        let _ = writeln!(src, "                \"{}\" => {{", m.name);
        let _ = writeln!(
            src,
            "                    rpc.{}(move |request| async move {{ inner.{fn_name}(request).await }}).await;",
            shape.dispatch
        );
        let _ = writeln!(src, "                }}");
    }
    let _ = writeln!(src, "                _ => rpc.unimplemented(),");
    let _ = writeln!(src, "            }}");
    let _ = writeln!(src, "        }}");
    let _ = writeln!(src, "    }}");
    let _ = writeln!(src, "}}");
}

fn emit_kernel_client_dialers(src: &mut String) {
    let _ = writeln!(
        src,
        "    /// Dial `target` (`host:port`, not a tonic `http://` / `https://` URI). See [`{G}::Channel::connect`]. Applies to every call shape."
    );
    let _ = writeln!(
        src,
        "    pub async fn connect(target: impl Into<{G}::Target>) -> ::core::result::Result<Self, {G}::Status> {{"
    );
    let _ = writeln!(
        src,
        "        Ok(Self::new({G}::Channel::connect(target).await?))"
    );
    let _ = writeln!(src, "    }}");
    let _ = writeln!(
        src,
        "    /// [`Self::connect`] with [`{G}::ChannelConfig`]. Applies to every call shape."
    );
    let _ = writeln!(
        src,
        "    pub async fn connect_with(target: impl Into<{G}::Target>, config: {G}::ChannelConfig) -> ::core::result::Result<Self, {G}::Status> {{"
    );
    let _ = writeln!(
        src,
        "        Ok(Self::new({G}::Channel::connect_with(target, config).await?))"
    );
    let _ = writeln!(src, "    }}");
    let _ = writeln!(
        src,
        "    /// Open `connections` slots. See [`{G}::Channel::connect_pool`]. Applies to every call shape."
    );
    let _ = writeln!(
        src,
        "    /// TLS (including mTLS) pooling is [`Self::connect_tls_with`] plus [`{G}::ChannelConfig::connections`]; Unix is [`Self::connect_unix_with`]. [`{G}::Channel::from_io`] cannot pool."
    );
    let _ = writeln!(
        src,
        "    /// A pool larger than the server's [`{G}::Server::max_concurrent_connections`] fails the dial as `UNAVAILABLE`."
    );
    let _ = writeln!(
        src,
        "    pub async fn connect_pool(target: impl Into<{G}::Target>, connections: usize) -> ::core::result::Result<Self, {G}::Status> {{"
    );
    let _ = writeln!(
        src,
        "        Ok(Self::new({G}::Channel::connect_pool(target, connections).await?))"
    );
    let _ = writeln!(src, "    }}");
    let _ = writeln!(
        src,
        "    /// Dial over TLS. See [`{G}::Channel::connect_tls`]. Applies to every call shape."
    );
    let _ = writeln!(
        src,
        "    pub async fn connect_tls(target: impl Into<{G}::Target>, tls: {G}::ClientTls) -> ::core::result::Result<Self, {G}::Status> {{"
    );
    let _ = writeln!(
        src,
        "        Ok(Self::new({G}::Channel::connect_tls(target, tls).await?))"
    );
    let _ = writeln!(src, "    }}");
    let _ = writeln!(
        src,
        "    /// [`Self::connect_tls`] with [`{G}::ChannelConfig`]. Applies to every call shape."
    );
    let _ = writeln!(
        src,
        "    /// [`{G}::ChannelConfig::connections`] opens that many TLS sockets (including mTLS); all must succeed. [`{G}::Channel::from_io`] cannot pool."
    );
    let _ = writeln!(
        src,
        "    pub async fn connect_tls_with(target: impl Into<{G}::Target>, config: {G}::ChannelConfig, tls: {G}::ClientTls) -> ::core::result::Result<Self, {G}::Status> {{"
    );
    let _ = writeln!(
        src,
        "        Ok(Self::new({G}::Channel::connect_tls_with(target, config, tls).await?))"
    );
    let _ = writeln!(src, "    }}");
    let _ = writeln!(
        src,
        "    /// Dial on the first RPC. See [`{G}::Channel::connect_lazy`]. Applies to every call shape."
    );
    let _ = writeln!(
        src,
        "    pub fn connect_lazy(target: impl Into<{G}::Target>) -> ::core::result::Result<Self, {G}::Status> {{"
    );
    let _ = writeln!(
        src,
        "        Ok(Self::new({G}::Channel::connect_lazy(target)?))"
    );
    let _ = writeln!(src, "    }}");
    let _ = writeln!(
        src,
        "    /// [`Self::connect_lazy`] with [`{G}::ChannelConfig`]. Applies to every call shape."
    );
    let _ = writeln!(
        src,
        "    pub fn connect_lazy_with(target: impl Into<{G}::Target>, config: {G}::ChannelConfig) -> ::core::result::Result<Self, {G}::Status> {{"
    );
    let _ = writeln!(
        src,
        "        Ok(Self::new({G}::Channel::connect_lazy_with(target, config)?))"
    );
    let _ = writeln!(src, "    }}");
    let _ = writeln!(
        src,
        "    /// [`Self::connect_lazy`] over TLS. See [`{G}::Channel::connect_tls_lazy`]. Applies to every call shape."
    );
    let _ = writeln!(
        src,
        "    pub fn connect_tls_lazy(target: impl Into<{G}::Target>, tls: {G}::ClientTls) -> ::core::result::Result<Self, {G}::Status> {{"
    );
    let _ = writeln!(
        src,
        "        Ok(Self::new({G}::Channel::connect_tls_lazy(target, tls)?))"
    );
    let _ = writeln!(src, "    }}");
    let _ = writeln!(
        src,
        "    /// [`Self::connect_tls_lazy`] with [`{G}::ChannelConfig`]. Applies to every call shape."
    );
    let _ = writeln!(
        src,
        "    pub fn connect_tls_lazy_with(target: impl Into<{G}::Target>, config: {G}::ChannelConfig, tls: {G}::ClientTls) -> ::core::result::Result<Self, {G}::Status> {{"
    );
    let _ = writeln!(
        src,
        "        Ok(Self::new({G}::Channel::connect_tls_lazy_with(target, config, tls)?))"
    );
    let _ = writeln!(src, "    }}");
    let _ = writeln!(
        src,
        "    /// Dial a Unix domain socket. See [`{G}::Channel::connect_unix`]. Applies to every call shape."
    );
    let _ = writeln!(src, "    #[cfg(unix)]");
    let _ = writeln!(
        src,
        "    pub async fn connect_unix(path: impl AsRef<::std::path::Path>) -> ::core::result::Result<Self, {G}::Status> {{"
    );
    let _ = writeln!(
        src,
        "        Ok(Self::new({G}::Channel::connect_unix(path).await?))"
    );
    let _ = writeln!(src, "    }}");
    let _ = writeln!(
        src,
        "    /// [`Self::connect_unix`] with [`{G}::ChannelConfig`]. Applies to every call shape."
    );
    let _ = writeln!(
        src,
        "    /// [`{G}::ChannelConfig::connections`] opens that many Unix sockets; all must succeed. [`{G}::Channel::from_io`] cannot pool."
    );
    let _ = writeln!(src, "    #[cfg(unix)]");
    let _ = writeln!(
        src,
        "    pub async fn connect_unix_with(path: impl AsRef<::std::path::Path>, config: {G}::ChannelConfig) -> ::core::result::Result<Self, {G}::Status> {{"
    );
    let _ = writeln!(
        src,
        "        Ok(Self::new({G}::Channel::connect_unix_with(path, config).await?))"
    );
    let _ = writeln!(src, "    }}");
    let _ = writeln!(
        src,
        "    /// [`Self::connect_unix`] that dials on the first RPC. Applies to every call shape."
    );
    let _ = writeln!(src, "    #[cfg(unix)]");
    let _ = writeln!(
        src,
        "    pub fn connect_unix_lazy(path: impl AsRef<::std::path::Path>) -> ::core::result::Result<Self, {G}::Status> {{"
    );
    let _ = writeln!(
        src,
        "        Ok(Self::new({G}::Channel::connect_unix_lazy(path)?))"
    );
    let _ = writeln!(src, "    }}");
    let _ = writeln!(
        src,
        "    /// [`Self::connect_unix_lazy`] with [`{G}::ChannelConfig`]. Applies to every call shape."
    );
    let _ = writeln!(src, "    #[cfg(unix)]");
    let _ = writeln!(
        src,
        "    pub fn connect_unix_lazy_with(path: impl AsRef<::std::path::Path>, config: {G}::ChannelConfig) -> ::core::result::Result<Self, {G}::Status> {{"
    );
    let _ = writeln!(
        src,
        "        Ok(Self::new({G}::Channel::connect_unix_lazy_with(path, config)?))"
    );
    let _ = writeln!(src, "    }}");
    let _ = writeln!(
        src,
        "    /// Speak gRPC over an already-connected byte stream. See [`{G}::Channel::from_io`]. Applies to every call shape."
    );
    let _ = writeln!(
        src,
        "    pub async fn from_io<IO>(io: IO, authority: impl Into<{G}::Target>) -> ::core::result::Result<Self, {G}::Status>"
    );
    let _ = writeln!(src, "    where");
    let _ = writeln!(
        src,
        "        IO: {G}::codegen_support::AsyncRead + {G}::codegen_support::AsyncWrite + Unpin + Send + 'static,"
    );
    let _ = writeln!(src, "    {{");
    let _ = writeln!(
        src,
        "        Ok(Self::new({G}::Channel::from_io(io, authority).await?))"
    );
    let _ = writeln!(src, "    }}");
    let _ = writeln!(
        src,
        "    /// [`Self::from_io`] with [`{G}::ChannelConfig`]. Applies to every call shape."
    );
    let _ = writeln!(
        src,
        "    /// [`{G}::ChannelConfig::connections`] is forced to 1: one duplex is one HTTP/2 connection."
    );
    let _ = writeln!(
        src,
        "    pub async fn from_io_with<IO>(io: IO, authority: impl Into<{G}::Target>, config: {G}::ChannelConfig) -> ::core::result::Result<Self, {G}::Status>"
    );
    let _ = writeln!(src, "    where");
    let _ = writeln!(
        src,
        "        IO: {G}::codegen_support::AsyncRead + {G}::codegen_support::AsyncWrite + Unpin + Send + 'static,"
    );
    let _ = writeln!(src, "    {{");
    let _ = writeln!(
        src,
        "        Ok(Self::new({G}::Channel::from_io_with(io, authority, config).await?))"
    );
    let _ = writeln!(src, "    }}");
    let _ = writeln!(
        src,
        "    /// Send `:scheme https` from a [`Self::from_io`] channel. See [`{G}::Channel::https_scheme`]. Applies to every call shape."
    );
    let _ = writeln!(src, "    #[must_use]");
    let _ = writeln!(
        src,
        "    pub fn https_scheme(self) -> Self {{ Self {{ channel: self.channel.https_scheme() }} }}"
    );
    let _ = writeln!(
        src,
        "    /// Override `:authority` on this clone without changing the dial. See [`{G}::Channel::origin`]. Applies to every call shape."
    );
    let _ = writeln!(
        src,
        "    pub fn origin(self, authority: impl Into<{G}::Target>) -> ::core::result::Result<Self, {G}::Status> {{"
    );
    let _ = writeln!(
        src,
        "        Ok(Self {{ channel: self.channel.origin(authority)? }})"
    );
    let _ = writeln!(src, "    }}");
    let _ = writeln!(
        src,
        "    /// HTTP/2 `:scheme` this client sends. See [`{G}::Channel::scheme`]. Applies to every call shape."
    );
    let _ = writeln!(src, "    #[must_use]");
    let _ = writeln!(
        src,
        "    pub fn scheme(&self) -> &'static str {{ self.channel.scheme() }}"
    );
    let _ = writeln!(
        src,
        "    /// The HTTP/2 `:authority` this client sends. See [`{G}::Channel::authority`]. Applies to every call shape."
    );
    let _ = writeln!(src, "    #[must_use]");
    let _ = writeln!(
        src,
        "    pub fn authority(&self) -> &str {{ self.channel.authority() }}"
    );
    let _ = writeln!(
        src,
        "    /// The `user-agent` this client sends. See [`{G}::Channel::grpc_user_agent`]. Applies to every call shape."
    );
    let _ = writeln!(src, "    #[must_use]");
    let _ = writeln!(
        src,
        "    pub fn grpc_user_agent(&self) -> &str {{ self.channel.grpc_user_agent() }}"
    );
    let _ = writeln!(
        src,
        "    /// The configuration in effect. See [`{G}::Channel::config`]."
    );
    let _ = writeln!(src, "    #[must_use]");
    let _ = writeln!(
        src,
        "    pub fn config(&self) -> {G}::ChannelConfig {{ self.channel.config() }}"
    );
    let _ = writeln!(
        src,
        "    /// Whether any pool slot currently holds a live HTTP/2 connection. Distinct from gRPC `GetState`. See [`{G}::Channel::connected`]. Applies to every call shape. Client interceptors see the same snapshot as [`{G}::Outgoing::connected`]. Distinct from [`Self::wait_for_ready`]: that overlay queues; this getter is a live snapshot."
    );
    let _ = writeln!(src, "    #[must_use]");
    let _ = writeln!(
        src,
        "    pub fn connected(&self) -> bool {{ self.channel.connected() }}"
    );
    let _ = writeln!(
        src,
        "    /// Default per-RPC deadline when the request omits one. Distinct from [`Self::timeout`], which sets it. See [`{G}::Channel::rpc_timeout`]. Applies to every call shape."
    );
    let _ = writeln!(src, "    #[must_use]");
    let _ = writeln!(
        src,
        "    pub fn rpc_timeout(&self) -> ::core::option::Option<::std::time::Duration> {{ self.channel.rpc_timeout() }}"
    );
    let _ = writeln!(
        src,
        "    /// Whether this client waits for a connection instead of failing fast. See [`{G}::Channel::waits_for_ready`]. Applies to every call shape. Distinct from [`Self::connected`]: that getter is a live snapshot, not this overlay."
    );
    let _ = writeln!(src, "    #[must_use]");
    let _ = writeln!(
        src,
        "    pub fn waits_for_ready(&self) -> bool {{ self.channel.waits_for_ready() }}"
    );
    let _ = writeln!(
        src,
        "    /// Whether this client gzips outbound payloads. See [`{G}::Channel::compresses_outbound`]. Applies to every call shape."
    );
    let _ = writeln!(src, "    #[must_use]");
    let _ = writeln!(
        src,
        "    pub fn compresses_outbound(&self) -> bool {{ self.channel.compresses_outbound() }}"
    );
    let _ = writeln!(
        src,
        "    /// Configured outbound gzip deflate level. Distinct from [`Self::gzip_compression_level`], which sets it. See [`{G}::Channel::gzip_level`]. Applies to every call shape."
    );
    let _ = writeln!(src, "    #[must_use]");
    let _ = writeln!(
        src,
        "    pub fn gzip_level(&self) -> u32 {{ self.channel.gzip_level() }}"
    );
    let _ = writeln!(
        src,
        "    /// Whether inbound gzip is inflated. Default `true`. Distinct from [`Self::accept_compressed`], which sets it. Distinct from [`{G}::Rpc::accepts_gzip`], which is the peer's `grpc-accept-encoding`. See [`{G}::Channel::accepts_compressed`]. Applies to every call shape."
    );
    let _ = writeln!(src, "    #[must_use]");
    let _ = writeln!(
        src,
        "    pub fn accepts_compressed(&self) -> bool {{ self.channel.accepts_compressed() }}"
    );
    let _ = writeln!(
        src,
        "    /// Configured channel-wide RPC cap, if any. Distinct from [`Self::max_concurrent_rpcs`], which sets it. See [`{G}::Channel::concurrent_rpc_limit`]. Applies to every call shape."
    );
    let _ = writeln!(src, "    #[must_use]");
    let _ = writeln!(
        src,
        "    pub fn concurrent_rpc_limit(&self) -> ::core::option::Option<usize> {{ self.channel.concurrent_rpc_limit() }}"
    );
    let _ = writeln!(
        src,
        "    /// Configured outbound streaming queue depth. Distinct from [`Self::stream_buffer`], which sets it. Distinct from [`Self::message_limits`]: that is message size, not queue depth. See [`{G}::Channel::stream_buffer_size`]. Applies to client-streaming and bidi request streams."
    );
    let _ = writeln!(src, "    #[must_use]");
    let _ = writeln!(
        src,
        "    pub fn stream_buffer_size(&self) -> usize {{ self.channel.stream_buffer_size() }}"
    );
    let _ = writeln!(
        src,
        "    /// Configured write-time HTTP/2 send buffer. Distinct from [`Self::max_send_buffer_size`], which sets it. Distinct from [`Self::stream_buffer_size`]: that is queue depth, not this send buffer. See [`{G}::Channel::send_buffer_size`]. Applies to every call shape."
    );
    let _ = writeln!(src, "    #[must_use]");
    let _ = writeln!(
        src,
        "    pub fn send_buffer_size(&self) -> usize {{ self.channel.send_buffer_size() }}"
    );
    let _ = writeln!(
        src,
        "    /// Configured message caps. Distinct from [`Self::message_limits`], which sets them. Distinct from [`Self::stream_buffer_size`]: that is queue depth, not uncompressed protobuf bytes. Distinct from [`Self::send_buffer_size`]: that is the HTTP/2 send buffer, not these caps. See [`{G}::Channel::limits`]. Applies to every call shape."
    );
    let _ = writeln!(src, "    #[must_use]");
    let _ = writeln!(
        src,
        "    pub fn limits(&self) -> {G}::MessageLimits {{ self.channel.limits() }}"
    );
}

fn emit_kernel_client(
    src: &mut String,
    trait_name: &str,
    client: &str,
    full_name: &str,
    svc: &ServiceDescriptor,
) {
    let _ = writeln!(src, "/// Client for `{full_name}`.");
    let _ = writeln!(src, "///");
    let _ = writeln!(
        src,
        "/// Wraps a [`{G}::Channel`]; cloning is cheap and shares connections."
    );
    let _ = writeln!(
        src,
        "/// Dial with [`Self::connect`], [`Self::connect_tls`], or [`Self::from_io`]."
    );
    let _ = writeln!(
        src,
        "/// On Unix, `connect_unix` takes a filesystem path. Wrap an existing channel with [`Self::new`]."
    );
    let _ = writeln!(
        src,
        "/// [`Self::authority`], [`Self::scheme`], and [`Self::grpc_user_agent`] read the same values interceptors see on [`{G}::Outgoing`]. [`Self::config`] is the channel overlay those values come from. [`Self::rpc_timeout`], [`Self::waits_for_ready`], [`Self::compresses_outbound`], [`Self::gzip_level`], [`Self::accepts_compressed`], [`Self::concurrent_rpc_limit`], [`Self::stream_buffer_size`], [`Self::send_buffer_size`], and [`Self::limits`] read that overlay without colliding with the setters."
    );
    let _ = writeln!(
        src,
        "/// [`Self::connected`] is the live-socket snapshot. Distinct from [`Self::waits_for_ready`]. Same snapshot as [`{G}::Channel::connected`]."
    );
    let _ = writeln!(src, "#[derive(::core::clone::Clone)]");
    let _ = writeln!(src, "pub struct {client} {{");
    let _ = writeln!(src, "    channel: {G}::Channel,");
    let _ = writeln!(src, "}}");
    let _ = writeln!(src, "impl ::core::fmt::Debug for {client} {{");
    let _ = writeln!(
        src,
        "    fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {{"
    );
    let _ = writeln!(
        src,
        "        f.debug_struct(\"{client}\").field(\"channel\", &self.channel).finish()"
    );
    let _ = writeln!(src, "    }}");
    let _ = writeln!(src, "}}");
    let _ = writeln!(src, "impl {client} {{");
    let _ = writeln!(src, "    /// Fully qualified proto service name.");
    let _ = writeln!(src, "    pub const NAME: &'static str = \"{full_name}\";");
    let _ = writeln!(src, "    /// Wrap a channel.");
    let _ = writeln!(src, "    #[must_use]");
    let _ = writeln!(
        src,
        "    pub fn new(channel: {G}::Channel) -> Self {{ Self {{ channel }} }}"
    );
    let _ = writeln!(src, "    /// Take the channel back.");
    let _ = writeln!(src, "    #[must_use]");
    let _ = writeln!(
        src,
        "    pub fn into_inner(self) -> {G}::Channel {{ self.channel }}"
    );
    emit_kernel_client_dialers(src);
    let _ = writeln!(
        src,
        "    /// Cap inbound messages at `limit` bytes. Default 4 MiB. Applies to every call shape, including over TLS, mTLS, Unix, and [`{G}::Channel::from_io`]."
    );
    let _ = writeln!(src, "    #[must_use]");
    let _ = writeln!(
        src,
        "    pub fn max_decoding_message_size(mut self, limit: usize) -> Self {{ self.channel = self.channel.max_decoding_message_size(limit); self }}"
    );
    let _ = writeln!(
        src,
        "    /// Cap outbound messages at `limit` bytes. Default unlimited. Applies to every call shape, including over TLS, mTLS, Unix, and [`{G}::Channel::from_io`]."
    );
    let _ = writeln!(src, "    #[must_use]");
    let _ = writeln!(
        src,
        "    pub fn max_encoding_message_size(mut self, limit: usize) -> Self {{ self.channel = self.channel.max_encoding_message_size(limit); self }}"
    );
    let _ = writeln!(
        src,
        "    /// Replace both message caps at once. Applies to every call shape. See [`{G}::Channel::message_limits`]."
    );
    let _ = writeln!(
        src,
        "    /// Distinct from [`Self::max_encoding_message_size`] / [`Self::max_decoding_message_size`]. Oversize is `RESOURCE_EXHAUSTED` on every call shape, including over TLS, mTLS, Unix, and [`{G}::Channel::from_io`]."
    );
    let _ = writeln!(src, "    #[must_use]");
    let _ = writeln!(
        src,
        "    pub fn message_limits(mut self, limits: {G}::MessageLimits) -> Self {{ self.channel = self.channel.message_limits(limits); self }}"
    );
    let _ = writeln!(
        src,
        "    /// Run `interceptor` on every outbound RPC before the stream opens. Invoked when the generated method is called, not on first poll of the [`{G}::Call`]. `Err` fails that Call on poll, including [`{G}::Status::with_error_details`]; nothing is sent. A local [`{G}::Status::with_error_details`] is [`{G}::Status::rpc`] / [`{G}::Status::error_details`] on that Call for every call shape. [`{G}::Outgoing::set_timeout`] is that Call's deadline on every call shape. `clear_timeout` opts out of the channel timeout on every call shape. `clear_compress` then `set_compress(compresses_outbound())` reapplies channel gzip on every call shape. `set_compress` stamps [`{G}::StreamSender::compress`] on client-streaming and bidi request streams. Outgoing getters apply to every call shape. `clear_user_agent` restores the channel user-agent after a generated intercept prefix."
    );
    let _ = writeln!(
        src,
        "    /// The interceptor sees a [`{G}::Outgoing`]: path, service, method, `:authority`, `:scheme`,"
    );
    let _ = writeln!(
        src,
        "    /// `user-agent` (`user_agent_is_set`), message caps, metadata, timeout / deadline Instant, wait-for-ready (`wait_for_ready_is_set`), compression (`compress_is_set`), channel overlays (`rpc_timeout` / `waits_for_ready` / `compresses_outbound` / `gzip_level` / `accepts_compressed` / `concurrent_rpc_limit` / `stream_buffer_size` / `send_buffer_size` / `limits`; `clear_*` opts out of the already-applied default), extensions, `connected` (same snapshot as [`{G}::Channel::connected`]). [`{G}::Outgoing::set_user_agent`] prefixes this RPC. [`{G}::Request::set_user_agent`] is the same prefix at the call site. [`{G}::Outgoing::connected`] is the live-socket snapshot. Distinct from wait-for-ready: a lazy first RPC sees `false` even when that overlay is on. [`{G}::Outgoing::gzip_level`] is deflate effort. Distinct from [`{G}::Outgoing::compresses_outbound`] (on or off). An interceptor cannot change it. [`{G}::Outgoing::accepts_compressed`] is the inbound gzip overlay (default on). [`{G}::Outgoing::limits`] is the channel message-cap overlay. Same overlay as [`{G}::Channel::limits`]. [`{G}::Outgoing::concurrent_rpc_limit`] is the channel RPC cap overlay. Distinct from [`{G}::Outgoing::waits_for_ready`]: that waits for a connection; this refuses extras. [`{G}::Outgoing::stream_buffer_size`] is the outbound streaming queue overlay. Distinct from [`{G}::Outgoing::limits`]: that is message size, not queue depth. [`{G}::Outgoing::send_buffer_size`] is the outbound HTTP/2 send buffer overlay. Distinct from [`{G}::Outgoing::stream_buffer_size`]: that is queue depth, not this send buffer."
    );
    let _ = writeln!(
        src,
        "    /// Compiling overlay dumps live on [`{G}::hello`] (`GreeterClient::new(channel).intercept`)."
    );
    let _ = writeln!(
        src,
        "    /// [`{G}::Outgoing::user_agent_is_set`] is occupancy on this generated intercept path, so a later interceptor can prefix only when unset."
    );
    let _ = writeln!(
        src,
        "    /// [`{G}::Outgoing::wait_for_ready_is_set`] is occupancy on this generated intercept path, so a later interceptor can fill wait-for-ready only when unset."
    );
    let _ = writeln!(
        src,
        "    /// [`{G}::Outgoing::compress_is_set`] is occupancy on this generated intercept path, so a later interceptor can fill compress only when unset."
    );
    let _ = writeln!(
        src,
        "    /// `clear_wait_for_ready` restores the channel wait-for-ready overlay after a generated intercept choice."
    );
    let _ = writeln!(
        src,
        "    /// `clear_timeout` opts out of the channel timeout after a generated intercept choice."
    );
    let _ = writeln!(
        src,
        "    /// `clear_compress` then `set_compress` from `compresses_outbound` reapplies channel gzip after a generated intercept choice."
    );
    let _ = writeln!(
        src,
        "    /// [`{G}::Outgoing::connected`] is the live-socket snapshot on this generated intercept path ([`{G}::Channel::connected`]), taken when the interceptor runs. Distinct from wait-for-ready: a lazy first RPC sees `false` even when that overlay is on."
    );
    let _ = writeln!(
        src,
        "    /// [`{G}::Status::from_error_details`] is the typed bag after a generated intercept Err; a local reject never opens a stream."
    );
    let _ = writeln!(
        src,
        "    /// Distinct from a generated handler Err: that is after the handler ran; this generated intercept Err is a local reject never opens a stream."
    );
    let _ = writeln!(
        src,
        "    /// Distinct from a generated client on_response Err: that fails the Call after a successful receive; this generated intercept Err is a local reject never opens a stream."
    );
    let _ = writeln!(
        src,
        "    /// Distinct from a generated server intercept Err: that is trailers without reading the body; this generated intercept Err is a local reject never opens a stream."
    );
    let _ = writeln!(
        src,
        "    /// Distinct from a generated StreamSender fail: that is trailers after any messages already sent; this generated intercept Err is a local reject never opens a stream."
    );
    let _ = writeln!(
        src,
        "    /// Distinct from [`{G}::Channel::max_concurrent_rpcs`]: that takes a slot when the [`{G}::Call`] is polled; a generated intercept already ran, so a local Err never consumes that budget."
    );
    let _ = writeln!(
        src,
        "    /// Distinct from [`{G}::Server::intercept`]: that runs on the inbound RPC before the handler; this generated intercept runs on the outbound call before the stream opens."
    );
    let _ = writeln!(
        src,
        "    /// Distinct from [`Self::on_response`]: that runs after a successful receive; this generated intercept runs on the outbound call before the stream opens."
    );
    let _ = writeln!(src, "    #[must_use]");
    let _ = writeln!(src, "    pub fn intercept<I>(self, interceptor: I) -> Self");
    let _ = writeln!(src, "    where");
    let _ = writeln!(src, "        I: {G}::ClientInterceptor,");
    let _ = writeln!(src, "    {{");
    let _ = writeln!(
        src,
        "        Self {{ channel: self.channel.intercept(interceptor) }}"
    );
    let _ = writeln!(src, "    }}");
    let _ = writeln!(
        src,
        "    /// Run `interceptor` after a successful receive, before the [`{G}::Call`] is Ready. Closures implement [`{G}::ResponseInterceptor`]. A received reply starts empty; this is how a client inserts typed context after the peer cannot. Distinct from [`Self::intercept`], which runs before the stream opens. Calling this twice stacks: the first interceptor runs first. Applies to every call shape, including over TLS, mTLS, Unix, and [`{G}::Channel::from_io`]. `Err` fails that Call (the peer already sent OK), including [`{G}::Status::with_error_details`]. A non-OK peer status skips this hook. On server-streaming and bidi, trailers on this envelope do not replace [`{G}::Streaming::trailers`]. See [`{G}::Channel::on_response`]. [`{G}::ResponseParts::path`] is kernel-stamped. Distinct from [`{G}::Outgoing::path`]. [`{G}::Response::gzip_level`] on a received reply is not the peer's deflate effort. Distinct from [`{G}::Response::encoding`]. [`{G}::Response::compresses_outbound`] on a received reply is `false` (the overlay is not on the wire). [`{G}::Response::accepts_gzip`] on a received reply is `false` (the advertisement is not on the reply wire). [`{G}::Response::deadline`] on a received reply is `None` (the peer deadline is not on the wire). [`{G}::Response::timeout`] on a received reply is `None` (the peer timeout is not on the reply wire). [`{G}::Response::limits`] on a received reply is `None` (the peer encode cap is not on the wire). [`{G}::Response::peer_timeout`] on a received reply is `None` (the client's `grpc-timeout` is not on the reply wire). [`{G}::Response::rpc_timeout`] on a received reply is `None` (the server overlay is not on the reply wire). [`{G}::Response::accepts_compressed`] on a received reply is `false` (this overlay is not a received-reply field). [`{G}::Response::send_buffer_size`] on a received reply is `None` (the peer send buffer is not on the reply wire)."
    );
    let _ = writeln!(
        src,
        "    /// [`{G}::ResponseParts::compress_is_set`] is occupancy after a generated client on_response, so a later interceptor can fill compress only when unset."
    );
    let _ = writeln!(
        src,
        "    /// [`{G}::ResponseParts::clear_compress`] drops a compress choice after a generated client on_response; a received reply has no server gzip overlay to restore."
    );
    let _ = writeln!(
        src,
        "    /// [`{G}::Status::from_error_details`] is the typed bag after a generated client on_response Err; a local reject fails the Call after a successful receive."
    );
    let _ = writeln!(
        src,
        "    /// Distinct from a generated handler Err: that is after the handler ran; this generated client on_response Err fails the Call after a successful receive."
    );
    let _ = writeln!(
        src,
        "    /// Distinct from a generated server intercept Err: that is trailers without reading the body; this generated client on_response Err fails the Call after a successful receive."
    );
    let _ = writeln!(
        src,
        "    /// Distinct from a generated intercept Err: that is a local reject never opens a stream; this generated client on_response Err fails the Call after a successful receive."
    );
    let _ = writeln!(
        src,
        "    /// Distinct from a generated server on_response Err: that is trailers-only after handler Ok; this generated client on_response Err fails the Call after a successful receive."
    );
    let _ = writeln!(
        src,
        "    /// Distinct from a generated StreamSender fail: that is trailers after any messages already sent; this generated client on_response Err fails the Call after a successful receive."
    );
    let _ = writeln!(
        src,
        "    /// Distinct from [`Self::intercept`]: that runs on the outbound call before the stream opens; this generated client on_response runs after a successful receive."
    );
    let _ = writeln!(
        src,
        "    /// Compiling overlay dumps live on [`{G}::hello`] (`GreeterClient::new(channel).on_response`)."
    );
    let _ = writeln!(src, "    #[must_use]");
    let _ = writeln!(
        src,
        "    pub fn on_response<I>(self, interceptor: I) -> Self"
    );
    let _ = writeln!(src, "    where");
    let _ = writeln!(src, "        I: {G}::ResponseInterceptor,");
    let _ = writeln!(src, "    {{");
    let _ = writeln!(
        src,
        "        Self {{ channel: self.channel.on_response(interceptor) }}"
    );
    let _ = writeln!(src, "    }}");
    let _ = writeln!(
        src,
        "    /// Prefix the kernel `user-agent`. See [`{G}::Channel::user_agent`]. Applies to every call shape."
    );
    let _ = writeln!(
        src,
        "    pub fn user_agent(self, prefix: impl AsRef<str>) -> ::core::result::Result<Self, {G}::Status> {{"
    );
    let _ = writeln!(
        src,
        "        Ok(Self {{ channel: self.channel.user_agent(prefix)? }})"
    );
    let _ = writeln!(src, "    }}");
    let _ = writeln!(
        src,
        "    /// gzip unary and server-streaming request payloads and [`{G}::StreamSender::send`]. Applies to every call shape, including over TLS, mTLS, Unix, and [`{G}::Channel::from_io`]. See [`{G}::Channel::send_compressed`]."
    );
    let _ = writeln!(src, "    #[must_use]");
    let _ = writeln!(
        src,
        "    pub fn send_compressed(mut self) -> Self {{ self.channel = self.channel.send_compressed(); self }}"
    );
    let _ = writeln!(
        src,
        "    /// Deflate effort for outbound gzip. Default 1 (`flate2` fast). Applies to every call shape, including over TLS, mTLS, Unix, and [`{G}::Channel::from_io`]. Distinct from [`Self::send_compressed`], which is on or off. 0 stores; 9 is best. See [`{G}::Channel::gzip_compression_level`]."
    );
    let _ = writeln!(src, "    #[must_use]");
    let _ = writeln!(
        src,
        "    pub fn gzip_compression_level(mut self, level: u32) -> Self {{ self.channel = self.channel.gzip_compression_level(level); self }}"
    );
    let _ = writeln!(
        src,
        "    /// Inflate inbound gzip. Default `true`. Applies to every call shape, including over TLS, mTLS, Unix, and [`{G}::Channel::from_io`]. Passing `false` omits gzip from `grpc-accept-encoding` and refuses a gzip reply as `UNIMPLEMENTED`. Distinct from [`Self::send_compressed`], which is outbound. See [`{G}::Channel::accept_compressed`]."
    );
    let _ = writeln!(src, "    #[must_use]");
    let _ = writeln!(
        src,
        "    pub fn accept_compressed(mut self, accept: bool) -> Self {{ self.channel = self.channel.accept_compressed(accept); self }}"
    );
    let _ = writeln!(
        src,
        "    /// Default per-RPC deadline when the request omits one. See [`{G}::Channel::timeout`]. Applies to every call shape, including over TLS, mTLS, Unix, and [`{G}::Channel::from_io`]. Interceptors run after this fill and can still set or [`{G}::Outgoing::clear_timeout`]."
    );
    let _ = writeln!(src, "    #[must_use]");
    let _ = writeln!(
        src,
        "    pub fn timeout(mut self, timeout: ::std::time::Duration) -> Self {{ self.channel = self.channel.timeout(timeout); self }}"
    );
    let _ = writeln!(
        src,
        "    /// Wait for a connection instead of failing fast. See [`{G}::Channel::wait_for_ready`]. Applies to every call shape."
    );
    let _ = writeln!(
        src,
        "    /// A request that already called [`{G}::Request::set_wait_for_ready`] is left alone. Interceptors run after this fill and can still set or clear it."
    );
    let _ = writeln!(src, "    #[must_use]");
    let _ = writeln!(
        src,
        "    pub fn wait_for_ready(mut self) -> Self {{ self.channel = self.channel.wait_for_ready(); self }}"
    );
    let _ = writeln!(
        src,
        "    /// How many messages sit between a client-streaming caller and the wire. See [`{G}::Channel::stream_buffer`]. Applies to client-streaming and bidi request streams."
    );
    let _ = writeln!(src, "    #[must_use]");
    let _ = writeln!(
        src,
        "    pub fn stream_buffer(mut self, messages: usize) -> Self {{ self.channel = self.channel.stream_buffer(messages); self }}"
    );
    let _ = writeln!(
        src,
        "    /// Write-time HTTP/2 send buffer threshold for outbound DATA. See [`{G}::Channel::max_send_buffer_size`]. Applies to every call shape, including over TLS, mTLS, Unix, and [`{G}::Channel::from_io`]. Overlay: does not change how a dead slot is redialed."
    );
    let _ = writeln!(src, "    #[must_use]");
    let _ = writeln!(
        src,
        "    pub fn max_send_buffer_size(mut self, bytes: usize) -> Self {{ self.channel = self.channel.max_send_buffer_size(bytes); self }}"
    );
    let _ = writeln!(
        src,
        "    /// Cap how many RPCs this channel will run at once. Applies to every call shape, including over TLS, mTLS, Unix, and [`{G}::Channel::from_io`]. Extra RPCs are `RESOURCE_EXHAUSTED` before the stream opens. Distinct from HTTP/2 `SETTINGS_MAX_CONCURRENT_STREAMS`, which waits. Distinct from [`{G}::Server::max_concurrent_rpcs`], which refuses inbound. Clones share the budget. See [`{G}::Channel::max_concurrent_rpcs`]."
    );
    let _ = writeln!(src, "    #[must_use]");
    let _ = writeln!(
        src,
        "    pub fn max_concurrent_rpcs(mut self, n: usize) -> Self {{ self.channel = self.channel.max_concurrent_rpcs(n); self }}"
    );
    let _ = writeln!(src, "    /// The channel this client sends on.");
    let _ = writeln!(src, "    #[must_use]");
    let _ = writeln!(
        src,
        "    pub fn channel(&self) -> &{G}::Channel {{ &self.channel }}"
    );
    for m in &svc.methods {
        let shape = shape_of(m);
        let fn_name = to_snake(&m.name);
        let path = format!("/{full_name}/{}", m.name);
        let _ = writeln!(src, "    /// {} `{}`.", shape.description, m.name);
        let _ = writeln!(
            src,
            "    /// Interceptors run when this method is called, not on first poll of the [`{G}::Call`]."
        );
        let _ = writeln!(
            src,
            "    /// The reply's [`{G}::Response::encoding`] is the peer's `grpc-encoding` (`None` for identity). The [`{G}::Call`] is fused after it resolves."
        );
        if !m.client_streaming && !m.server_streaming {
            let _ = writeln!(
                src,
                "    /// A [`{G}::CallHandle`] taken before await cancels the RPC."
            );
            let _ = writeln!(
                src,
                "    /// OK-path custom trailers land on [`{G}::Response::trailers`]; a `-bin` trailer must not appear as a header, including over TLS, mTLS, Unix, and [`{G}::Channel::from_io`]."
            );
        }
        if m.server_streaming {
            let _ = writeln!(
                src,
                "    /// Dropping the received [`{G}::Streaming`] before the end resets the RPC."
            );
            let _ = writeln!(
                src,
                "    /// After end-of-stream or error that stream is fused (`{G}::FusedStream`)."
            );
            let _ = writeln!(
                src,
                "    /// A [`{G}::CallHandle`] taken before await still cancels while waiting for headers, and still cancels that live stream after headers."
            );
            let _ = writeln!(
                src,
                "    /// Letting the deadline fire RSTs the send half before headers and after headers the same way."
            );
            let _ = writeln!(
                src,
                "    /// [`{G}::Streaming::trailers`] waits for end-of-stream, including when called before draining messages. A non-OK trailing `grpc-status` is `Err`. A `-bin` trailer must not appear as a header, including over TLS, mTLS, Unix, and [`{G}::Channel::from_io`]."
            );
        }
        if m.client_streaming && !m.server_streaming {
            let _ = writeln!(
                src,
                "    /// A [`{G}::CallHandle`] taken before await still cancels after the sender is closed, while the unary response is pending."
            );
            let _ = writeln!(
                src,
                "    /// Cancelling before any request message (`cancel_after_begin`) is [`{G}::Code::Cancelled`], not OK from a half-close: hold the [`{G}::StreamSender`] until the [`{G}::Call`] settles, including over TLS, mTLS, Unix, and [`{G}::Channel::from_io`]."
            );
            let _ = writeln!(
                src,
                "    /// Dropping the [`{G}::Call`] or letting its deadline fire after that half-close resets the same way."
            );
            let _ = writeln!(
                src,
                "    /// OK-path custom trailers land on [`{G}::Response::trailers`]; a `-bin` trailer must not appear as a header, including over TLS, mTLS, Unix, and [`{G}::Channel::from_io`]."
            );
            let _ = writeln!(
                src,
                "    /// [`{G}::StreamSender::fail`] resolves the [`{G}::Call`] with that status (no request-side `grpc-status`; the stream is reset with CANCEL)."
            );
        }
        if m.client_streaming && m.server_streaming {
            let _ = writeln!(
                src,
                "    /// [`{G}::StreamSender::fail`] before headers resolves the [`{G}::Call`] with that status, not `UNAVAILABLE` from the reset; after headers the received [`{G}::Streaming`] sees [`{G}::Code::Cancelled`], not that status."
            );
        }
        if shape.client_return.starts_with('(') {
            let _ = writeln!(
                src,
                "    /// Dropping the pair without awaiting the [`{G}::Call`] resets the stream."
            );
            let _ = writeln!(
                src,
                "    #[must_use = \"dropping a streaming Call resets the stream\"]"
            );
        }
        let _ = writeln!(
            src,
            "    pub fn {fn_name}(&self, request: {}) -> {} {{",
            shape.client_arg, shape.client_return
        );
        let _ = writeln!(
            src,
            "        self.channel.{}(\"{path}\", request)",
            shape.client_call
        );
        let _ = writeln!(src, "    }}");
    }
    let _ = writeln!(src, "}}");
    let _ = writeln!(
        src,
        "impl ::core::convert::From<{G}::Channel> for {client} {{"
    );
    let _ = writeln!(
        src,
        "    fn from(channel: {G}::Channel) -> Self {{ Self::new(channel) }}"
    );
    let _ = writeln!(src, "}}");
    let _ = writeln!(src, "// `{trait_name}` is the server-side counterpart.");
}

fn emit_service(src: &mut String, svc: &ServiceDescriptor) {
    let svc_ty = rust_ident(&svc.full_name);
    let client = format!("{svc_ty}Client");
    let server = format!("{svc_ty}Server");
    let path_prefix = format!("/{}", svc.full_name);
    let _ = writeln!(src, "pub trait {svc_ty}: Send + Sync + 'static {{");
    for m in &svc.methods {
        emit_service_trait_method(src, m);
    }
    let _ = writeln!(src, "}}");
    let _ = writeln!(src, "#[derive(Clone, Debug)]");
    let _ = writeln!(src, "pub struct {client}<T> {{");
    let _ = writeln!(src, "    inner: tonic::client::Grpc<T>,");
    let _ = writeln!(src, "}}");
    let _ = writeln!(src, "impl<T> {client}<T>");
    let _ = writeln!(src, "where");
    let _ = writeln!(src, "    T: tonic::client::GrpcService<tonic::body::Body>,");
    let _ = writeln!(src, "    T::Error: Into<tonic::codegen::StdError>,");
    let _ = writeln!(
        src,
        "    T::ResponseBody: tonic::codegen::Body<Data = tonic::codegen::Bytes> + Send + 'static,"
    );
    let _ = writeln!(
        src,
        "    <T::ResponseBody as tonic::codegen::Body>::Error: Into<tonic::codegen::StdError> + Send,"
    );
    let _ = writeln!(src, "{{");
    let _ = writeln!(
        src,
        "    pub fn new(inner: T) -> Self {{ Self {{ inner: tonic::client::Grpc::new(inner) }} }}"
    );
    let _ = writeln!(
        src,
        "    pub fn with_interceptor<F>(inner: T, interceptor: F) -> {client}<tonic::service::interceptor::InterceptedService<T, F>>"
    );
    let _ = writeln!(src, "    where");
    let _ = writeln!(src, "        F: tonic::service::Interceptor,");
    let _ = writeln!(src, "        T::ResponseBody: Default,");
    let _ = writeln!(src, "        T: tonic::codegen::Service<http::Request<tonic::body::Body>, Response = http::Response<<T as tonic::client::GrpcService<tonic::body::Body>>::ResponseBody>>,");
    let _ = writeln!(
        src,
        "        <T as tonic::codegen::Service<http::Request<tonic::body::Body>>>::Error: Into<tonic::codegen::StdError> + Send + Sync,"
    );
    let _ = writeln!(src, "    {{");
    let _ = writeln!(
        src,
        "        {client}::new(tonic::service::interceptor::InterceptedService::new(inner, interceptor))"
    );
    let _ = writeln!(src, "    }}");
    let _ = writeln!(
        src,
        "    pub fn send_compressed(mut self, encoding: tonic::codec::CompressionEncoding) -> Self {{ self.inner = self.inner.send_compressed(encoding); self }}"
    );
    let _ = writeln!(
        src,
        "    pub fn accept_compressed(mut self, encoding: tonic::codec::CompressionEncoding) -> Self {{ self.inner = self.inner.accept_compressed(encoding); self }}"
    );
    let _ = writeln!(
        src,
        "    pub fn max_decoding_message_size(mut self, limit: usize) -> Self {{ self.inner = self.inner.max_decoding_message_size(limit); self }}"
    );
    let _ = writeln!(
        src,
        "    pub fn max_encoding_message_size(mut self, limit: usize) -> Self {{ self.inner = self.inner.max_encoding_message_size(limit); self }}"
    );
    for m in &svc.methods {
        emit_client_method(src, m, &path_prefix);
    }
    let _ = writeln!(src, "}}");
    let _ = writeln!(src, "pub struct {server}<T> {{");
    let _ = writeln!(src, "    inner: Arc<T>,");
    let _ = writeln!(
        src,
        "    accept_compression_encodings: tonic::codec::EnabledCompressionEncodings,"
    );
    let _ = writeln!(
        src,
        "    send_compression_encodings: tonic::codec::EnabledCompressionEncodings,"
    );
    let _ = writeln!(src, "    max_decoding_message_size: Option<usize>,");
    let _ = writeln!(src, "    max_encoding_message_size: Option<usize>,");
    let _ = writeln!(src, "}}");
    let _ = writeln!(src, "impl<T> Clone for {server}<T> {{");
    let _ = writeln!(src, "    fn clone(&self) -> Self {{");
    let _ = writeln!(src, "        Self {{");
    let _ = writeln!(src, "            inner: self.inner.clone(),");
    let _ = writeln!(
        src,
        "            accept_compression_encodings: self.accept_compression_encodings,"
    );
    let _ = writeln!(
        src,
        "            send_compression_encodings: self.send_compression_encodings,"
    );
    let _ = writeln!(
        src,
        "            max_decoding_message_size: self.max_decoding_message_size,"
    );
    let _ = writeln!(
        src,
        "            max_encoding_message_size: self.max_encoding_message_size,"
    );
    let _ = writeln!(src, "        }}");
    let _ = writeln!(src, "    }}");
    let _ = writeln!(src, "}}");
    let _ = writeln!(src, "impl<T: {svc_ty}> {server}<T> {{");
    let _ = writeln!(src, "    pub fn new(inner: T) -> Self {{");
    let _ = writeln!(src, "        Self {{");
    let _ = writeln!(src, "            inner: Arc::new(inner),");
    let _ = writeln!(
        src,
        "            accept_compression_encodings: Default::default(),"
    );
    let _ = writeln!(
        src,
        "            send_compression_encodings: Default::default(),"
    );
    let _ = writeln!(src, "            max_decoding_message_size: None,");
    let _ = writeln!(src, "            max_encoding_message_size: None,");
    let _ = writeln!(src, "        }}");
    let _ = writeln!(src, "    }}");
    let _ = writeln!(
        src,
        "    pub fn with_interceptor<F>(inner: T, interceptor: F) -> tonic::service::interceptor::InterceptedService<Self, F>"
    );
    let _ = writeln!(src, "    where F: tonic::service::Interceptor {{");
    let _ = writeln!(
        src,
        "        tonic::service::interceptor::InterceptedService::new(Self::new(inner), interceptor)"
    );
    let _ = writeln!(src, "    }}");
    let _ = writeln!(
        src,
        "    pub fn accept_compressed(mut self, encoding: tonic::codec::CompressionEncoding) -> Self {{ self.accept_compression_encodings.enable(encoding); self }}"
    );
    let _ = writeln!(
        src,
        "    pub fn send_compressed(mut self, encoding: tonic::codec::CompressionEncoding) -> Self {{ self.send_compression_encodings.enable(encoding); self }}"
    );
    let _ = writeln!(
        src,
        "    pub fn max_decoding_message_size(mut self, limit: usize) -> Self {{ self.max_decoding_message_size = Some(limit); self }}"
    );
    let _ = writeln!(
        src,
        "    pub fn max_encoding_message_size(mut self, limit: usize) -> Self {{ self.max_encoding_message_size = Some(limit); self }}"
    );
    let _ = writeln!(src, "}}");
    let _ = writeln!(
        src,
        "impl<T: {svc_ty}> tonic::server::NamedService for {server}<T> {{"
    );
    let _ = writeln!(src, "    const NAME: &'static str = \"{}\";", svc.full_name);
    let _ = writeln!(src, "}}");
    let _ = writeln!(
        src,
        "impl<T, B> tonic::codegen::Service<http::Request<B>> for {server}<T>"
    );
    let _ = writeln!(src, "where");
    let _ = writeln!(src, "    T: {svc_ty},");
    let _ = writeln!(src, "    B: tonic::codegen::Body + Send + 'static,");
    let _ = writeln!(
        src,
        "    B::Error: Into<tonic::codegen::StdError> + Send + 'static,"
    );
    let _ = writeln!(src, "{{");
    let _ = writeln!(
        src,
        "    type Response = http::Response<tonic::body::Body>;"
    );
    let _ = writeln!(src, "    type Error = Infallible;");
    let _ = writeln!(src, "    type Future = Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>> + Send>>;");
    let _ = writeln!(src, "    fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {{ Poll::Ready(Ok(())) }}");
    let _ = writeln!(
        src,
        "    fn call(&mut self, req: http::Request<B>) -> Self::Future {{"
    );
    let _ = writeln!(src, "        let inner = self.inner.clone();");
    let _ = writeln!(
        src,
        "        let accept = self.accept_compression_encodings;"
    );
    let _ = writeln!(src, "        let send = self.send_compression_encodings;");
    let _ = writeln!(src, "        let max_dec = self.max_decoding_message_size;");
    let _ = writeln!(src, "        let max_enc = self.max_encoding_message_size;");
    let _ = writeln!(src, "        Box::pin(async move {{");
    let _ = writeln!(src, "            match req.uri().path() {{");
    for m in &svc.methods {
        emit_server_route(src, m, &path_prefix, &svc_ty);
    }
    let _ = writeln!(src, "                _ => {{");
    let _ = writeln!(
        src,
        "                    let mut response = http::Response::new(tonic::body::Body::default());"
    );
    let _ = writeln!(
        src,
        "                    let headers = response.headers_mut();"
    );
    let _ = writeln!(src, "                    headers.insert(tonic::Status::GRPC_STATUS, (tonic::Code::Unimplemented as i32).into());");
    let _ = writeln!(src, "                    headers.insert(http::header::CONTENT_TYPE, tonic::metadata::GRPC_CONTENT_TYPE);");
    let _ = writeln!(src, "                    Ok(response)");
    let _ = writeln!(src, "                }}");
    let _ = writeln!(src, "            }}");
    let _ = writeln!(src, "        }})");
    let _ = writeln!(src, "    }}");
    let _ = writeln!(src, "}}");
}

fn emit_service_trait_method(src: &mut String, m: &MethodDescriptor) {
    let fn_name = to_snake(&m.name);
    let req = rust_ident(&m.input_type);
    let resp = rust_ident(&m.output_type);
    let assoc = format!("{}Stream", m.name);
    match (m.client_streaming, m.server_streaming) {
        (false, false) => {
            let _ = writeln!(src, "    fn {fn_name}(&self, request: tonic::Request<{req}>) -> impl Future<Output = Result<tonic::Response<{resp}>, tonic::Status>> + Send;");
        }
        (true, true) => {
            let _ = writeln!(src, "    type {assoc}: tokio_stream::Stream<Item = Result<{resp}, tonic::Status>> + Send + 'static;");
            let _ = writeln!(src, "    fn {fn_name}(&self, request: tonic::Request<tonic::Streaming<{req}>>) -> impl Future<Output = Result<tonic::Response<Self::{assoc}>, tonic::Status>> + Send;");
        }
        (true, false) => {
            let _ = writeln!(src, "    fn {fn_name}(&self, request: tonic::Request<tonic::Streaming<{req}>>) -> impl Future<Output = Result<tonic::Response<{resp}>, tonic::Status>> + Send;");
        }
        (false, true) => {
            let _ = writeln!(src, "    type {assoc}: tokio_stream::Stream<Item = Result<{resp}, tonic::Status>> + Send + 'static;");
            let _ = writeln!(src, "    fn {fn_name}(&self, request: tonic::Request<{req}>) -> impl Future<Output = Result<tonic::Response<Self::{assoc}>, tonic::Status>> + Send;");
        }
    }
}

fn emit_client_method(src: &mut String, m: &MethodDescriptor, prefix: &str) {
    let fn_name = to_snake(&m.name);
    let req = rust_ident(&m.input_type);
    let resp = rust_ident(&m.output_type);
    let path = format!("{prefix}/{}", m.name);
    match (m.client_streaming, m.server_streaming) {
        (false, false) => {
            let _ = writeln!(src, "    pub async fn {fn_name}(&mut self, request: tonic::Request<{req}>) -> Result<tonic::Response<{resp}>, tonic::Status> {{");
            let _ = writeln!(src, "        self.inner.ready().await.map_err(|e| tonic::Status::unknown(e.into().to_string()))?;");
            let _ = writeln!(src, "        self.inner.unary(request, \"{path}\".parse().unwrap(), ProtobufCodec::<{req}, {resp}>::default()).await");
            let _ = writeln!(src, "    }}");
        }
        (true, true) => {
            let _ = writeln!(src, "    pub async fn {fn_name}<S>(&mut self, request: tonic::Request<S>) -> Result<tonic::Response<tonic::Streaming<{resp}>>, tonic::Status>");
            let _ = writeln!(
                src,
                "    where S: tokio_stream::Stream<Item = {req}> + Send + 'static {{"
            );
            let _ = writeln!(src, "        self.inner.ready().await.map_err(|e| tonic::Status::unknown(e.into().to_string()))?;");
            let _ = writeln!(src, "        self.inner.streaming(request, \"{path}\".parse().unwrap(), ProtobufCodec::<{req}, {resp}>::default()).await");
            let _ = writeln!(src, "    }}");
        }
        (true, false) => {
            let _ = writeln!(src, "    pub async fn {fn_name}<S>(&mut self, request: tonic::Request<S>) -> Result<tonic::Response<{resp}>, tonic::Status>");
            let _ = writeln!(
                src,
                "    where S: tokio_stream::Stream<Item = {req}> + Send + 'static {{"
            );
            let _ = writeln!(src, "        self.inner.ready().await.map_err(|e| tonic::Status::unknown(e.into().to_string()))?;");
            let _ = writeln!(src, "        self.inner.client_streaming(request, \"{path}\".parse().unwrap(), ProtobufCodec::<{req}, {resp}>::default()).await");
            let _ = writeln!(src, "    }}");
        }
        (false, true) => {
            let _ = writeln!(src, "    pub async fn {fn_name}(&mut self, request: tonic::Request<{req}>) -> Result<tonic::Response<tonic::Streaming<{resp}>>, tonic::Status> {{");
            let _ = writeln!(src, "        self.inner.ready().await.map_err(|e| tonic::Status::unknown(e.into().to_string()))?;");
            let _ = writeln!(src, "        self.inner.server_streaming(request, \"{path}\".parse().unwrap(), ProtobufCodec::<{req}, {resp}>::default()).await");
            let _ = writeln!(src, "    }}");
        }
    }
}

fn emit_server_route(src: &mut String, m: &MethodDescriptor, prefix: &str, svc_ty: &str) {
    let fn_name = to_snake(&m.name);
    let req = rust_ident(&m.input_type);
    let resp = rust_ident(&m.output_type);
    let path = format!("{prefix}/{}", m.name);
    match (m.client_streaming, m.server_streaming) {
        (false, false) => {
            let _ = writeln!(src, "                \"{path}\" => {{");
            let _ = writeln!(src, "                    struct Svc<T>(Arc<T>);");
            let _ = writeln!(src, "                    impl<T: {svc_ty}> tonic::server::UnaryService<{req}> for Svc<T> {{");
            let _ = writeln!(src, "                        type Response = {resp};");
            let _ = writeln!(src, "                        type Future = Pin<Box<dyn Future<Output = Result<tonic::Response<{resp}>, tonic::Status>> + Send>>;");
            let _ = writeln!(src, "                        fn call(&mut self, request: tonic::Request<{req}>) -> Self::Future {{");
            let _ = writeln!(
                src,
                "                            let inner = self.0.clone();"
            );
            let _ = writeln!(src, "                            Box::pin(async move {{ inner.{fn_name}(request).await }})");
            let _ = writeln!(src, "                        }}");
            let _ = writeln!(src, "                    }}");
            let _ = writeln!(src, "                    let mut grpc = tonic::server::Grpc::new(ProtobufCodec::<{resp}, {req}>::default()).apply_compression_config(accept, send).apply_max_message_size_config(max_dec, max_enc);");
            let _ = writeln!(
                src,
                "                    Ok(grpc.unary(Svc(inner), req).await)"
            );
            let _ = writeln!(src, "                }}");
        }
        (true, true) => {
            let assoc = format!("{}Stream", m.name);
            let _ = writeln!(src, "                \"{path}\" => {{");
            let _ = writeln!(src, "                    struct Svc<T>(Arc<T>);");
            let _ = writeln!(src, "                    impl<T: {svc_ty}> tonic::server::StreamingService<{req}> for Svc<T> {{");
            let _ = writeln!(src, "                        type Response = {resp};");
            let _ = writeln!(
                src,
                "                        type ResponseStream = T::{assoc};"
            );
            let _ = writeln!(src, "                        type Future = Pin<Box<dyn Future<Output = Result<tonic::Response<Self::ResponseStream>, tonic::Status>> + Send>>;");
            let _ = writeln!(src, "                        fn call(&mut self, request: tonic::Request<tonic::Streaming<{req}>>) -> Self::Future {{");
            let _ = writeln!(
                src,
                "                            let inner = self.0.clone();"
            );
            let _ = writeln!(src, "                            Box::pin(async move {{ inner.{fn_name}(request).await }})");
            let _ = writeln!(src, "                        }}");
            let _ = writeln!(src, "                    }}");
            let _ = writeln!(src, "                    let mut grpc = tonic::server::Grpc::new(ProtobufCodec::<{resp}, {req}>::default()).apply_compression_config(accept, send).apply_max_message_size_config(max_dec, max_enc);");
            let _ = writeln!(
                src,
                "                    Ok(grpc.streaming(Svc(inner), req).await)"
            );
            let _ = writeln!(src, "                }}");
        }
        (true, false) => {
            let _ = writeln!(src, "                \"{path}\" => {{");
            let _ = writeln!(src, "                    struct Svc<T>(Arc<T>);");
            let _ = writeln!(src, "                    impl<T: {svc_ty}> tonic::server::ClientStreamingService<{req}> for Svc<T> {{");
            let _ = writeln!(src, "                        type Response = {resp};");
            let _ = writeln!(src, "                        type Future = Pin<Box<dyn Future<Output = Result<tonic::Response<{resp}>, tonic::Status>> + Send>>;");
            let _ = writeln!(src, "                        fn call(&mut self, request: tonic::Request<tonic::Streaming<{req}>>) -> Self::Future {{");
            let _ = writeln!(
                src,
                "                            let inner = self.0.clone();"
            );
            let _ = writeln!(src, "                            Box::pin(async move {{ inner.{fn_name}(request).await }})");
            let _ = writeln!(src, "                        }}");
            let _ = writeln!(src, "                    }}");
            let _ = writeln!(src, "                    let mut grpc = tonic::server::Grpc::new(ProtobufCodec::<{resp}, {req}>::default()).apply_compression_config(accept, send).apply_max_message_size_config(max_dec, max_enc);");
            let _ = writeln!(
                src,
                "                    Ok(grpc.client_streaming(Svc(inner), req).await)"
            );
            let _ = writeln!(src, "                }}");
        }
        (false, true) => {
            let assoc = format!("{}Stream", m.name);
            let _ = writeln!(src, "                \"{path}\" => {{");
            let _ = writeln!(src, "                    struct Svc<T>(Arc<T>);");
            let _ = writeln!(src, "                    impl<T: {svc_ty}> tonic::server::ServerStreamingService<{req}> for Svc<T> {{");
            let _ = writeln!(src, "                        type Response = {resp};");
            let _ = writeln!(
                src,
                "                        type ResponseStream = T::{assoc};"
            );
            let _ = writeln!(src, "                        type Future = Pin<Box<dyn Future<Output = Result<tonic::Response<Self::ResponseStream>, tonic::Status>> + Send>>;");
            let _ = writeln!(src, "                        fn call(&mut self, request: tonic::Request<{req}>) -> Self::Future {{");
            let _ = writeln!(
                src,
                "                            let inner = self.0.clone();"
            );
            let _ = writeln!(src, "                            Box::pin(async move {{ inner.{fn_name}(request).await }})");
            let _ = writeln!(src, "                        }}");
            let _ = writeln!(src, "                    }}");
            let _ = writeln!(src, "                    let mut grpc = tonic::server::Grpc::new(ProtobufCodec::<{resp}, {req}>::default()).apply_compression_config(accept, send).apply_max_message_size_config(max_dec, max_enc);");
            let _ = writeln!(
                src,
                "                    Ok(grpc.server_streaming(Svc(inner), req).await)"
            );
            let _ = writeln!(src, "                }}");
        }
    }
}