cbor-edn 0.0.10

Converter and processor for CBOR Diagnostic Notation (EDN)
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
//! # Processing CBOR Diagnostic Notation (EDN)
//!
//! This crate provides tools to interconvert CBOR data between its binary and its diagnostic form,
//! and can manipulate the diagnostic representation.
//!
//! ## What this works with
//!
//! [CBOR] is a self-describing data format that is compact and efficient to use; think JSON but
//! binary. As a binary format, it is not human readable, but there exists a Diagnostic Notation
//! for it called [EDN] (which is currently [being revised]). CBOR can express all information of
//! JSON and more, and the diagnostic notation extends JSON. As examples, the compact binary data
//! (represented in hex in the first line) is equivalent to the diagnostic notation in the 2nd
//! line:
//!
//! ```text
//! 83 01 02 62 68 69
//! [1, 2, "hi"]
//! ```
//!
//! [CBOR]: https://cbor.io/
//! [EDN]: https://datatracker.ietf.org/doc/html/rfc8610#appendix-G
//! [being revised]: https://www.ietf.org/archive/id/draft-ietf-cbor-edn-literals-15.html
//!
//! ## API Overview
//!
//! The main entry points to this crate are:
//!
//! * [`StandaloneItem`] can parse both CBOR and diagnostic notation.
//! * [`Sequence`] can parse multiple concatenated CBOR items (called [CBOR sequences]).
//!
//! In all cases, the library preserves loaded data through serialization back into the original
//! format:
//!
//! 1. Choices that are exclusive to EDN are preserved when saving as EDN.
//!
//!    This includes whether a byte string is shown as ASCII or hexadecimal, comments, optional
//!    commas, and even spaces.
//!
//! 2. Choices that mainly exist in CBOR.
//!
//!    This includes whether a list is encoded in definite length or indefinite length, and in how
//!    many bytes short numbers are encoded.
//!
//! Converting between the format is, of course, preserving the CBOR information content, but
//! generally loses aspects such as comments or trailing zeros in decimal numbers.
//!
//! Beyond converting CBOR to EDN and vice versa, this crate can also be used to manipulate CBOR,
//! e. g. to provide explanatory comments around items or to use more specialized representations.
//! Handling those usually involves a single [`Item`], which is distinct from a [`StandaloneItem`]
//! in that any space or comments around it are part of the surrounding structure (which means that
//! it is not sensible to parse EDN into a single [`Item`] because even an innocent line break at
//! the end of the EDN would throw the parser off).
//!
//! [CBOR sequences]: https://www.rfc-editor.org/rfc/rfc8742.html
//!
//! ### Example usage
//!
//! This example shows how to convert EDN into CBOR and back to EDN.
//!
//! ```
//! # use cbor_edn::StandaloneItem;
//! // Ingest CBOR Diagnostic Notation.
//! let input: &str  = &r#"[1, 2, "x"]"#;
//! let parsed = StandaloneItem::parse(input).unwrap();
//! // Emit it as CBOR.
//! let cbor = parsed.to_cbor().unwrap();
//!
//! // Parse the CBOR
//! let parsed = StandaloneItem::from_cbor(&cbor).unwrap();
//! let edn = parsed.serialize();
//! assert_eq!(edn.as_str(), input);
//! ```
//!
//! ## Implementation remarks
//!
//! The parser used by this crate is a PEG (Parsing Expression Grammer) parser built from the ABNF
//! used in the [EDN specification].
//!
//! The types' data model is oriented more towards EDN than towards CBOR, as that has richer
//! information and is generally needed for tasks such as annotation or delayed processing of
//! application oriented literals.
//!
//! Parsed values are expected to round-trip to identical representations when serialized. Most
//! manipulations of the values will ensure that their serialization output can also be
//! round-tripped from the internal format to the EDN serialization and back into the internal
//! format, but this can not be provided by all. (For example, removing all optional commas
//! while retaining comments would make the previous distinction between whether a comment was
//! before or after a comma indistinguishable).
//!
//! Correct parsing does not guarantee that the value can also be encoded into CBOR. While there
//! are aspects that could be handled at parsing time and are not (eg. tag numbers exceeding the
//! encodable number space), there are cases that can not be handled by a library without further
//! context or privileges (eg. the e'' application oriented literal that needs application context,
//! or the ref'' application oriented literal that defers to relative files, accessing which can
//! involve file or network access). Consequentially, conversion to CBOR through the various
//! `.to_cbor()` methods is inherently fallible, while `.serialize()`ing into EDN is not.
//!
//! [EDN specification]: https://www.ietf.org/archive/id/draft-ietf-cbor-edn-literals-15.html
//!
//! ## Completeness
//!
//! Known limitations are:
//!
//! * Support for inspecting and constructing CBOR items is incomplete. The most common types can
//!   be constructed; contructing or inspecting more exotic items is possible through parsing
//!   hand-crafted EDN/CBOR and using the generated serializations, respectively.
//!
//! * Options for attaching comments and space are limited and immature:
//!
//!   * [`Item::with_comment()`] & [`StandaloneItem::set_comment`] can be used to add comments, but
//!     mainly produce [top-level items][StandaloneItem]. Deeper items are not configurable that
//!     way, as the comments don't live in the item but its container.
//!
//!   * Comments can be added to items through visitors such as [`Item::visit_map_elements`]; both
//!     the success and the error path of a visiting function can set comments around a tag.
//!
//!   * Replacing an item with hand-crafted EDN (possibly from serialized item) is always an
//!     option.
//!
//! * Indenting EDN works for the easy cases, but more exotic cases such as overflowing the limited
//!   width, long keys, or hash comments, easily disrupt the visual result.
//!
//! ## Security
//!
//! This library does not access network or file system in any surprising ways and does not
//! endanger memory safety on its own. The main threat in using it is not resource bound: even
//! without packed CBOR, heavy nesting can easily overflow the stack, and the float conversions are
//! costly in time. Unless resource usage per user is limited, it is recommended to limit untrusted
//! user input to the length of repeated `{` characters that do not yet overflow the stack.
//!
//! The crate has not been audited internally or externally. As the
//! [licenses](https://spdx.org/licenses/MIT.html)
//! [state](https://spdx.org/licenses/Apache-2.0.html), the software is provided "as is".
//!
//! ## CLI application
//!
//! Some functionality is available through a binary included with this crate:
//!
//! <!-- See https://github.com/assert-rs/snapbox/issues/172 -->
//! ```console
//! $ echo "[1, 2, 'x', ip'2001:db1::/64']" | cbor-edn diag2diag
//! [1, 2, 'x', ip'2001:db1::/64']
//! ```
#![forbid(unsafe_code)]

use std::borrow::Cow;

mod visitor;
use visitor::{
    ApplicationLiteralsVisitor, ArrayElementVisitor, MapElementVisitor, MapValueHandler,
    ProcessResult, TagVisitor, Visitor,
};

pub mod application;
pub mod error;
mod float;
mod space;
use space::{Comment, SDetails, MS, MSC, S, SOC};
mod number;
use number::{Number, NumberParts, NumberValue, Sign};
mod string;
use string::{CborString, PreprocessedStringComponent, String1e};

#[cfg(test)]
mod tests;

use error::*;

const U8MAX: u64 = u8::MAX as _;
const U16MAX: u64 = u16::MAX as _;
const U32MAX: u64 = u32::MAX as _;

/// A CBOR Item, including any space and comments surrounding it in a serialization.
///
/// This is typically parsed either from EDN or from CBOR:
///
/// ```
/// # use cbor_edn::*;
/// let from_edn = StandaloneItem::parse("{1: {2: {3: null}}}").unwrap();
/// let mut from_cbor = StandaloneItem::from_cbor(
///     &[0xa1, 0x01, 0xa1, 0x02, 0xa1, 0x03, 0xf6]
/// ).unwrap();
/// ```
///
/// … and then manipulated:
///
/// ```
/// # use cbor_edn::*;
/// # let mut from_cbor = StandaloneItem::from_cbor(
/// #     &[0xa1, 0x01, 0xa1, 0x02, 0xa1, 0x03, 0xf6]
/// # ).unwrap();
/// // No spaces when we don't need them.
/// from_cbor.set_delimiters(DelimiterPolicy::DiscardAll);
/// ```
///
/// … and then serialized:
///
/// ```
/// # use cbor_edn::*;
/// # let from_edn = StandaloneItem::parse("{1: {2: {3: null}}}").unwrap();
/// # let mut from_cbor = StandaloneItem::from_cbor(
/// #     &[0xa1, 0x01, 0xa1, 0x02, 0xa1, 0x03, 0xf6]
/// # ).unwrap();
/// # from_cbor.set_delimiters(DelimiterPolicy::DiscardAll);
/// assert_eq!(from_cbor.serialize(), "{1:{2:{3:null}}}");
/// assert_eq!(from_cbor.to_cbor().unwrap(), from_edn.to_cbor().unwrap());
/// ```
#[derive(Debug, Clone, PartialEq)]
pub struct StandaloneItem<'a>(S<'a>, Item<'a>, S<'a>);

/// A CBOR Item.
///
/// This represents an inner item as is contained in a CBOR array, map, tag, but also in a
/// [`Sequence`], or a [`StandaloneItem`] (to which it is identical in CBOR, but the standalone
/// item also describes any comments or space before or after the top-level item).
///
/// It is mainly found in deeper interaction with CBOR items, for example:
///
/// ```
/// # use cbor_edn::*;
/// let my_map = StandaloneItem::parse(r#"{1: "one", 2: "two"}"#).unwrap();
/// let my_toplevel: &Item = my_map.item();
/// for (key, value) in my_toplevel.get_map_items().unwrap() {
///     let key: &Item = key;
///     println!("Mapping {} to {}", key.serialize(), value.serialize());
/// }
/// ```
///
/// By virtue of EDN's expressiveness, this type is capable not
/// only of expressing any well-formed CBOR, but also to preserve encoding details that are not
/// preferred (eg. a small integer encoded in more bytes than necessary). Some transformations on
/// the EDN may lose such details; components that perform a translation such as recoding `(_
/// h'18', h'6402')` into `<<100, 2>>` have a choice to either not perform the translation or to
/// discard some encoding details.
#[derive(Debug, Clone, PartialEq)]
pub struct Item<'a>(InnerItem<'a>);

/// # Conversion between the in-memory format and serializations
impl<'a> StandaloneItem<'a> {
    /// Ingests CBOR Diagnostic Notation (EDN) representing a single CBOR item
    ///
    /// Note that this will only return syntactic errors. Content errors that make it impossible to
    /// produce this as CBOR, such as non-matching encoding indicators or unknown application
    /// oriented literals, are not reported.
    pub fn parse(s: &'a str) -> Result<Self, ParseError> {
        cbordiagnostic::one_item(s).map_err(ParseError)
    }

    /// Produce an EDN String from the item
    pub fn serialize(&self) -> String {
        Unparse::serialize(self)
    }

    /// Parse a complete CBOR item.
    ///
    /// Providing excessive data results in an error.
    pub fn from_cbor(cbor: &[u8]) -> Result<Self, CborError> {
        Ok(Self(S::default(), Item::from_cbor(cbor)?, S::default()))
    }

    /// Parse a complete CBOR item.
    ///
    /// Any remaining byts are returned as part of the result.
    pub fn from_cbor_with_rest(cbor: &[u8]) -> Result<(Self, &[u8]), CborError> {
        let (item, rest) = Item::from_cbor_with_rest(cbor)?;
        Ok((Self(S::default(), item, S::default()), rest))
    }

    /// Encode into a binary CBOR representation
    pub fn to_cbor(&self) -> Result<Vec<u8>, InconsistentEdn> {
        Ok(Unparse::to_cbor(self)?.collect())
    }
}

/// # Helpers for conversion between standalone and bare items
impl<'a> StandaloneItem<'a> {
    /// Discards the comments and space around the single item, returning only the item itself.
    pub fn into_item(self) -> Item<'a> {
        self.1
    }

    /// Accesses the single item.
    pub fn item(&self) -> &Item<'a> {
        &self.1
    }

    /// Mutably accesses the single item.
    pub fn item_mut(&mut self) -> &mut Item<'a> {
        &mut self.1
    }

    fn inner(&self) -> &InnerItem<'a> {
        self.1.inner()
    }

    /// Clone the item, turning any [`Cow::Borrowed`] into owned versions, which can then satisfy
    /// any lifetime.
    pub fn cloned<'any>(&self) -> StandaloneItem<'any> {
        StandaloneItem(self.0.cloned(), self.1.cloned(), self.2.cloned())
    }
}

/// # Conversion between the in-memory format and serializations
///
/// Note that unlike [`StandaloneItem`], this does not provide EDN parsing: Any standalone EDN CBOR
/// item may contain outer blank space or comments, which can only be represented in a
/// [`StandaloneItem`].
impl Item<'_> {
    /// Produce an EDN String from the item
    pub fn serialize(&self) -> String {
        Unparse::serialize(self)
    }

    /// Parse a complete CBOR item.
    ///
    /// Providing excessive data results in an error.
    pub fn from_cbor(cbor: &[u8]) -> Result<Self, CborError> {
        match Self::from_cbor_with_rest(cbor) {
            Ok((s, &[])) => Ok(s),
            Ok(_) => Err(CborError("Data after item")),
            Err(e) => Err(e),
        }
    }

    /// Parse a complete CBOR item.
    ///
    /// Any remaining byts are returned as part of the result.
    pub fn from_cbor_with_rest(cbor: &[u8]) -> Result<(Self, &[u8]), CborError> {
        let (major, argument, spec, mut tail) = process_cbor_major_argument(cbor)?;

        let mut s = match (major, argument, spec) {
            (Major::Unsigned, Some(argument), spec) => Self::new_integer_decimal_with_spec(
                argument,
                spec.or_none_if_default_for_arg(argument),
            ),
            (Major::Negative, Some(argument), spec) => Self::new_integer_decimal_with_spec(
                -1i128 - i128::from(argument),
                spec.or_none_if_default_for_arg(argument),
            ),
            (Major::FloatSimple, Some(n @ 0..=19), Spec::S_i) => {
                Simple::Numeric(Box::new(Self::new_integer_decimal(n).into())).into()
            }
            (Major::FloatSimple, Some(20), Spec::S_i) => Simple::False.into(),
            (Major::FloatSimple, Some(21), Spec::S_i) => Simple::True.into(),
            (Major::FloatSimple, Some(22), Spec::S_i) => Simple::Null.into(),
            (Major::FloatSimple, Some(23), Spec::S_i) => Simple::Undefined.into(),
            (Major::FloatSimple, Some(n @ 32..=255), Spec::S_0) => {
                Simple::Numeric(Box::new(Self::new_integer_decimal(n).into())).into()
            }
            // 0..=31 in S_0 or 24..=31 in S_i
            (Major::FloatSimple, _, Spec::S_i | Spec::S_0) => {
                return Err(CborError("Invalid simple value"))
            }
            (Major::FloatSimple, Some(0x7c00), Spec::S_1) => {
                Number(Cow::from("Infinity")).with_spec(Some(Spec::S_1))
            }
            (Major::FloatSimple, Some(0xfc00), Spec::S_1) => {
                Number(Cow::from("-Infinity")).with_spec(Some(Spec::S_1))
            }
            (Major::FloatSimple, Some(0x7e00), Spec::S_1) => {
                Number(Cow::from("NaN")).with_spec(Some(Spec::S_1))
            }
            (Major::FloatSimple, Some(n), Spec::S_1) => {
                let f =
                    float::f16_bits_to_f64(n.try_into().expect("Range limited by construction"));
                Number::new_float(f).with_spec(Some(Spec::S_1))
            }
            (Major::FloatSimple, Some(n), Spec::S_2) => {
                let n: u32 = n.try_into().expect("Range limited by construction");
                let f = f64::from(f32::from_bits(n));
                Number::new_float(f).with_spec(Some(Spec::S_2))
            }
            (Major::FloatSimple, Some(n), Spec::S_3) => {
                let f = f64::from_bits(n);
                Number::new_float(f).with_spec(Some(Spec::S_3))
            }
            (Major::FloatSimple, None, _ /* S_ not written for exhaustiveness */)
            | (Major::FloatSimple, _ /* None not written for exhaustiveness */, Spec::S_) => {
                return Err(CborError(
                    "Break code only expected at end of indefinte length items",
                ))
            }
            (Major::Tagged, Some(n), s) => {
                // FIXME this is recursing on the stack rather than on the heap
                let (item, new_tail) = StandaloneItem::from_cbor_with_rest(tail)?;
                tail = new_tail;
                item.tagged_with_spec(n, s.or_none_if_default_for_arg(n))
            }
            (Major::Unsigned | Major::Negative | Major::Tagged, None, _) => {
                return Err(CborError(
                    "Integer/Tag with indefinite length encoding is not well-formed",
                ))
            }
            (Major::ByteString, Some(n), spec) => {
                let data = n
                    .try_into()
                    .ok()
                    .and_then(|n| tail.get(..n))
                    .ok_or(CborError("Announced bytes unavailable"))?;
                tail = &tail[data.len()..];
                Self::new_bytes_hex_with_spec(data, spec.or_none_if_default_for_arg(n))
            }
            (Major::TextString, Some(n), spec) => {
                let data = n
                    .try_into()
                    .ok()
                    .and_then(|n| tail.get(..n))
                    .ok_or(CborError("Announced bytes unavailable"))?;
                let data = core::str::from_utf8(data)
                    .map_err(|_| CborError("Text string must be valid UTF-8"))?;
                tail = &tail[data.len()..];
                Self::new_text_with_spec(data, spec.or_none_if_default_for_arg(n))
            }
            (
                Major::ByteString | Major::TextString,
                None,
                _, /* S_ not written for exhaustiveness */
            ) => {
                let mut items = vec![];
                while tail.first() != Some(&0xff) {
                    let (inner_major, argument, spec, new_tail) =
                        process_cbor_major_argument(tail)?;
                    let Some(argument) = argument.and_then(|a| usize::try_from(a).ok()) else {
                        return Err(CborError(
                            "Indefinite length strings can only contain definite lengths and must fit in data",
                        ));
                    };
                    if inner_major != major {
                        return Err(CborError(
                            "Indefinite length strings can only contain matching items",
                        ));
                    }
                    if new_tail.len() < argument {
                        return Err(CborError(
                            "Announced bytes unavailable inside indefinite length byte string",
                        ));
                    }
                    // with split_at_checked, we could combine the checkinto the split
                    let (item_data, new_tail) = new_tail.split_at(argument);
                    tail = new_tail;
                    items.push(match major {
                        Major::ByteString => {
                            CborString::new_bytes_hex_with_spec(item_data, Some(spec))
                        }
                        Major::TextString => CborString::new_text_with_spec(
                            core::str::from_utf8(item_data)
                                .map_err(|_| CborError("Text string must be valid UTF-8"))?,
                            Some(spec),
                        ),
                        _ => unreachable!(),
                    });
                }
                if tail.is_empty() {
                    return Err(CborError(
                        "Indefinite length byte string terminated after item",
                    ));
                }
                tail = &tail[1..];

                let mut items = items.drain(..);
                if let Some(first_item) = items.next() {
                    InnerItem::StreamString(
                        Default::default(),
                        NonemptyMscVec::new(first_item, items),
                    )
                    .into()
                } else {
                    todo!()
                }
            }
            (Major::Array, mut length, spec) => {
                // FIXME this is recursing on the stack rather than on the heap
                let mut items = vec![];
                while length != Some(0) && tail.first() != Some(&0xff) {
                    let (item, new_tail) = Self::from_cbor_with_rest(tail)?;
                    items.push(item);
                    tail = new_tail;
                    if let Some(ref mut n) = &mut length {
                        *n -= 1;
                    }
                }
                if length.is_none() {
                    if tail.is_empty() {
                        return Err(CborError(
                            "Indefinite length byte string terminated after item",
                        ));
                    }
                    tail = &tail[1..];
                }
                let spec = match length {
                    Some(l) => spec.or_none_if_default_for_arg(l),
                    None => Some(spec), // which is always indefinite length
                };
                InnerItem::Array(SpecMscVec::new(spec, items.into_iter())).into()
            }
            (Major::Map, mut length, spec) => {
                // FIXME this is recursing on the stack rather than on the heap
                let mut items = vec![];
                while length != Some(0) && tail.first() != Some(&0xff) {
                    let (key, new_tail) = Self::from_cbor_with_rest(tail)?;
                    tail = new_tail;
                    let (value, new_tail) = Self::from_cbor_with_rest(tail)?;
                    tail = new_tail;
                    items.push(Kp::new(key, value));
                    if let Some(ref mut n) = &mut length {
                        *n -= 1;
                    }
                }
                if length.is_none() {
                    if tail.is_empty() {
                        return Err(CborError(
                            "Indefinite length byte string terminated after item",
                        ));
                    }
                    tail = &tail[1..];
                }
                let spec = match length {
                    Some(l) => spec.or_none_if_default_for_arg(l),
                    None => Some(spec), // which is always indefinite length
                };
                InnerItem::Map(SpecMscVec::new(spec, items.into_iter())).into()
            }
        };

        s.set_delimiters(DelimiterPolicy::SingleLineRegularSpacing);
        Ok((s, tail))
    }

    fn visit(&mut self, visitor: &mut impl Visitor) -> ProcessResult {
        let mut result = visitor.process(self);
        if result.take_recurse() {
            self.0.visit(visitor);
        }
        result
    }

    /// Clone the item, turning any [`Cow::Borrowed`] into owned versions, which can then satisfy
    /// any lifetime.
    pub fn cloned<'any>(&self) -> Item<'any> {
        Item(self.0.cloned())
    }
}

/// # Conversion between the in-memory format and serializations
impl<'a> Item<'a> {
    fn inner(&self) -> &InnerItem<'a> {
        &self.0
    }

    fn inner_mut(&mut self) -> &mut InnerItem<'a> {
        &mut self.0
    }
}

/// # Creating items from data or by wrapping other items
impl<'a> StandaloneItem<'a> {
    fn tagged_with_spec(self, tag: u64, spec: Option<Spec>) -> Item<'a> {
        InnerItem::Tagged(tag, spec, Box::new(self)).into()
    }

    /// Wrap the item into a CBOR tag.
    pub fn tagged(self, tag: u64) -> Item<'a> {
        InnerItem::Tagged(tag, None, Box::new(self)).into()
    }
}

/// # Creating items from data or by wrapping other items
impl<'a> Item<'a> {
    fn new_integer_decimal_with_spec(value: impl Into<i128>, spec: Option<Spec>) -> Self {
        Number(format!("{}", value.into()).into()).with_spec(spec)
    }

    /// Create a new item that is integer valued in CBOR and expressed in decimal in EDN.
    ///
    /// Note that while values exceeding i65 are accepted, they can not be encoded into CBOR.
    pub fn new_integer_decimal(value: impl Into<i128>) -> Self {
        Self::new_integer_decimal_with_spec(value, None)
    }

    /// Create a new item that is float valued in CBOR and expressed in decimal in EDN.
    pub fn new_float_decimal(value: f64) -> Self {
        Number::new_float(value).with_spec(None)
    }

    /// Create a new item that is integer valued in CBOR and expressed in hexadecimal in EDN.
    ///
    /// Negative values have not been implemented in this constructor.
    pub fn new_integer_hex(value: impl Into<u64>) -> Self {
        InnerItem::Number(Number(format!("0x{:x}", value.into()).into()), None).into()
    }

    fn new_bytes_hex_with_spec(value: &[u8], spec: Option<Spec>) -> Self {
        InnerItem::String(CborString::new_bytes_hex_with_spec(value, spec)).into()
    }

    /// Create a new item that is a byte string in CBOR (identical to the passed in value) and
    /// expressed as a `h'...'` string in EDN.
    pub fn new_bytes_hex(value: &[u8]) -> Self {
        Self::new_bytes_hex_with_spec(value, None)
    }

    fn new_text_with_spec(value: &str, spec: Option<Spec>) -> Self {
        InnerItem::String(CborString::new_text_with_spec(value, spec)).into()
    }

    /// Create a new item that is a text string in CBOR (identical to the passed in value) and
    /// expressed as a single double-quoted string in EDN.
    ///
    /// ```rust
    /// # use cbor_edn::*;
    /// assert_eq!(
    ///     Item::new_text("Hello \"World\"\0").serialize(),
    ///     r#""Hello \"World\"\u{0}""#,
    /// );
    /// ```
    pub fn new_text(value: &str) -> Self {
        Self::new_text_with_spec(value, None)
    }

    pub fn new_application_literal(identifier: &str, value: &str) -> Result<Self, InconsistentEdn> {
        if cbordiagnostic::app_prefix(identifier).is_err() {
            // FIXME bad error type
            return Err(InconsistentEdn(
                "Identifier is not a valid application string identifier",
            ));
        };
        Ok(InnerItem::String(CborString::new_application_literal(identifier, value, None)).into())
    }

    /// Create a CBOR array out of the items
    pub fn new_array(items: impl Iterator<Item = Item<'a>>) -> Self {
        InnerItem::Array(SpecMscVec::new(None, items)).into()
    }

    /// Create a CBOR map out of the keys-value pairs
    pub fn new_map(items: impl Iterator<Item = (Item<'a>, Item<'a>)>) -> Self {
        InnerItem::Map(SpecMscVec::new(
            None,
            items.map(|(key, value)| Kp::new(key, value)),
        ))
        .into()
    }

    /// Wrap the item into a CBOR tag.
    pub fn tagged(self, tag: u64) -> Item<'a> {
        StandaloneItem::from(self).tagged(tag)
    }
}

/// # Accessing and modifying an item in place
impl StandaloneItem<'_> {
    /// Replace any comment before the item with the new comment
    pub fn with_comment(self, comment: &str) -> Self {
        let wrapped_comment = if comment.contains('/') {
            format!("# {}\n", comment.replace('\n', "\n# "))
        } else {
            format!("/ {} /", comment)
        };
        Self(S(wrapped_comment.into()), self.1, self.2)
    }

    /// Replace any comment before the item with the new comment
    pub fn set_comment(&mut self, comment: &str) {
        let wrapped_comment = if comment.contains('/') {
            format!("# {}\n", comment.replace('\n', "\n# "))
        } else {
            format!("/ {} /", comment)
        };
        self.0 = S(wrapped_comment.into());
    }

    /// Alters how space and comments are placed inside the item.
    ///
    /// See the policy values for details.
    pub fn set_delimiters(&mut self, policy: DelimiterPolicy) {
        // On the top level, let's not add the leading \n, because that would cause an empty line
        // above the sole element formatted like this.
        self.0.set_delimiters(policy, false);
        self.1.set_delimiters(policy);
        // FIXME: Does this also need the code from the Sequence cases?
        self.2.set_delimiters(policy, true);
    }

    fn visit(&mut self, visitor: &mut impl Visitor) {
        self.1
            .visit(visitor)
            .use_space_before(&mut self.0)
            .use_space_after(&mut self.2)
            .done();
    }

    /// For each item in the tree that is a single application literal, call a callback.
    ///
    /// This is primarily used to apply custom EDN filtering:
    ///
    /// ```rust
    /// # use cbor_edn::*;
    /// let mut full = StandaloneItem::parse("[0 /unmodified/, german'zweiundvierzig']").unwrap();
    /// full.visit_application_literals(&mut |id, value: String, item: &mut cbor_edn::Item| {
    ///     if id == "german" {
    ///         let numeric = match value.as_str() {
    ///             "dreiundzwanzig" => 23,
    ///             "zweiundvierzig" => 42,
    ///             _ => todo!(),
    ///         };
    ///         *item = Item::new_integer_decimal(numeric).into();
    ///     }
    ///     Ok(())
    /// });
    /// assert_eq!(full.serialize(), "[0 /unmodified/, 42]");
    /// ```
    pub fn visit_application_literals<F, RF>(&mut self, mut f: RF)
    where
        F: for<'b> FnMut(String, String, &mut Item<'b>) -> Result<(), String> + ?Sized,
        RF: std::ops::DerefMut<Target = F>,
    {
        self.visit(&mut ApplicationLiteralsVisitor {
            user_fn: f.deref_mut(),
        });
    }

    /// For each item in the full tree (including embedded representations) that is tagged, call a
    /// callback.
    ///
    /// Any error string is placed in a comment next to the item. The function should return Ok(())
    /// on any tags it is not interested in visiting.
    ///
    /// This is primarily used to apply custom EDN application; see [application::dt_tag_to_aol] for an
    /// example.
    pub fn visit_tag<F, RF>(&mut self, mut f: RF)
    where
        F: for<'b> FnMut(u64, &mut Item<'b>) -> Result<(), String> + ?Sized,
        RF: std::ops::DerefMut<Target = F>,
    {
        self.visit(&mut TagVisitor {
            user_fn: f.deref_mut(),
        });
    }
}

/// # Accessing and modifying an item in place
impl<'a> Item<'a> {
    /// Access application-extension identifier and string value
    ///
    /// This only succeeds if the item is expressed using a single application oriented literal.
    pub fn get_application_literal(&self) -> Result<(String, String), TypeMismatch> {
        let InnerItem::String(CborString { ref items, .. }) = self.inner() else {
            return Err(TypeMismatch::expecting("application-oriented literal"));
        };
        let [chunk] = items.as_slice() else {
            return Err(TypeMismatch::expecting(
                "single application-oriented literal",
            ));
        };
        let PreprocessedStringComponent::AppString(identifier, value) = chunk
            .preprocess()
            // The only reason this would err is if there is embedded CBOR in there, and then
            // that'd just mean it's not what we requested
            .map_err(|_| TypeMismatch::expecting("application-oriented literal"))?
        else {
            return Err(TypeMismatch::expecting("application-oriented literal"));
        };

        Ok((identifier, value))
    }

    /// Access a byte literal value
    ///
    /// This only succeeds if the item is a single byte string on the CBOR level, no matter how
    /// many EDN concatenations or even chunks. The EDN standard byte encodings (hex, base64 etc.)
    /// are supported, other application-oriented literals need to be resolved first.
    pub fn get_bytes(&self) -> Result<Vec<u8>, TypeMismatch> {
        let mut result = vec![];

        let mut append_items = |items: &Vec<String1e>| -> Result<(), TypeMismatch> {
            for item in items {
                if item
                    .encoded_major_type()
                    .map_err(|_| TypeMismatch::expecting("encodable item"))?
                    != Major::ByteString
                {
                    return Err(TypeMismatch::expecting("byte literal"));
                }
                result.extend(
                    item.bytes_value()
                        .map_err(|_| TypeMismatch::expecting("byte literal or compatible"))?,
                );
            }
            Ok(())
        };

        match self.inner() {
            InnerItem::String(CborString { ref items, .. }) => append_items(items)?,
            InnerItem::StreamString(_, ref chunks) => {
                for CborString { ref items, .. } in chunks.iter() {
                    append_items(items)?;
                }
            }
            _ => return Err(TypeMismatch::expecting("byte literal")),
        }

        Ok(result)
    }

    /// Accesses a string literal value.
    ///
    /// This only succeeds if the item is a single text string on the CBOR level, no matter how
    /// many EDN concatenations or even chunks. The EDN standard byte encodings (hex, base64 etc.)
    /// are tolerated in subsequent items as required for expressing otherwise hard to read parts.
    ///
    /// ```
    /// let item = cbor_edn::StandaloneItem::parse(
    ///     r#" (_ "hello" h'20' "world" ) "#
    /// ).unwrap();
    /// let item = item.item();
    /// assert_eq!("hello world", &item.get_string().unwrap());
    /// ```
    pub fn get_string(&self) -> Result<String, TypeMismatch> {
        let mut result = vec![];

        let mut append_items = |items: &Vec<String1e>| -> Result<(), TypeMismatch> {
            for item in items {
                result.extend(
                    item.bytes_value()
                        .map_err(|_| TypeMismatch::expecting("text literal or compatible"))?,
                );
            }
            Ok(())
        };

        // Just checking the first item because they can not be mixed "except that byte string
        // literal notation can be used inside a sequence of concatenated text string notation
        // literals"
        let check_first = |item: &String1e<'_>| -> Result<(), TypeMismatch> {
            if item
                .encoded_major_type()
                .map_err(|_| TypeMismatch::expecting("encodable item"))?
                != Major::TextString
            {
                return Err(TypeMismatch::expecting("text literal"));
            }
            Ok(())
        };

        match self.inner() {
            InnerItem::String(CborString { ref items, .. }) => {
                check_first(items.first().expect("Part of the type guarantees"))?;
                append_items(items)?;
            }
            InnerItem::StreamString(_, ref chunks) => {
                check_first(
                    chunks
                        .first
                        .items
                        .first()
                        .expect("Part of the type guarantees"),
                )?;
                for CborString { ref items, .. } in chunks.iter() {
                    append_items(items)?;
                }
            }
            _ => return Err(TypeMismatch::expecting("byte literal")),
        }

        String::from_utf8(result).map_err(|_| TypeMismatch::expecting("valid UTF-8"))
    }

    /// Access the tag number
    ///
    /// This only succeeds if the item is a tagged item. Use [`Self::get_tagged()`] to get the
    /// corresponding tagged item.
    pub fn get_tag(&self) -> Result<u64, TypeMismatch> {
        let InnerItem::Tagged(tag, _, _) = self.inner() else {
            return Err(TypeMismatch::expecting("tagged item"));
        };
        Ok(*tag)
    }

    /// Access the inner item of a tag
    ///
    /// This only succeeds if the item is a tagged item. Use [`Self::get_tag()`] to get the
    /// corresponding tag number.
    pub fn get_tagged(&self) -> Result<&StandaloneItem<'a>, TypeMismatch> {
        let InnerItem::Tagged(_, _, ref item) = self.inner() else {
            return Err(TypeMismatch::expecting("tagged item"));
        };
        Ok(item)
    }

    /// Mutably ccess the inner item of a tag
    ///
    /// This only succeeds if the item is a tagged item. Use [`Self::get_tag()`] to get the
    /// corresponding tag number.
    pub fn get_tagged_mut(&mut self) -> Result<&mut StandaloneItem<'a>, TypeMismatch> {
        let InnerItem::Tagged(_, _, ref mut item) = self.inner_mut() else {
            return Err(TypeMismatch::expecting("tagged item"));
        };
        Ok(item)
    }

    /// Access the integer value of an item
    ///
    /// This only succeeds if the item is integer valued; the returned range is an i65 (expressed
    /// as an i128 for simplicity).
    pub fn get_integer(&self) -> Result<i128, TypeMismatch> {
        let InnerItem::Number(ref number, _) = self.inner() else {
            return Err(TypeMismatch::expecting("integer"));
        };
        match number.value() {
            NumberValue::Float(_) => Err(TypeMismatch::expecting("integer")),
            NumberValue::Positive(n) => Ok(n.into()),
            NumberValue::Negative(n) => Ok(-1 - i128::from(n)),
            // FIXME: that's definitely not a type mismatch
            NumberValue::Big(n) => n
                .try_into()
                .map_err(|_| TypeMismatch::expecting("integer in i128 range")),
        }
    }

    /// Access the float value of an item
    ///
    /// This only succeeds if the item is float valued.
    pub fn get_float(&self) -> Result<f64, TypeMismatch> {
        let InnerItem::Number(ref number, _) = self.inner() else {
            return Err(TypeMismatch::expecting("float"));
        };
        match number.value() {
            NumberValue::Float(f) => Ok(f),
            NumberValue::Positive(_) => Err(TypeMismatch::expecting("float (not integer)")),
            NumberValue::Negative(_) => Err(TypeMismatch::expecting("float (not integer)")),
            NumberValue::Big(_) => Err(TypeMismatch::expecting("float (not integer)")),
        }
    }

    /// Access the items inside an array
    ///
    /// This only succeeds if the item is an array.
    pub fn get_array_items(&self) -> Result<impl Iterator<Item = &Item<'a>>, TypeMismatch> {
        let InnerItem::Array(smv) = self.inner() else {
            return Err(TypeMismatch::expecting("array"));
        };

        Ok(smv.iter())
    }

    /// Mutably access the items inside an array
    ///
    /// This only succeeds if the item is an array.
    pub fn get_array_items_mut(
        &mut self,
    ) -> Result<impl Iterator<Item = &mut Item<'a>>, TypeMismatch> {
        let InnerItem::Array(smv) = self.inner_mut() else {
            return Err(TypeMismatch::expecting("array"));
        };

        Ok(smv.iter_mut())
    }

    /// Access the items inside a map
    ///
    /// This only succeeds if the item is a map.
    pub fn get_map_items(
        &self,
    ) -> Result<impl Iterator<Item = (&Item<'a>, &Item<'a>)>, TypeMismatch> {
        let InnerItem::Map(smv) = self.inner() else {
            return Err(TypeMismatch::expecting("map"));
        };

        Ok(smv.iter().map(|kp| (&kp.key, &kp.value)))
    }

    /// Access the items inside a map
    ///
    /// This only succeeds if the item is a map.
    pub fn get_map_items_mut(
        &mut self,
    ) -> Result<impl Iterator<Item = (&mut Item<'a>, &mut Item<'a>)>, TypeMismatch> {
        let InnerItem::Map(smv) = self.inner_mut() else {
            return Err(TypeMismatch::expecting("map"));
        };

        Ok(smv.iter_mut().map(|kp| (&mut kp.key, &mut kp.value)))
    }

    /// Removes any encoding indicators present in the item.
    ///
    /// This does not affect space or comments; in particular, an item containing only the
    /// necessary space may be left with extraneous (but harmless) space that was previously needed
    /// to set an encoding indicator apart from a value.
    pub fn discard_encoding_indicators(&mut self) {
        self.inner_mut().discard_encoding_indicators();
    }

    /// Alters how space and comments are placed inside the item.
    ///
    /// Being a plain [`Item`], this only affects inner space; it can not have any around itself.
    ///
    /// See the policy values for details.
    pub fn set_delimiters(&mut self, policy: DelimiterPolicy) {
        self.0.set_delimiters(policy);
    }

    /// Turn the item into a [`StandaloneItem`] and add a single new comment
    pub fn with_comment(self, comment: &str) -> StandaloneItem<'a> {
        let wrapped_comment = if comment.contains('/') {
            format!("# {}\n", comment.replace('\n', "\n# "))
        } else {
            format!("/ {} /", comment)
        };
        StandaloneItem(S(wrapped_comment.into()), self, S::default())
    }

    /// Calls a callback on any key item inside the map.
    ///
    /// Calling this on a non-map item returns a [type mismatch error][TypeMismatch].
    ///
    /// An error string returned by the callback is stored in the tree as a comment next to the
    /// key. A successful result may also contain text that gets placed next to the key, and may
    /// contain a callback that gets applied in the same fashion to the value after the key.
    ///
    /// # Example
    ///
    /// The [`application::comment_ccs`] method is an exampel of a callback function.
    ///
    /// # Future development
    ///
    /// Once `feature(try_trait)` is usable, those return types can be simplified; until then,
    /// using a [`Result`] enables easy propagation of errors out of the callbacks.
    pub fn visit_map_elements<F, RF>(&mut self, mut f: RF) -> Result<(), TypeMismatch>
    where
        F: for<'b> FnMut(
                &mut Item<'b>,
            ) -> Result<(Option<String>, Option<MapValueHandler>), String>
            + ?Sized,
        RF: std::ops::DerefMut<Target = F>,
    {
        if !matches!(self.0, InnerItem::Map(_)) {
            return Err(TypeMismatch::expecting("map"));
        }
        let f = f.deref_mut();
        self.visit(&mut MapElementVisitor::new(f)).done();
        Ok(())
    }

    /// Calls a callback on any key item inside the array.
    ///
    /// Calling this on a non-array item returns a [type mismatch error][TypeMismatch].
    ///
    /// An error string returned by the callback is stored in the tree as a comment next to the
    /// item, as is the string in the successful variant.
    ///
    /// # Example
    ///
    /// The [`application::comment_lang_tag`] method is an exampel of a callback function. It is
    /// relatively complex (see below).
    ///
    /// # Future development
    ///
    /// Once `feature(try_trait)` is usable, those return types can be simplified; until then,
    /// using a [`Result`] enables easy propagation of errors out of the callbacks.
    ///
    /// This function is relatively impractical to use: When a callback needs to know its position
    /// in the array (which is a frequent occurrence in inhomogenous arrays), it needs to use
    /// internal state to count up; in doing so it needs to be a closure rather than a function,
    /// and due to [suboptimal lifetimes](https://codeberg.org/chrysn/cbor-edn/issues/9) that means
    /// that the callback may easily need to be boxed.
    pub fn visit_array_elements<F, RF>(&mut self, mut f: RF) -> Result<(), TypeMismatch>
    where
        F: for<'b> FnMut(&mut Item<'b>) -> Result<Option<String>, String> + ?Sized,
        RF: std::ops::DerefMut<Target = F>,
    {
        if !matches!(self.0, InnerItem::Array(_)) {
            return Err(TypeMismatch::expecting("array"));
        }
        let f = f.deref_mut();
        self.visit(&mut ArrayElementVisitor::new(f)).done();
        Ok(())
    }
}

impl Unparse for StandaloneItem<'_> {
    fn serialize_write(&self, formatter: &mut core::fmt::Formatter) -> core::fmt::Result {
        self.0.serialize_write(formatter)?;
        self.1.serialize_write(formatter)?;
        self.2.serialize_write(formatter)?;
        Ok(())
    }

    fn to_cbor(&self) -> Result<impl Iterator<Item = u8>, InconsistentEdn> {
        self.1.to_cbor()
    }
}

impl Unparse for Item<'_> {
    fn serialize_write(&self, formatter: &mut core::fmt::Formatter) -> core::fmt::Result {
        self.0.serialize_write(formatter)
    }

    fn to_cbor(&self) -> Result<impl Iterator<Item = u8>, InconsistentEdn> {
        self.0.to_cbor()
    }
}

impl<'a> From<InnerItem<'a>> for StandaloneItem<'a> {
    fn from(inner: InnerItem<'a>) -> Self {
        Item::from(inner).into()
    }
}

impl<'a> From<Item<'a>> for StandaloneItem<'a> {
    fn from(inner: Item<'a>) -> Self {
        Self(S::default(), inner, S::default())
    }
}

impl<'a> From<InnerItem<'a>> for Item<'a> {
    fn from(inner: InnerItem<'a>) -> Self {
        Item(inner)
    }
}

/// A CBOR Sequence.
///
/// Typical actions on this are the same as on [`StandaloneItem`], but it can process multiple CBOR
/// items in a row, and thus offers interaction with all those items:
///
/// ```
/// # use cbor_edn::*;
/// let from_edn = Sequence::parse("
///     1
///     2
///     3
/// ").unwrap();
/// assert_eq!(from_edn.items().count(), 3);
/// ```
#[derive(Debug, Clone, PartialEq)]
pub struct Sequence<'a> {
    s0: S<'a>,
    items: Option<NonemptyMscVec<'a, Item<'a>>>,
}

impl<'a> Sequence<'a> {
    /// Ingests CBOR Diagnostic Notation (EDN) representing a CBOR sequence
    ///
    /// Note that this will only return syntactic errors. Content errors that make it impossible to
    /// produce this as CBOR, such as non-matching encoding indicators or unknown application
    /// oriented literals, are not reported.
    pub fn parse(s: &'a str) -> Result<Self, ParseError> {
        cbordiagnostic::seq(s).map_err(ParseError)
    }

    /// Produce an EDN String from the sequence
    pub fn serialize(&self) -> String {
        Unparse::serialize(self)
    }

    pub fn from_cbor(cbor: &[u8]) -> Result<Self, CborError> {
        let mut tail = cbor;
        // Could this be more efficient if we returned an iterator? Yes. Would it be easier to
        // maintain? Probably not.
        let mut items = vec![];
        while !tail.is_empty() {
            let (item, new_tail) = Item::from_cbor_with_rest(tail)?;
            items.push(item);
            tail = new_tail;
        }
        let mut s = Self::new(items.into_iter());
        s.set_delimiters(DelimiterPolicy::SingleLineRegularSpacing);
        Ok(s)
    }

    /// Encode into a binary CBOR representation
    pub fn to_cbor(&self) -> Result<Vec<u8>, InconsistentEdn> {
        Ok(Unparse::to_cbor(self)?.collect())
    }

    /// Construct a CBOR sequence from items
    pub fn new(mut items: impl Iterator<Item = Item<'a>>) -> Self {
        Sequence {
            s0: Default::default(),
            items: items.next().map(|first| NonemptyMscVec::new(first, items)),
        }
    }

    /// For each item in the tree that is any element of the squence, call a callback.
    ///
    /// This is primarily used to apply custom EDN filtering:
    ///
    /// ```rust
    /// # use cbor_edn::*;
    /// let mut full = Sequence::parse("0 /unmodified/, german'zweiundvierzig'").unwrap();
    /// full.visit_application_literals(&mut |id, value: String, item: &mut cbor_edn::Item| {
    ///     if id == "german" {
    ///         let numeric = match value.as_str() {
    ///             "dreiundzwanzig" => 23,
    ///             "zweiundvierzig" => 42,
    ///             _ => todo!(),
    ///         };
    ///         *item = Item::new_integer_decimal(numeric).into();
    ///     }
    ///     Ok(())
    /// });
    /// assert_eq!(full.serialize(), "0 /unmodified/, 42");
    /// ```
    pub fn visit_application_literals<F, RF>(&mut self, mut f: RF)
    where
        F: for<'b> FnMut(String, String, &mut Item<'b>) -> Result<(), String> + ?Sized,
        RF: std::ops::DerefMut<Target = F>,
    {
        self.visit(&mut ApplicationLiteralsVisitor {
            user_fn: f.deref_mut(),
        });
    }

    /// For each item in the full tree of any element (including embedded representations) that is
    /// tagged, call a callback.
    ///
    /// Any error string is placed in a comment next to the item. The function should return Ok(())
    /// on any tags it is not interested in visiting.
    ///
    /// This is primarily used to apply custom EDN application; see [application::dt_tag_to_aol] for an
    /// example.
    pub fn visit_tag<F, RF>(&mut self, mut f: RF)
    where
        F: for<'b> FnMut(u64, &mut Item<'b>) -> Result<(), String> + ?Sized,
        RF: std::ops::DerefMut<Target = F>,
    {
        self.visit(&mut TagVisitor {
            user_fn: f.deref_mut(),
        });
    }

    /// Access the items of the sequence
    pub fn items(&self) -> impl Iterator<Item = &Item<'a>> {
        self.items.as_ref().map(|i| i.iter()).into_iter().flatten()
    }

    /// Mutably access the items of the sequence
    pub fn items_mut(&mut self) -> impl Iterator<Item = &mut Item<'a>> {
        self.items
            .as_mut()
            .map(|i| i.iter_mut())
            .into_iter()
            .flatten()
    }

    #[deprecated(note = "renamed to items_mut()")]
    pub fn get_items_mut(&mut self) -> impl Iterator<Item = &mut Item<'a>> {
        self.items_mut()
    }

    /// Removes any encoding indicators present in the sequence.
    ///
    /// This does not affect space or comments; in particular, an item containing only the
    /// necessary space may be left with extraneous (but harmless) space that was previously needed
    /// to set an encoding indicator apart from a value.
    pub fn discard_encoding_indicators(&mut self) {
        for i in self.items_mut() {
            i.discard_encoding_indicators()
        }
    }

    /// Alters how space and comments are placed inside the sequence.
    ///
    /// See the policy values for details.
    pub fn set_delimiters(&mut self, policy: DelimiterPolicy) {
        // On the top level, let's not add the leading \n, because that would cause an empty line
        // above the sole element formatted like this.
        self.s0.set_delimiters(policy, false);
        if let Some(items) = self.items.as_mut() {
            items.first.set_delimiters(policy);
            for (msc, item) in items.tail.iter_mut() {
                msc.set_delimiters(policy, true);
                item.set_delimiters(policy);
            }
            match policy {
                DelimiterPolicy::IndentedRegularSpacing {
                    trailing_newline: TrailingNewlinePolicy::Always,
                    ..
                } => items.soc.set_delimiters(policy, true),
                DelimiterPolicy::IndentedRegularSpacing {
                    trailing_newline: TrailingNewlinePolicy::Never,
                    ..
                } => items.soc.set_delimiters(policy, false),
                // FIXME: Should we be more explicit for the other cases, maybe even abandoning the
                // 2nd argument?
                _ => items.soc.set_delimiters(policy, !items.tail.is_empty()),
            }
        }
        // else, I guess that even under TrailingNewlinePolicy::Always, not sending \n is OK
        // because after all, we did end every line with a newline.
    }

    fn visit(&mut self, visitor: &mut impl Visitor) {
        if let Some(nmv) = self.items.as_mut() {
            nmv.visit(visitor).use_space_after(&mut self.s0).done();
        }
    }

    /// Clone the item, turning any [`Cow::Borrowed`] into owned versions, which can then satisfy
    /// any lifetime.
    pub fn cloned<'any>(&self) -> Sequence<'any> {
        Sequence {
            s0: self.s0.cloned(),
            items: self.items.as_ref().map(|i| i.cloned()),
        }
    }
}

impl Unparse for Sequence<'_> {
    fn serialize_write(&self, formatter: &mut core::fmt::Formatter) -> core::fmt::Result {
        self.s0.serialize_write(formatter)?;
        if let Some(items) = self.items.as_ref() {
            items.serialize_write(formatter)?;
        }
        Ok(())
    }

    fn to_cbor(&self) -> Result<impl Iterator<Item = u8>, InconsistentEdn> {
        let chain = self.items.as_ref().map(|items| items.to_cbor());
        let chain = chain.transpose();
        chain.map(|optit| optit.into_iter().flatten())
    }
}

/// Rule set for the `set_delimiters()` family of methods
#[derive(Copy, Clone, Debug, PartialEq)]
#[non_exhaustive]
pub enum DelimiterPolicy {
    /// Remove all comments, optional space and commas; place commas exactly where in there absence there
    /// would need to be space instead.
    DiscardAll,
    /// Like [`DiscardAll`][DelimiterPolicy::DiscardAll], but leave comments in place.
    DiscardAllButComments,
    /// Set commas where separation is mandatory, followed by a single space; set a single space after colons of key-value pairs.
    ///
    /// All other space and commas are removed. Comments are retained, including space between
    /// adjacent comments.
    SingleLineRegularSpacing,
    /// Replace all space with automated indentation. Comments are left in place, including line
    /// breaks, space and commas inside or between adjacent comments.
    ///
    /// For an easy default construction, see the [`.indented()`](Self::indented) method.
    IndentedRegularSpacing {
        /// Indentation level at the start
        base_indent: usize,
        /// Indentation added per nesting level
        indent_level: usize,
        /// Maximum width of lines that is left as a single item.
        ///
        /// If zero, this will wrap all nested structures; otherwise, it will leave small items
        /// with `SingleLineRegularSpacing`.
        ///
        /// Note that this measures line width in bytes; this is not exact if non-ASCII characters
        /// are involved, but a good enough estimate for most EDN content.
        max_width: usize,
        /// Guides whether
        trailing_newline: TrailingNewlinePolicy,
    },
    /// Set a single space wherever one is allowed.
    ///
    /// This is not a practical policy over-all, but some functions may set this for their
    /// downstream items.
    SingleSpace,
}

impl DelimiterPolicy {
    /// Constructor for [`Self::IndentedRegularSpacing`] with default settings
    pub fn indented() -> Self {
        Self::IndentedRegularSpacing {
            base_indent: 0,
            indent_level: 4,
            max_width: 80,
            trailing_newline: TrailingNewlinePolicy::IfMultiline,
        }
    }

    /// Constructs a policy with default settings (like [`.indented()`][Self::indented]), but
    /// always producing a final newline.
    pub fn indented_with_final_newline() -> Self {
        Self::IndentedRegularSpacing {
            base_indent: 0,
            indent_level: 4,
            max_width: 80,
            trailing_newline: TrailingNewlinePolicy::Always,
        }
    }
}

#[derive(Copy, Clone, Debug, PartialEq)]
pub enum TrailingNewlinePolicy {
    /// Never produces a newline at the end of EDN
    Never,
    /// Produces a newline at the end of EDN if there are any internal newlines
    IfMultiline,
    /// Always produces a newline at the end of EDN
    Always,
}

/// Trait through which a parsed CBOR diagnostic notation item can be turned back into a string
trait Unparse: Sized {
    /// Write the full item into a given formatter
    ///
    /// This is mainly used to implement this trait, but rarely called from the outside.
    fn serialize_write(&self, formatter: &mut core::fmt::Formatter) -> core::fmt::Result;

    /// Produce a String from the full item
    ///
    /// No reason is known to not use the provided method; this is what is usually called on an
    /// item implemlenting this trait.
    fn serialize(&self) -> String {
        struct Unparsed<'a, T: Unparse>(&'a T);
        impl<T: Unparse> core::fmt::Display for Unparsed<'_, T> {
            fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
                self.0.serialize_write(f)
            }
        }

        format!("{}", Unparsed(self))
    }

    fn to_cbor(&self) -> Result<impl Iterator<Item = u8>, InconsistentEdn>;
}

/// This represents a `T *(MSC T) SOC` sequence.
///
/// This type is common to CBOR sequences, streamstrings and array/map, with different mechsnisms of
/// optionality around them ("just have the whole thing None", "there must be at least one" and
/// "the empty variant has a different type (specms vs. spec) next to it").
#[derive(Debug, Clone, PartialEq)]
struct NonemptyMscVec<'a, T: Unparse> {
    // Most users of this are somehow inside Item, and T is usally an item itself -- so we box the
    // T here to avoid recursively sized types.
    first: Box<T>,
    tail: Vec<(MSC<'a>, T)>,
    soc: SOC<'a>,
}

impl<'a, T: Unparse> NonemptyMscVec<'a, T> {
    /// Creates a new instance from just the items, with default space.
    fn new(first: T, tail: impl Iterator<Item = T>) -> Self {
        Self {
            first: Box::new(first),
            tail: tail.map(|i| (Default::default(), i)).collect(),
            soc: Default::default(),
        }
    }

    /// Creates a new instance, taking explicitly all space components (as used in a parser).
    fn new_parsing(first: T, tail: Vec<(MSC<'a>, T)>, soc: SOC<'a>) -> Self {
        Self {
            first: Box::new(first),
            tail,
            soc,
        }
    }

    fn len(&self) -> usize {
        1 + self.tail.len()
    }

    fn iter(&self) -> impl Iterator<Item = &T> {
        core::iter::once(&*self.first).chain(self.tail.iter().map(|(_msc, t)| t))
    }
}

impl<'a> NonemptyMscVec<'a, Item<'a>> {
    fn visit(&mut self, visitor: &mut impl Visitor) -> ProcessResult {
        let mut own_result = self.first.visit(visitor);
        let mut last_result: Option<ProcessResult> = None;
        for (msc, item) in self.tail.iter_mut() {
            if let Some(result) = last_result.take() {
                result.use_space_after(msc).done();
            } else {
                own_result = own_result.use_space_after(msc);
            }
            let item_result = item.visit(visitor);
            let replaced = last_result.replace(item_result.use_space_before(msc));
            assert!(replaced.is_none());
        }
        if let Some(result) = last_result.take() {
            result.use_space_after(&mut self.soc).done();
        } else {
            own_result = own_result.use_space_after(&mut self.soc);
        }

        own_result
    }

    fn cloned<'any>(&self) -> NonemptyMscVec<'any, Item<'any>> {
        NonemptyMscVec {
            first: Box::new(self.first.cloned()),
            tail: self
                .tail
                .iter()
                .map(|(msc, i)| (msc.cloned(), i.cloned()))
                .collect(),
            soc: self.soc.cloned(),
        }
    }
}
// Those ↑ and ↓ are identical, but we don't have a trait for being visit'able and having a
// cloned()… should we?
impl<'a> NonemptyMscVec<'a, Kp<'a>> {
    fn visit(&mut self, visitor: &mut impl Visitor) -> ProcessResult {
        let mut own_result = self.first.visit(visitor);
        let mut last_result: Option<ProcessResult> = None;
        for (msc, item) in self.tail.iter_mut() {
            if let Some(result) = last_result.take() {
                result.use_space_after(msc).done();
            } else {
                own_result = own_result.use_space_after(msc);
            }
            let item_result = item.visit(visitor);
            let replaced = last_result.replace(item_result.use_space_before(msc));
            assert!(replaced.is_none());
        }
        if let Some(result) = last_result.take() {
            result.use_space_after(&mut self.soc).done();
        } else {
            own_result = own_result.use_space_after(&mut self.soc);
        }

        own_result
    }

    fn cloned<'any>(&self) -> NonemptyMscVec<'any, Kp<'any>> {
        NonemptyMscVec {
            first: Box::new(self.first.cloned()),
            tail: self
                .tail
                .iter()
                .map(|(msc, i)| (msc.cloned(), i.cloned()))
                .collect(),
            soc: self.soc.cloned(),
        }
    }
}
// ↓ And that's only needef for around strings
impl<'a> NonemptyMscVec<'a, CborString<'a>> {
    fn cloned<'any>(&self) -> NonemptyMscVec<'any, CborString<'any>> {
        NonemptyMscVec {
            first: Box::new(self.first.cloned()),
            tail: self
                .tail
                .iter()
                .map(|(msc, i)| (msc.cloned(), i.cloned()))
                .collect(),
            soc: self.soc.cloned(),
        }
    }
}

// With feature(precise_capturing), we can use the impl … + use syntax, and unify over T.
// fn iter_mut(&mut self) -> impl Iterator<Item = &mut T> + use<'_, 'a, T> {
macro_rules! nmv_concrete_impl {
    ($t:ident) => {
        impl<'a> NonemptyMscVec<'a, $t<'a>> {
            fn iter_mut(&mut self) -> impl Iterator<Item = &mut $t<'a>> {
                let first: &mut $t<'a> = &mut self.first;
                let tail = &mut self.tail;
                core::iter::once(first).chain(tail.iter_mut().map(|(_msc, i)| i))
            }
        }
    };
}
nmv_concrete_impl!(Item);
nmv_concrete_impl!(CborString);

impl<T: Unparse> Unparse for NonemptyMscVec<'_, T> {
    fn serialize_write(&self, formatter: &mut core::fmt::Formatter) -> core::fmt::Result {
        self.first.serialize_write(formatter)?;
        for (msc, item) in self.tail.iter() {
            msc.serialize_write(formatter)?;
            item.serialize_write(formatter)?;
        }
        self.soc.serialize_write(formatter)?;
        Ok(())
    }

    fn to_cbor(&self) -> Result<impl Iterator<Item = u8>, InconsistentEdn> {
        // Collecting in a vec of inner iterators to flush out the error early
        let collected: Result<Vec<_>, _> = self.iter().map(Unparse::to_cbor).collect();
        Ok(collected?.into_iter().flatten())
    }
}

/// An empty-allowing extension of [`NonemptyMscVec`] where the empty and nonempty versions differ
/// in that the empty version has a spec and the nonempty version has a specms.
///
/// Note that this is a bit funny in that the space after specms is always empty when there is
/// Some spec (because then its inner MS consumes them), whereas when there is no spec, the space
/// lands in the `.s`.
#[derive(Debug, Clone, PartialEq)]
enum SpecMscVec<'a, T: Unparse> {
    Present {
        spec: Option<(Spec, MS<'a>)>,
        s: S<'a>,
        items: NonemptyMscVec<'a, T>,
    },
    Absent {
        spec: Option<Spec>,
        s: S<'a>,
    },
}

impl<T: Unparse> SpecMscVec<'_, T> {
    /// Construct a new list from a spec and items
    fn new(spec: Option<Spec>, mut items: impl Iterator<Item = T>) -> Self {
        if let Some(first) = items.next() {
            // The Some is a bit weird here because the type of SpecMscVec expects Spec to
            // non-nullable; we'll see how this develops once that is removed)
            SpecMscVec::Present {
                spec: spec.map(|spec| (spec, Default::default())),
                s: Default::default(),
                items: NonemptyMscVec::new(first, items),
            }
        } else {
            SpecMscVec::Absent {
                spec,
                s: Default::default(),
            }
        }
    }

    fn len(&self) -> usize {
        match self {
            SpecMscVec::Present { items, .. } => items.len(),
            SpecMscVec::Absent { .. } => 0,
        }
    }

    fn spec(&self) -> Option<Spec> {
        match self {
            SpecMscVec::Present {
                spec: Some((spec, _ms)),
                ..
            } => Some(*spec),
            SpecMscVec::Present { spec: None, .. } => None,
            SpecMscVec::Absent { spec, .. } => *spec,
        }
    }

    fn iter(&self) -> impl Iterator<Item = &T> {
        let (first, tail) = match self {
            SpecMscVec::Absent { .. } => (None, None),
            SpecMscVec::Present {
                items: NonemptyMscVec { first, tail, .. },
                ..
            } => (Some(first.as_ref()), Some(tail)),
        };
        first
            .into_iter()
            .chain(tail.into_iter().flatten().map(|(_msc, i)| i))
    }

    /// Discards the own spec.
    ///
    /// On presence, this discards a single blank character from the MS that becomes the S (for the
    /// common case of the MS just having that mandatory space), but retains any other space
    /// including comments.
    fn discard_own_encoding_indicator(&mut self) {
        match self {
            SpecMscVec::Absent { spec, .. } => *spec = None,
            SpecMscVec::Present { spec, s, .. } => {
                if let Some((_spec, ms)) = spec.take() {
                    if ms != Default::default() {
                        // Most of the time, s is already empty, but during manipulation, it can
                        // get some value too.
                        s.prefix(ms.0);
                    }
                }
            }
        }
    }
}

// With feature(precise_capturing), we can use the impl … + use syntax, and unify over T. When
// restoring the generic form, beware that this will require an explicit lifetime on the impl
// (instead of `impl<T: …> SpecMscVec<'_, T>`).
//
// fn iter_mut(&mut self) -> impl Iterator<Item = &mut T> + use<'_, 'a, T> {
macro_rules! smv_concrete_impl {
    ($t:ident) => {
        impl<'a> SpecMscVec<'a, $t<'a>> {
            fn iter_mut(&mut self) -> impl Iterator<Item = &mut $t<'a>> {
                let (first, tail) = match self {
                    SpecMscVec::Absent { .. } => (None, None),
                    SpecMscVec::Present {
                        items: NonemptyMscVec { first, tail, .. },
                        ..
                    } => (Some(first.as_mut()), Some(tail)),
                };
                first
                    .into_iter()
                    .chain(tail.into_iter().flatten().map(|(_msc, i)| i))
            }

            // This one is not sufferyng from feature(precise_capt) but from our .cloned() not being a
            // trait method.
            fn cloned<'any>(&self) -> SpecMscVec<'any, $t<'any>> {
                match self {
                    SpecMscVec::Present { spec, s, items } => SpecMscVec::Present {
                        spec: spec.as_ref().map(|(spec, ms)| (*spec, ms.cloned())),
                        s: s.cloned(),
                        items: items.cloned(),
                    },
                    SpecMscVec::Absent { spec, s } => SpecMscVec::Absent {
                        spec: spec.map(|s| s.clone()),
                        s: s.cloned(),
                    },
                }
            }
        }
    };
}
smv_concrete_impl!(Item);
smv_concrete_impl!(Kp);

impl<'a> SpecMscVec<'a, Item<'a>> {
    fn visit(&mut self, visitor: &mut impl Visitor) {
        match self {
            SpecMscVec::Present { spec: _, s, items } => {
                // anything to after the last item is processed internally
                items.visit(visitor).use_space_before(s).done();
            }
            SpecMscVec::Absent { spec: _, s: _ } => (),
        }
    }
}
// Those ↑ and ↓ are identical, but we don't have a trait for being visit'able … should we?
impl<'a> SpecMscVec<'a, Kp<'a>> {
    fn visit(&mut self, visitor: &mut impl Visitor) {
        match self {
            SpecMscVec::Present { spec: _, s, items } => {
                // anything to after the last item is processed internally
                items.visit(visitor).use_space_before(s).done();
            }
            SpecMscVec::Absent { spec: _, s: _ } => (),
        }
    }
}

impl<T: Unparse> Unparse for SpecMscVec<'_, T> {
    fn serialize_write(&self, formatter: &mut core::fmt::Formatter) -> core::fmt::Result {
        match self {
            SpecMscVec::Present { spec, s, items } => {
                if let Some((spec, msc)) = spec {
                    spec.serialize_write(formatter)?;
                    msc.serialize_write(formatter)?;
                }
                s.serialize_write(formatter)?;
                items.serialize_write(formatter)?;
                Ok(())
            }
            SpecMscVec::Absent { spec, s } => {
                if let Some(spec) = spec {
                    spec.serialize_write(formatter)?;
                }
                s.serialize_write(formatter)?;
                Ok(())
            }
        }
    }

    // This writes just the CBOR items; it is up to the caller to process the spec.
    fn to_cbor(&self) -> Result<impl Iterator<Item = u8>, InconsistentEdn> {
        // FIXME: Or is this just now the point to split Unparse and not implement the CBOR side?

        // Collecting in a vec of inner iterators to flush out the error early
        let collected: Result<Vec<_>, _> = self.iter().map(Unparse::to_cbor).collect();
        Ok(collected?.into_iter().flatten())
    }
}

/// A key-value pair of CBOR items, both surrounded by [S]pace, separated by a ":"
#[derive(Debug, Clone, PartialEq)]
struct Kp<'a> {
    key: Item<'a>,
    s0: S<'a>,
    s1: S<'a>,
    value: Item<'a>,
}

impl<'a> Kp<'a> {
    fn new(key: Item<'a>, value: Item<'a>) -> Self {
        Self {
            key,
            s0: Default::default(),
            s1: Default::default(),
            value,
        }
    }

    fn visit(&mut self, visitor: &mut impl Visitor) -> ProcessResult {
        let key_result = self.key.visit(visitor);
        let value_result = self.value.visit(visitor);
        key_result
            .use_space_after(&mut self.s0)
            .chain(value_result.use_space_before(&mut self.s1))
    }

    fn cloned<'any>(&self) -> Kp<'any> {
        Kp {
            key: self.key.cloned(),
            s0: self.s0.cloned(),
            s1: self.s1.cloned(),
            value: self.value.cloned(),
        }
    }
}

impl Unparse for Kp<'_> {
    fn serialize_write(&self, formatter: &mut core::fmt::Formatter) -> core::fmt::Result {
        self.key.serialize_write(formatter)?;
        self.s0.serialize_write(formatter)?;
        formatter.write_str(":")?;
        self.s1.serialize_write(formatter)?;
        self.value.serialize_write(formatter)?;
        Ok(())
    }

    fn to_cbor(&self) -> Result<impl Iterator<Item = u8>, InconsistentEdn> {
        Ok([self.key.to_cbor()?, self.value.to_cbor()?]
            .into_iter()
            .flatten())
    }
}

#[derive(Debug, Clone, PartialEq)]
enum Simple<'a> {
    False,
    True,
    Null,
    Undefined,
    // Note that later processing may be upset if the string is not a Number item, but cpa'something' may make sense
    Numeric(Box<StandaloneItem<'a>>),
}
impl Simple<'_> {
    pub(crate) fn cloned<'any>(&self) -> Simple<'any> {
        match self {
            Simple::False => Simple::False,
            Simple::True => Simple::True,
            Simple::Null => Simple::Null,
            Simple::Undefined => Simple::Undefined,
            Simple::Numeric(standalone_item) => Simple::Numeric(Box::new(standalone_item.cloned())),
        }
    }
}

impl Unparse for Simple<'_> {
    fn serialize_write(&self, formatter: &mut core::fmt::Formatter) -> core::fmt::Result {
        match self {
            Simple::False => formatter.write_str("false")?,
            Simple::True => formatter.write_str("true")?,
            Simple::Null => formatter.write_str("null")?,
            Simple::Undefined => formatter.write_str("undefined")?,
            Simple::Numeric(i) => {
                formatter.write_str("simple(")?;
                i.serialize_write(formatter)?;
                formatter.write_str(")")?;
            }
        }
        Ok(())
    }

    fn to_cbor(&self) -> Result<impl Iterator<Item = u8>, InconsistentEdn> {
        let mut result = Vec::new();
        match self {
            Simple::False => result.push(0xf4),
            Simple::True => result.push(0xf5),
            Simple::Null => result.push(0xf6),
            Simple::Undefined => result.push(0xf7),
            Simple::Numeric(i) => {
                let InnerItem::Number(ref number, spec) = i.inner() else {
                    return Err(InconsistentEdn(
                        "Items inside simple() need to be numbers for serialization.",
                    ));
                };
                let NumberValue::Positive(number) = number.value() else {
                    return Err(InconsistentEdn(
                        "Non-positive numbers can not be in a Simple",
                    ));
                };
                if number > 255 {
                    return Err(InconsistentEdn("Spec exceeds valid range of 0..=255"));
                }
                let requested = Spec::encode_argument(spec.as_ref(), Major::FloatSimple, number)?;
                let permissible = Spec::encode_argument(None, Major::FloatSimple, number)?;
                if requested != permissible {
                    return Err(InconsistentEdn(
                        "Encoding indicators on simple value must use the preferred encoding",
                    ));
                }
                result.extend(permissible);
            }
        };
        Ok(result.into_iter())
    }
}

impl<'a> From<Simple<'a>> for Item<'a> {
    fn from(input: Simple<'a>) -> Self {
        InnerItem::Simple(input).into()
    }
}

/// An arbitrary CBOR item
#[derive(Clone, Debug, PartialEq)]
enum InnerItem<'a> {
    Map(SpecMscVec<'a, Kp<'a>>),
    Array(SpecMscVec<'a, Item<'a>>),
    Tagged(u64, Option<Spec>, Box<StandaloneItem<'a>>),
    /// Stored as a string, but we could also explicitly capture the variation:
    /// * is a sign present? (even in an integer negative 0?)
    /// * what is the base?
    /// * how many leading zeros are there?
    /// * is there an explicit power (and if so, does it have an explicit sign, or leading zeros?)
    /// * note that there are no inner spaces or underscors: no "1 000 000" or "1_000_000", the
    ///   latter would conflict with encoding indicators.
    ///
    /// (and it can be arbitrarily long, exceeding a u64)
    Number(Number<'a>, Option<Spec>),
    Simple(Simple<'a>),
    String(CborString<'a>),
    StreamString(MS<'a>, NonemptyMscVec<'a, CborString<'a>>),
}

impl InnerItem<'_> {
    /// Discard any encoding indicators ([Spec]) that may be part of the item
    fn discard_encoding_indicators(&mut self) {
        match self {
            InnerItem::Map(items) => {
                for i in items.iter_mut() {
                    i.key.discard_encoding_indicators();
                    i.value.discard_encoding_indicators();
                }
                items.discard_own_encoding_indicator();
            }
            InnerItem::Array(items) => {
                for i in items.iter_mut() {
                    i.discard_encoding_indicators();
                }
                items.discard_own_encoding_indicator();
            }
            InnerItem::Tagged(_n, spec, item) => {
                *spec = None;
                item.item_mut().discard_encoding_indicators();
            }
            InnerItem::Number(_n, spec) => {
                *spec = None;
            }
            InnerItem::Simple(Simple::Numeric(i)) => i.item_mut().discard_encoding_indicators(),
            InnerItem::Simple(_) => {}
            InnerItem::String(items) => {
                items.discard_encoding_indicators();
            }
            InnerItem::StreamString(_ms, items) => {
                // FIXME: Shouldn't this just become String? (StreamString is kind of an encoding
                // indicator)
                for i in items.iter_mut() {
                    i.discard_encoding_indicators();
                }
            }
        }
    }

    fn set_delimiters(&mut self, policy: DelimiterPolicy) {
        use DelimiterPolicy::*;

        let nested_policy = if let IndentedRegularSpacing {
            base_indent,
            indent_level,
            max_width,
            trailing_newline,
        } = policy
        {
            // Try fitting it in one line; that doesn't do anything that won't be changed by proper
            // indentation later anyway, so we don't need to roll back.
            self.set_delimiters(SingleLineRegularSpacing);
            if self.serialize().len() + base_indent < max_width {
                return;
            }

            IndentedRegularSpacing {
                base_indent: base_indent + indent_level,
                indent_level,
                max_width,
                trailing_newline,
            }
        } else {
            policy
        };

        match self {
            InnerItem::Map(items) => match items {
                SpecMscVec::Absent { s, .. } => s.set_delimiters(nested_policy, false),
                SpecMscVec::Present { s, items, .. } => {
                    s.set_delimiters(nested_policy, true);
                    let set_on_item = |kp: &mut Kp| {
                        kp.key.set_delimiters(nested_policy);
                        kp.value.set_delimiters(nested_policy);
                        kp.s0.set_delimiters(nested_policy, false);
                        if matches!(policy, SingleLineRegularSpacing) {
                            kp.s1.0 = " ".into();
                        } else {
                            // Or true … but that may need an extra case in the top-level
                            // inden`ted-to-single-line logic
                            kp.s1.set_delimiters(nested_policy, false);
                        }
                    };
                    set_on_item(&mut items.first);
                    for (msc, item) in items.tail.iter_mut() {
                        set_on_item(item);
                        msc.set_delimiters(nested_policy, true);
                    }
                    items.soc.set_delimiters(policy, true);
                }
            },
            InnerItem::Array(items) => match items {
                SpecMscVec::Absent { s, .. } => s.set_delimiters(nested_policy, false),
                SpecMscVec::Present { s, items, .. } => {
                    s.set_delimiters(nested_policy, true);
                    items.first.set_delimiters(nested_policy);
                    for (msc, item) in items.tail.iter_mut() {
                        item.set_delimiters(nested_policy);
                        msc.set_delimiters(nested_policy, true);
                    }
                    items.soc.set_delimiters(policy, true);
                }
            },
            InnerItem::Tagged(_n, _spec, item) => {
                item.set_delimiters(nested_policy);
            }
            InnerItem::Number(_n, _spec) => {}
            InnerItem::Simple(Simple::Numeric(item)) => {
                // Setting the nested_policy on the item as a whole would lead to unsightly indentation --
                // setting it piecemeal instead.
                item.0.set_delimiters(nested_policy, false);
                item.1.set_delimiters(nested_policy);
                item.2.set_delimiters(nested_policy, false);
            }
            InnerItem::Simple(_) => {}
            InnerItem::String(CborString { items, separators }) => {
                for i in items {
                    i.set_delimiters(nested_policy);
                }
                for (sep_pre, sep_post) in separators {
                    match nested_policy {
                        SingleLineRegularSpacing => {
                            sep_pre.set_delimiters(SingleSpace, true);
                            sep_post.set_delimiters(SingleSpace, false);
                        }
                        _ => {
                            sep_pre.set_delimiters(nested_policy, true);
                            sep_post.set_delimiters(nested_policy, false);
                        }
                    }
                }
            }
            InnerItem::StreamString(ms, NonemptyMscVec { first, tail, soc }) => {
                ms.set_delimiters(nested_policy, true);
                first.set_delimiters(nested_policy);
                for (ms, item) in tail {
                    ms.set_delimiters(nested_policy, true);
                    item.set_delimiters(nested_policy);
                }
                soc.set_delimiters(policy, true);
            }
        }
    }

    fn visit(&mut self, visitor: &mut impl Visitor) {
        match self {
            InnerItem::Map(spec_msc_vec) => {
                spec_msc_vec.visit(visitor);
            }
            InnerItem::Array(spec_msc_vec) => {
                spec_msc_vec.visit(visitor);
            }
            InnerItem::Tagged(_number, _spec, standalone_item) => {
                // This mainly returns no ProcessResult because comments can well be placed inside
                // the item -- but if someone really wants to act on the outside, that could be
                // taken through here.
                standalone_item.visit(visitor);
            }
            InnerItem::Number(_number, _spec) => (),
            InnerItem::Simple(_simple) => (),
            InnerItem::String(_cbor_string) => (),
            InnerItem::StreamString(_ms, _nonempty_msc_vec) => (),
        }
    }

    fn cloned<'any>(&self) -> InnerItem<'any> {
        match self {
            InnerItem::Map(spec_msc_vec) => InnerItem::Map(spec_msc_vec.cloned()),
            InnerItem::Array(spec_msc_vec) => InnerItem::Array(spec_msc_vec.cloned()),
            InnerItem::Tagged(tag, spec, standalone_item) => {
                InnerItem::Tagged(*tag, *spec, Box::new(standalone_item.cloned()))
            }
            InnerItem::Number(number, spec) => InnerItem::Number(number.cloned(), *spec),
            InnerItem::Simple(simple) => InnerItem::Simple(simple.cloned()),
            InnerItem::String(cbor_string) => InnerItem::String(cbor_string.cloned()),
            InnerItem::StreamString(ms, nonempty_msc_vec) => {
                InnerItem::StreamString(ms.cloned(), nonempty_msc_vec.cloned())
            }
        }
    }
}

impl Unparse for InnerItem<'_> {
    fn serialize_write(&self, formatter: &mut core::fmt::Formatter) -> core::fmt::Result {
        match self {
            InnerItem::Map(items) => {
                write!(formatter, "{{")?;
                items.serialize_write(formatter)?;
                write!(formatter, "}}")?;
                Ok(())
            }
            InnerItem::Array(items) => {
                write!(formatter, "[")?;
                items.serialize_write(formatter)?;
                write!(formatter, "]")?;
                Ok(())
            }
            InnerItem::Tagged(n, spec, item) => {
                write!(formatter, "{}", n)?;
                if let Some(spec) = spec {
                    spec.serialize_write(formatter)?;
                }
                formatter.write_str("(")?;
                item.serialize_write(formatter)?;
                formatter.write_str(")")?;
                Ok(())
            }
            InnerItem::Number(n, spec) => {
                formatter.write_str(&n.0)?;
                if let Some(spec) = spec {
                    spec.serialize_write(formatter)?;
                }
                Ok(())
            }
            InnerItem::Simple(s) => s.serialize_write(formatter),
            InnerItem::String(s) => s.serialize_write(formatter),
            InnerItem::StreamString(ms, nmv) => {
                formatter.write_str("(_")?;
                ms.serialize_write(formatter)?;
                nmv.serialize_write(formatter)?;
                formatter.write_str(")")?;
                Ok(())
            }
        }
    }

    fn to_cbor(&self) -> Result<impl Iterator<Item = u8>, InconsistentEdn> {
        let mut result = vec![];
        match self {
            InnerItem::Map(smv) => {
                let len = smv.len();
                let spec = smv.spec();
                let (head, tail) = Spec::encode_item_count(spec.as_ref(), Major::Map, len)?;
                result.extend(head);
                for i in smv.iter() {
                    result.extend(i.to_cbor()?);
                }
                result.extend(tail);
            }
            InnerItem::Array(smv) => {
                let len = smv.len();
                let spec = smv.spec();
                let (head, tail) = Spec::encode_item_count(spec.as_ref(), Major::Array, len)?;
                result.extend(head);
                for i in smv.iter() {
                    result.extend(i.to_cbor()?);
                }
                result.extend(tail);
            }
            InnerItem::Tagged(n, spec, item) => {
                result.extend(Spec::encode_argument(spec.as_ref(), Major::Tagged, *n)?);
                result.extend(item.to_cbor()?);
            }
            InnerItem::Number(n, spec) => match n.value() {
                NumberValue::Positive(n) => {
                    result.extend(Spec::encode_argument(spec.as_ref(), Major::Unsigned, n)?)
                }
                NumberValue::Negative(n) => {
                    result.extend(Spec::encode_argument(spec.as_ref(), Major::Negative, n)?)
                }
                NumberValue::Float(n) => result.extend(float::encode(n, *spec)?),
                NumberValue::Big(n) => match spec {
                    None => {
                        let (tag, positive) = if n >= num_bigint::BigInt::ZERO {
                            (2, n)
                        } else {
                            (3, -n)
                        };
                        use num_traits::ops::bytes::ToBytes;
                        result.extend(Spec::encode_argument(None, Major::Tagged, tag)?);
                        let bytes = positive.to_be_bytes();
                        result.extend(Spec::encode_argument(
                            None,
                            Major::ByteString,
                            bytes
                                .len()
                                .try_into()
                                .expect("Even on 128-bit systems, EDN does not exceed 64bit sizes"),
                        )?);
                        result.extend(bytes);
                    }
                    _ => {
                        return Err(InconsistentEdn(
                            "Encoding indicators not specified for bignums",
                        ))
                    }
                },
            },
            InnerItem::Simple(s) => result.extend(s.to_cbor()?),
            InnerItem::String(s) => result.extend(s.to_cbor()?),
            InnerItem::StreamString(_ms, NonemptyMscVec { first, tail, .. }) => {
                let major = first.encoded_major_type()?;
                if !matches!(major, Major::TextString | Major::ByteString) {
                    // Syntax can't catch this: Might be an application oriented literal that is
                    // not string-valued
                    return Err(InconsistentEdn(
                        "Item in indefinite length string that is neither bytes nor string",
                    ));
                }
                result.push(((major as u8) << 5) | 31);
                result.extend(first.to_cbor()?);
                for item in tail.iter() {
                    if item.1.encoded_major_type()? != major {
                        return Err(InconsistentEdn("Item in indefinite length string has different encoding than head element"));
                    }
                    result.extend(item.1.to_cbor()?);
                }
                result.push(0xff);
            }
        }
        Ok(result.into_iter())
    }
}

#[derive(PartialEq, Debug, Copy, Clone)]
enum Major {
    Unsigned = 0,
    Negative = 1,
    ByteString = 2,
    TextString = 3,
    Array = 4,
    Map = 5,
    Tagged = 6,
    FloatSimple = 7,
}

impl Major {
    /// Given a byte, return its major type and the additional information.
    fn from_byte(byte: u8) -> (Self, u8) {
        (
            match byte >> 5 {
                0 => Major::Unsigned,
                1 => Major::Negative,
                2 => Major::ByteString,
                3 => Major::TextString,
                4 => Major::Array,
                5 => Major::Map,
                6 => Major::Tagged,
                7 => Major::FloatSimple,
                _ => unreachable!(),
            },
            byte & 0x1f,
        )
    }
}

/// An encoding indicator
///
/// Encoding indicators are typically rendered with an underscore, eg. in `4_1`, `_1` is the
/// encoding indicator `Spec("1")`, and tells that the number 4 was encoded in more bytes than
/// would have been needed.
///
/// While encoding indicators are described as an extensible registry, new values would interfere
/// so deeply with this crate's operation that they would need a code change; consequently, unknown
/// values are rejected at parsing time.
#[derive(Copy, Clone, Debug, PartialEq)]
#[allow(non_camel_case_types)] // reason: underscores are part of what we express here
enum Spec {
    S_,
    S_i,
    S_0,
    S_1,
    S_2,
    S_3,
}

impl Spec {
    /// Given an item count, produce the encoded item count for a given Major type (only makes
    /// sense for an array and map), as well as any terminator that'd be necessary after the list
    /// in case of in indefinite length encoding
    fn encode_item_count(
        self_: Option<&Self>,
        major: Major,
        count: usize,
    ) -> Result<(Vec<u8>, &[u8]), InconsistentEdn> {
        debug_assert!(matches!(major, Major::Map | Major::Array), "Encoding an item count only makes see for maps and arrays; strings work a bit different.");
        Ok((
            Spec::encode_argument(self_, major, count.try_into().expect("Even on 128bit architectures we can't have more than 64bit long counts of items"))?,
            if matches!(self_, Some(Spec::S_)) { [0xff].as_slice() } else { [].as_slice() },
        ))
    }

    fn encode_argument(
        self_: Option<&Self>,
        major: Major,
        argument: u64,
    ) -> Result<Vec<u8>, InconsistentEdn> {
        let full_spec = match (self_, argument) {
            (None, 0..=23) => Self::S_i,
            (None, 0..=U8MAX) => Self::S_0,
            (None, 0..=U16MAX) => Self::S_1,
            (None, 0..=U32MAX) => Self::S_2,
            (None, _) => Self::S_3,
            (Some(s), _) => *s,
        };

        let immediate_value = match full_spec {
            Self::S_ => 31,
            Self::S_i => {
                if argument < 24 {
                    argument as u8
                } else {
                    return Err(InconsistentEdn(
                        "Immediate encoding demanded but value exceeds 23",
                    ));
                }
            }
            Self::S_0 => 24,
            Self::S_1 => 25,
            Self::S_2 => 26,
            Self::S_3 => 27,
        };
        let first = core::iter::once(((major as u8) << 5) | immediate_value);
        Ok(match full_spec {
            Self::S_ | Self::S_i => first.collect(),
            Self::S_0 => first.chain(u8::try_from(argument)?.to_be_bytes()).collect(),
            Self::S_1 => first
                .chain(u16::try_from(argument)?.to_be_bytes())
                .collect(),
            Self::S_2 => first
                .chain(u32::try_from(argument)?.to_be_bytes())
                .collect(),
            Self::S_3 => first.chain(argument.to_be_bytes()).collect(),
        })
    }

    fn serialize_write(&self, formatter: &mut core::fmt::Formatter) -> core::fmt::Result {
        match self {
            Self::S_ => formatter.write_str("_"),
            Self::S_i => formatter.write_str("_i"),
            Self::S_0 => formatter.write_str("_0"),
            Self::S_1 => formatter.write_str("_1"),
            Self::S_2 => formatter.write_str("_2"),
            Self::S_3 => formatter.write_str("_3"),
        }
    }

    /// Return None if the integer argument leads to self being selected in preferred encoding
    /// anyway.
    ///
    /// We can't do this in [process_cbor_major_argument] because floats are not so trivial to
    /// classify.
    fn or_none_if_default_for_arg(self, arg: u64) -> Option<Self> {
        const U8MAXPLUS: u64 = U8MAX + 1;
        const U16MAXPLUS: u64 = U16MAX + 1;
        const U32MAXPLUS: u64 = U32MAX + 1;
        match (self, arg) {
            (Spec::S_i, 0..=23) => None,
            (Spec::S_0, 24..=U8MAX) => None,
            (Spec::S_1, U8MAXPLUS..=U16MAX) => None,
            (Spec::S_2, U16MAXPLUS..=U32MAX) => None,
            (Spec::S_3, U32MAXPLUS..=u64::MAX) => None,
            (s, _) => Some(s),
        }
    }
}

impl core::str::FromStr for Spec {
    type Err = &'static str;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "" => Ok(Self::S_),
            "i" => Ok(Self::S_i),
            "0" => Ok(Self::S_0),
            "1" => Ok(Self::S_1),
            "2" => Ok(Self::S_2),
            "3" => Ok(Self::S_3),
            _ => Err("Unsupported encoding indicator"),
        }
    }
}

/// From a byte string, process the first and subsequent bytes into a major type, an argument, and
/// a spec
///
/// Spec will be S_ iff the [`Option<u64>`] is none.
#[allow(clippy::type_complexity)]
// reason: All items make sense here, and it is an internal function used in situations when you
// would expect those very items.
fn process_cbor_major_argument(
    cbor: &[u8],
) -> Result<(Major, Option<u64>, Spec, &[u8]), CborError> {
    // It would be tempting to use minicbor or another CBOR implementation, but they don't
    // expose which option was chosen for argument, so we are on our own, because we need that
    // information for encoding indicators.
    let head = cbor
        .first()
        .ok_or(CborError("Expected item, out of data"))?;

    let (major, additional) = Major::from_byte(*head);
    let tail = &cbor[1..];

    let (argument, spec, skip): (Option<u64>, _, _) = match additional {
        0..=23 => (Some(additional.into()), Spec::S_i, 0),
        24 => (
            Some(
                tail.first()
                    .copied()
                    .ok_or(CborError("Missing 1 byte"))?
                    .into(),
            ),
            Spec::S_0,
            1,
        ),
        25 => (
            Some(
                u16::from_be_bytes(
                    tail.get(..2)
                        .ok_or(CborError("Missing 2 bytes"))?
                        .try_into()
                        .unwrap(),
                )
                .into(),
            ),
            Spec::S_1,
            2,
        ),
        26 => (
            Some(
                u32::from_be_bytes(
                    tail.get(..4)
                        .ok_or(CborError("Missing 4 bytes"))?
                        .try_into()
                        .unwrap(),
                )
                .into(),
            ),
            Spec::S_2,
            4,
        ),
        27 => (
            Some(u64::from_be_bytes(
                tail.get(..8)
                    .ok_or(CborError("Missing 8 bytes"))?
                    .try_into()
                    .unwrap(),
            )),
            Spec::S_3,
            8,
        ),
        31 => (None, Spec::S_, 0),
        _ => return Err(CborError("Reserved header byte")),
    };

    Ok((major, argument, spec, &tail[skip..]))
}

peg::parser! { grammar cbordiagnostic() for str {

// seq             = S [item *(MSC item) SOC]
    pub rule seq() -> Sequence<'input>
        = s0:S() items:(first:item() tail:(msc:MSC() inner:item() { (msc, inner) })* soc:SOC() { NonemptyMscVec::new_parsing(first, tail, soc) })? {
            Sequence { s0, items }
        }


// one-item        = S item S
    pub rule one_item() -> StandaloneItem<'input>
        = s1:S() i:item() s2:S() { StandaloneItem(s1, i, s2) }

// item            = map / array / tagged
//                 / number / simple
//                 / string / streamstring
    rule item() -> Item<'input>
        = inner:(map() / array() / tagged() /
          number() / simple() /
          string:string() { InnerItem::String(string) } / streamstring()) { inner.into() }

// string1         = (tstr / bstr) spec
    rule string1() -> String1e<'input>
        = value:$(tstr() / bstr()) spec:spec() {?
            Ok(if value.starts_with("<<") {
                // FIXME: How can we propagate the parsing we already did instead of parsing again
                // and having bad error handling?
                String1e::EmbeddedChunk(cbordiagnostic::seq(&value[2..value.len() - 2]).map_err(|_| "Parse error in embedded CBOR")?, spec)
            } else {
                String1e::TextChunk(Cow::Borrowed(value), spec)
            })
        }
// string1e        = string1 / ellipsis
    rule string1e() -> String1e<'input>
        = string1() / ellipsis()
// ellipsis        = 3*"." ; "..." or more dots
    rule ellipsis() -> String1e<'input>
        = dots:$("."*<3,>) { String1e::Ellipsis(dots.len()) }
// string          = string1e *(S "+" S string1e)
    rule string() -> CborString<'input>
        = head:string1e() tail:(separator:S() "+" s1:S() inner:string1e() { (separator, s1, inner) })* {
            CborString {
                items: core::iter::once(head).chain(tail.iter().map(|(_sep_pre, _sep_post, inner)| inner).cloned()).collect(),
                separators: tail.iter().map(|(sep_pre, sep_post, _inner)| (sep_pre.clone(), sep_post.clone())).collect()
            }
        }

// number          = (hexfloat / hexint / octint / binint
//                    / decnumber / nonfin) spec
    rule number() -> InnerItem<'input>
        = num:$((hexfloat() / hexint() / octint() / binint() / decnumber() / nonfin())) spec:spec() {InnerItem::Number(Number(Cow::Borrowed(num)), spec)}

// sign            = "+" / "-"
    rule sign() -> Sign
        = "+" { Sign::Plus } / "-" { Sign::Minus }

// decnumber       = [sign] (1*DIGIT ["." *DIGIT] / "." 1*DIGIT)
//                          ["e" [sign] 1*DIGIT]
    pub rule decnumber() -> NumberParts<'input>
        = sign:sign()? prepost:(predot:$(DIGIT()+) postdot:("." postdot:$(DIGIT()*) { postdot })? { (predot, postdot) } / "." postdot:$(DIGIT()+) { ("", Some(postdot)) })
                         exponent:(['e'|'E'] sign:sign()? exponent:$(DIGIT()+) {(sign, exponent)})?
        {
            let (predot, postdot) = prepost;
            NumberParts {
                base: 10,
                sign,
                predot,
                postdot,
                exponent,
            }
        }
// hexfloat        = [sign] "0x" (1*HEXDIG ["." *HEXDIG] / "." 1*HEXDIG)
//                          "p" [sign] 1*DIGIT
   pub rule hexfloat() -> NumberParts<'input>
       = sign:sign()?
       "0" ['x'|'X']
       prepost:(
           predot:$(HEXDIG()+) postdot:("." postdot:$(HEXDIG()*) { postdot })?
           { (Some(predot), postdot) }
           / "." postdot:$(HEXDIG()+)
           { (None, Some(postdot)) }
       )
       ['p'|'P']
       expsign:sign()?
       exp:$(DIGIT()+)
       {
           NumberParts {
               base: 16,
               sign,
               predot: prepost.0.unwrap_or(""),
               postdot: prepost.1,
               exponent: Some((expsign, exp))
           }
       }
// hexint          = [sign] "0x" 1*HEXDIG
   pub rule hexint() -> NumberParts<'input>
       = sign:sign()? "0" ['x'|'X'] predot:$(HEXDIG()+) { NumberParts {base: 16, sign, predot, postdot: None, exponent: None} }
// octint          = [sign] "0o" 1*ODIGIT
   pub rule octint() -> NumberParts<'input>
       = sign:sign()? "0" ['o'|'O'] predot:$(ODIGIT()+) { NumberParts {base: 8, sign, predot, postdot: None, exponent: None} }
// binint          = [sign] "0b" 1*BDIGIT
   pub rule binint() -> NumberParts<'input>
       = sign:sign()? "0" ['b'|'B'] predot:$(BDIGIT()+) { NumberParts {base: 2, sign, predot, postdot: None, exponent: None} }
// nonfin          = %s"Infinity"
//                 / %s"-Infinity"
//                 / %s"NaN"
    rule nonfin()
        = "Infinity" / "-Infinity" / "NaN"
// simple          = %s"false"
//                 / %s"true"
//                 / %s"null"
//                 / %s"undefined"
//                 / %s"simple(" S item S ")"
    rule simple() -> InnerItem<'input>
        = "false" { InnerItem::Simple(Simple::False) }
                / "true" { InnerItem::Simple(Simple::True) }
                / "null" { InnerItem::Simple(Simple::Null) }
                / "undefined" { InnerItem::Simple(Simple::Undefined) }
                / "simple(" s1:S() i:item() s2:S() ")" {InnerItem::Simple(Simple::Numeric(Box::new(StandaloneItem(s1, i, s2))))}
// uint            = "0" / DIGIT1 *DIGIT
    rule uint() -> u64
        = n:$("0" / DIGIT1() DIGIT()*) {? n.parse().or(Err("Exceeding tag space")) }
// tagged          = uint spec "(" S item S ")"
    rule tagged() -> InnerItem<'input>
        = tag:uint() tagspec:spec() "(" s0:S() value:item() s1:S() ")" { InnerItem::Tagged(tag, tagspec, Box::new(StandaloneItem(s0, value, s1))) }

// app-prefix      = lcalpha *lcalnum ; including h and b64
//                 / ucalpha *ucalnum ; tagged variant, if defined
    pub rule app_prefix() =
        quiet!{lcalpha() lcalnum()* / ucalpha() ucalnum()*} / expected!("application prefix")
// app-string      = app-prefix sqstr
    pub rule app_string() -> (&'input str, String)
        = prefix:$(app_prefix()) data:sqstr() { (prefix, data) }
// sqstr           = SQUOTE *single-quoted SQUOTE
    pub rule sqstr() -> String // Yes it is String: Just because they can contain binary doesn't mean
                               // that the ABNF allows it -- no '\xff'.
        = SQUOTE() sqstr:single_quoted()* SQUOTE() { sqstr.iter().filter_map(|c| *c).collect() }
// bstr            = app-string / sqstr / embedded
//                   ; app-string could be any type
    rule bstr()
        = app_string() / sqstr() / embedded()
// tstr            = DQUOTE *double-quoted DQUOTE
    pub rule tstr() -> String
        = DQUOTE() text:double_quoted()* DQUOTE() { text.iter().filter_map(|c| *c).collect() }

// embedded        = "<<" seq ">>"
    rule embedded()
        = "<<" seq() ">>"

// array           = "[" (specms S item *(MSC item) SOC / spec S) "]"
    rule array() -> InnerItem<'input>
        = "[" array:(
            spec:specms() s:S() first:item() tail:(msc:MSC() inner:item() { (msc, inner) })* soc:SOC()
            { SpecMscVec::Present { spec, s, items: NonemptyMscVec::new_parsing(first, tail, soc) } }
            / spec:spec() s:S()
            { SpecMscVec::Absent { spec, s } }
            ) "]"
        { InnerItem::Array(array) }
// map             = "{" (specms S keyp *(MSC keyp) SOC / spec S) "}"
    rule map() -> InnerItem<'input>
        = "{" map:(
            spec:specms() s:S() first:keyp() tail:(msc:MSC() inner:keyp() { (msc, inner) })* soc:SOC()
            { SpecMscVec::Present { spec, s, items: NonemptyMscVec::new_parsing(first, tail, soc) } }
            / spec:spec() s:S()
            { SpecMscVec::Absent { spec, s } }
            ) "}"
        { InnerItem::Map(map) }
// keyp            = item S ":" S item
    rule keyp() -> Kp<'input>
        = key:item() s0:S() ":" s1:S() value:item() { Kp { key, s0, s1, value } }

// ; We allow %x09 HT in prose, but not in strings
// blank           = %x09 / %x0A / %x0D / %x20
    rule blank() -> ()
        = quiet!{"\x09" / "\x0A" / "\x0D" / "\x20"} / expected!("tabs, spaces or newlines")

// non-slash       = blank / %x21-2e / %x30-D7FF / %xE000-10FFFF
    rule non_slash() -> ()
        = blank() / ['\x21'..='\x2e' | '\x30'..='\u{D7FF}' | '\u{E000}'..='\u{10FFFF}'] {}
// non-lf          = %x09 / %x0D / %x20-D7FF / %xE000-10FFFF
    rule non_lf() -> ()
        = ['\x09' | '\x0D' | '\x20'..='\u{D7FF}' | '\u{E000}'..='\u{10FFFF}'] {}

// comment         = "/" *non-slash "/"
//                 / "#" *non-lf %x0A
    rule comment() -> Comment
        = quiet!{"/" body:$(non_slash()*) "/" { Comment::Slashed } / "#" body:$(non_lf()*) "\x0A" { Comment::Hashed }} / expected!("comment")

// ; optional space
// S               = *blank *(comment *blank)
    // This rule is expressed twice because it is very common to need `s0:S()`, but for comment
    // reshaping we occasionally need the internals
    rule S() -> S<'input>
        = data:S_details() { S(Cow::Borrowed(data.data)) }
    pub(crate) rule S_details() -> SDetails<'input>
        = sliced:with_slice(<blank()* comments:(comment:comment() blank()* { comment })* { comments.last().cloned() }>) { SDetails { data: sliced.1, last_comment_style: sliced.0 } }
// ; mandatory space
// MS              = (blank/comment) S
    rule MS() -> MS<'input>
        = data:$( (blank() / comment() ) S()) { MS(Cow::Borrowed(data)) }
// ; mandatory comma and/or space
// MSC             = ("," S) / (MS ["," S])
    rule MSC() -> MSC<'input>
        = data:$( ("," S()) / (MS() ("," S())?) ) { MSC(Cow::Borrowed(data)) }

// ; optional comma and/or space
// SOC             = S ["," S]
    rule SOC() -> SOC<'input>
        = data:$( SOC_details() ) { SOC(Cow::Borrowed(data)) }
    pub(crate) rule SOC_details() -> (SDetails<'input>, Option<SDetails<'input>>)
        = before:S_details() after:("," after:S_details() { after })? { (before, after) }

// ; check semantically that strings are either all text or all bytes
// ; note that there must be at least one string to distinguish
// streamstring    = "(_" MS string *(MSC string) SOC ")"
    rule streamstring() -> InnerItem<'input>
        = "(_" ms:MS() first:string() tail:(msc:MSC() inner:string() { (msc, inner) })* soc:SOC() ")" {
            InnerItem::StreamString(ms, NonemptyMscVec::new_parsing(first, tail, soc))
        }

// spec            = ["_" *wordchar]
    rule spec() -> Option<Spec>
        = quiet!{("_" spec:$(wordchar()*) {? spec.parse() })? } / expected!(r#"a valid encoding indicator ("_", "_i", "_0", "_1", "_2" or "_3")"#)
// specms          = ["_" *wordchar MS]
    rule specms() -> Option<(Spec, MS<'input>)>
        = quiet!{("_" spec:$(wordchar()*) ms:MS() {? spec.parse().map(|spec| (spec, ms)) })? } / expected!(r#"a valid encoding indicator ("_", "_i", "_0", "_1", "_2" or "_3")"#)

// double-quoted   = unescaped
//                 / SQUOTE
//                 / "\" DQUOTE
//                 / "\" escapable
    rule double_quoted() -> Option<char>
        = unescaped() /
            SQUOTE() { Some('\'') } /
            "\\" DQUOTE() { Some('"') } /
            "\\" e:escapable() { Some(e) }

// single-quoted   = unescaped
//                 / DQUOTE
//                 / "\" SQUOTE
//                 / "\" escapable
    rule single_quoted() -> Option<char>
        = unescaped() / DQUOTE() { Some('"') } / "\\" SQUOTE() { Some('\'') } / "\\" e:escapable() { Some(e) }

// escapable       = %s"b" ; BS backspace U+0008
//                 / %s"f" ; FF form feed U+000C
//                 / %s"n" ; LF line feed U+000A
//                 / %s"r" ; CR carriage return U+000D
//                 / %s"t" ; HT horizontal tab U+0009
//                 / "/"   ; / slash (solidus) U+002F (JSON!)
//                 / "\"   ; \ backslash (reverse solidus) U+005C
//                 / (%s"u" hexchar) ;  uXXXX      U+XXXX
    rule escapable() -> char
        = "b" { '\x08' }
            / "f" { '\x0c' }
            / "n" { '\n' }
            / "r" { '\r' }
            / "t" { '\t' }
            / "/" { '/' }
            / "\\" { '\\' }
            / h:("u" h:hexchar() { h }) { h }

// hexchar         = "{" (1*"0" [ hexscalar ] / hexscalar) "}"
//                 / non-surrogate
//                 / (high-surrogate "\" %s"u" low-surrogate)
    rule hexchar() -> char
        =
            "{" hex:$("0"+ hexscalar()? / hexscalar()) "}"
            {
                char::try_from(
                    u32::from_str_radix(hex, 16)
                        .expect("Syntax ensures this works")
                    )
                    .expect("Syntax rules out surrogate sequences and numbers beyond Unicode specification")
            }
            / hex:$(non_surrogate())
            {
                char::try_from(
                    u32::from(
                        u16::from_str_radix(hex, 16)
                            .expect("Syntax ensures this works")
                        )
                    )
                    .expect("Syntax rules out surrogate sequences and numbers beyond Unicode specification")
            }
            / hl:(h:$(high_surrogate()) "\\" "u" l:$(low_surrogate()) { format!("{h}{l}") /* conveniently, syntax ensures it's always 4 nibbles */ } )
            {
                encoding_rs::UTF_16BE.decode(
                    &u32::from_str_radix(&hl, 16)
                        .expect("Syntax ensures this works")
                        .to_be_bytes()
                        // now it is UTF-16
                    )
                    .0
                    .chars()
                    .next()
                    .expect("Syntax ensures this produces exactly one valid character")
            }
// non-surrogate   = ((DIGIT / "A"/"B"/"C" / "E"/"F") 3HEXDIG)
//                 / ("D" ODIGIT 2HEXDIG )
    rule non_surrogate()
        = ((DIGIT() / "A"/"B"/"C" / "E"/"F" / "a"/"b"/"c" / "e"/"f") HEXDIG()*<3,3>)
                / (("D" / "d") ODIGIT() HEXDIG()*<2,2> )
// high-surrogate  = "D" ("8"/"9"/"A"/"B") 2HEXDIG
    rule high_surrogate()
        = ("D" / "d") ("8"/"9"/"A"/"B"/"a"/"b") HEXDIG()*<2,2>
// low-surrogate   = "D" ("C"/"D"/"E"/"F") 2HEXDIG
    rule low_surrogate()
        = ("D" / "d") ("C"/"D"/"E"/"F" / "c"/"d"/"e"/"f") HEXDIG()*<2,2>
// hexscalar       = "10" 4HEXDIG / HEXDIG1 4HEXDIG
//                 / non-surrogate / 1*3HEXDIG
    rule hexscalar()
        = "10" HEXDIG()*<4,4> / HEXDIG1() HEXDIG()*<4,4> / non_surrogate() / HEXDIG()*<1,3>

// ; Note that no other C0 characters are allowed, including %x09 HT
// unescaped       = %x0A ; new line
//                 / %x0D ; carriage return -- ignored on input
//                 / %x20-21
//                      ; omit 0x22 "
//                 / %x23-26
//                      ; omit 0x27 '
//                 / %x28-5B
//                      ; omit 0x5C \
//                 / %x5D-D7FF ; skip surrogate code points
//                 / %xE000-10FFFF
    // Returning an option to express that the carriage return is ignored
    rule unescaped() -> Option<char> = "\r" { None } / good:[ '\x0a' | '\x0D' | '\x20'..='\x21' | '\x23'..='\x26' | '\x28'..='\x5b' | '\x5d'..='\u{d7ff}' | '\u{e000}'..='\u{10ffff}' ] { Some(good) }

// DQUOTE          = %x22    ; " double quote
    rule DQUOTE() = "\""
// SQUOTE          = "'"     ; ' single quote
    rule SQUOTE() = "'"

// DIGIT           = %x30-39 ; 0-9
// DIGIT1          = %x31-39 ; 1-9
// ODIGIT          = %x30-37 ; 0-7
// BDIGIT          = %x30-31 ; 0-1
// HEXDIG          = DIGIT / "A" / "B" / "C" / "D" / "E" / "F"
// HEXDIG1         = DIGIT1 / "A" / "B" / "C" / "D" / "E" / "F"
    rule DIGIT() = quiet!{['0'..='9']} / expected!("digits")
    rule DIGIT1() = quiet!{['1'..='9']} / expected!("digits excluding 0")
    rule ODIGIT() = ['0'..='7']
    rule BDIGIT() = ['0'..='1']
    rule HEXDIG() -> u8 = n:$(DIGIT() / ['A'..='F' | 'a'..='f']) { u8::from_str_radix(n, 16).expect("Syntax ensures this is OK") }
    rule HEXDIG1() = DIGIT1() / ['A'..='F' | 'a'..='f']

// ; Note: double-quoted strings as in "A" are case-insensitive in ABNF
// lcalpha         = %x61-7A ; a-z
// lcalnum         = lcalpha / DIGIT
// ucalpha         = %x41-5A ; A-Z
// ucalnum         = ucalpha / DIGIT
// wordchar        = "_" / lcalnum / ucalpha ; [_a-z0-9A-Z]
    rule lcalpha() = ['a'..='z']
    rule lcalnum() = ['a'..='z'] / DIGIT()
    rule ucalpha() = ['A'..='Z']
    rule ucalnum() = ['A'..='Z'] / DIGIT()
    rule wordchar() = "_" / lcalnum() / ucalpha()

// Not starting a new grammar for these: their names are unique enough, and they reuse many of the
// other definitions

// app-string-h    = S *(HEXDIG S HEXDIG S / ellipsis S)
//                   ["#" *non-lf]
    pub rule app_string_h() -> Vec<u8> = S() byte:(high:HEXDIG() S() low:HEXDIG() S() { (high << 4) | low } / ellipsis() S() {? Err("Hex string was abbreviated") })*
        ("#" non_lf()*)?
        { byte }

    /// Return both the value and slice matched by the rule.
    ///
    /// This is the canonical workaround to get both a slice and a value, as discussed in
    /// <https://github.com/kevinmehall/rust-peg/issues/377#issuecomment-2158664327>
    rule with_slice<T>(r: rule<T>) -> (T, &'input str)
        = value:&r() input:$(r()) { (value, input) }
}}