asun 1.2.0

ASUN (Array-Schema Unified Notation) - A high-performance, token-efficient serde data format with schema-data separation
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
//! ASUN text decoding.
//!
//! The entry point is the free function [`decode`]; most users only need that
//! plus `#[derive(AsunDecode)]`. Decoding supports zero-copy borrowing — any
//! `&'de str` field in the target type borrows directly from the input.
//!
//! [`Decoder`] and its `struct_field_*` / `decode_*` / `begin_*` methods are the
//! low-level machinery the derive macro drives. They are `pub` so
//! derive-generated code in downstream crates can reach them via
//! `::asun::decode::...`; you rarely need to call them by hand.
//!
//! [`StructDecodeMode`] captures how a struct row is matched against its schema
//! (positional vs. by-name); it is exposed for the generated code and for
//! diagnostics.

use crate::error::{Error, Result};
use crate::simd;
use crate::traits::AsunDecode;
use std::cell::RefCell;
use std::collections::HashMap;
use std::hash::{BuildHasherDefault, Hasher};
use std::sync::Arc;

type CachedSchemaNames = Arc<[Box<str>]>;

/// Maximum structural nesting the decoder will follow before bailing out.
///
/// Schema annotations (`@[[[…]]]`), nested schemas (`@{…}`) and nested
/// sequences all recurse, so without a cap a small hand-crafted payload can
/// exhaust the stack and abort the process — a crash that `catch_unwind`
/// cannot contain.
pub const MAX_DEPTH: u32 = 128;

/// Upper bound on the per-thread schema cache. Untrusted input can contain an
/// unbounded number of distinct schemas; without a cap the cache is an
/// unbounded memory leak.
const SCHEMA_CACHE_CAP: usize = 512;

/// FxHash — the rustc/Firefox multiply-xor-rotate hash. Schema keys are short
/// byte strings compared millions of times per second; SipHash (the std
/// default) shows up in profiles well above the cost of the lookup itself.
#[derive(Default)]
struct FxHasher {
    hash: u64,
}

const FX_SEED: u64 = 0x51_7c_c1_b7_27_22_0a_95;

impl FxHasher {
    #[inline(always)]
    fn add(&mut self, word: u64) {
        self.hash = (self.hash.rotate_left(5) ^ word).wrapping_mul(FX_SEED);
    }
}

impl Hasher for FxHasher {
    #[inline]
    fn write(&mut self, bytes: &[u8]) {
        let mut b = bytes;
        while b.len() >= 8 {
            self.add(u64::from_le_bytes(b[..8].try_into().unwrap()));
            b = &b[8..];
        }
        if b.len() >= 4 {
            self.add(u32::from_le_bytes(b[..4].try_into().unwrap()) as u64);
            b = &b[4..];
        }
        for &x in b {
            self.add(x as u64);
        }
        self.add(bytes.len() as u64);
    }

    #[inline]
    fn write_usize(&mut self, n: usize) {
        self.add(n as u64);
    }

    #[inline]
    fn finish(&self) -> u64 {
        self.hash
    }
}

type FxBuild = BuildHasherDefault<FxHasher>;

thread_local! {
    /// Per-thread schema cache.
    ///
    /// Thread-local rather than a global `Mutex<HashMap>` so concurrent decodes
    /// never contend on a process-wide lock (and so a panic while holding it
    /// cannot poison every future decode), and bounded so hostile input cannot
    /// grow it without limit. Evicting is safe because every `Decoder` that
    /// takes an entry also stores a strong reference in its own arena — see
    /// [`Decoder::intern_schema`].
    static SCHEMA_CACHE: RefCell<HashMap<Box<[u8]>, CachedSchemaNames, FxBuild>> =
        RefCell::new(HashMap::default());
}

#[derive(Hash, PartialEq, Eq, Clone, Copy)]
struct StructModeCacheKey {
    source_ptr: usize,
    source_len: usize,
    target_ptr: usize,
    target_len: usize,
}

/// The decode plan a derived struct impl must follow, chosen by
/// [`Decoder::begin_struct_decode`].
///
/// - `Exact`: the source tuple's fields line up 1:1 (same order, same names)
///   with the target struct. The derive reads fields positionally.
/// - `ByName`: the source schema differs (reordered / missing / extra fields).
///   The derive iterates source keys, matching each to a target field by name,
///   and fills any unmatched target field with its type default.
#[derive(Clone, Copy, PartialEq, Eq)]
pub enum StructDecodeMode {
    Exact,
    ByName,
}

/// The ASUN text decode source that derive-generated [`AsunDecode`] impls pull
/// from. Prefer the [`decode`] free function; this type is exposed for the
/// generated code. The `'de` lifetime is the borrow of the input buffer, which
/// enables zero-copy `&'de str` fields.
///
/// [`AsunDecode`]: crate::AsunDecode
pub struct Decoder<'de> {
    input: &'de [u8],
    pos: usize,
    /// Schema field names for current object context (positional mapping)
    schema_fields: Option<SchemaFields<'de>>,
    /// Current field index within a tuple
    field_index: usize,
    /// True when schema_fields holds the shared vec-header schema,
    /// meaning the next struct should use those field names directly
    /// (source schema) rather than replacing with target struct fields.
    vec_schema_active: bool,
    /// Structural nesting depth, checked against [`MAX_DEPTH`].
    depth: u32,
    /// Tiny MRU cache of resolved field alignments. For deep nested structs
    /// (e.g. `Company > Division > Team > Project > Task`) several struct
    /// types interleave during a single decode; a 1-slot cache thrashes and
    /// every miss falls back to the `HashMap`. `MRU_SLOTS` is sized to
    /// comfortably cover realistic nesting depths.
    ///
    /// Entries are `Copy`, so an MRU hit costs a few integer compares — no
    /// refcount traffic, which used to dominate this path.
    last_struct_mode: [Option<CachedStructMode>; MRU_SLOTS],
    /// Index of the most recently filled MRU slot — checked first.
    last_struct_mode_head: usize,
    /// Per-decode cache for repeated nested struct/source-schema alignments.
    ///
    /// Deliberately *not* process-global: the key contains raw pointers into
    /// schema and target-field slices, which are only guaranteed stable for the
    /// lifetime of one decode (the arenas below pin them). A global cache would
    /// hand a recycled address the previous occupant's plan.
    struct_mode_cache_local: HashMap<StructModeCacheKey, StructPlan, FxBuild>,
    /// Strong references to every schema this decode touched. Keeps the names
    /// alive for the whole decode so [`SchemaFields`] can be a plain `Copy`
    /// borrow instead of a refcounted handle.
    schema_arena: Vec<CachedSchemaNames>,
    /// Backing store for `StructPlan::ByName` missing-field lists, referenced
    /// by index so plans stay `Copy`.
    missing_arena: Vec<Box<[&'static str]>>,
    /// When > 0, every scalar `decode_*` returns a type default instead of
    /// reading the input. This is the direct analog of the previous
    /// `DefaultValueDeserializer`: it lets a derived struct impl produce a
    /// default value for a missing field by simply calling `T::decode`.
    default_depth: u32,
    /// True while decoding an enum whose value was wrapped in `(...)`; tells
    /// `end_enum` to consume the trailing `)`.
    enum_opened_paren: bool,

    // --- Per-struct decode state (used by the begin_struct_decode seam) ---
    /// Stack of in-progress struct frames. Each `begin_struct_decode` pushes
    /// one; `end_struct_decode` pops it. Nesting depth mirrors data nesting.
    struct_frames: Vec<StructFrame<'de>>,
}

/// State for one struct currently being decoded via the derive seam.
struct StructFrame<'de> {
    /// The schema in effect for the parent context, saved so we can restore it
    /// on `end_struct_decode`.
    parent_schema: Option<SchemaFields<'de>>,
    parent_field_index: usize,
    /// True if the parent schema must be restored on end (i.e. we did not
    /// consume a vec-header schema in place).
    restore_parent: bool,
    /// True if this frame opened a `(` that `end_struct_decode` must close
    /// (after skipping any trailing source fields).
    close_paren: bool,

    // ByName-mode cursors.
    /// Number of source fields already consumed via `next_struct_key`.
    byname_source_index: usize,
    /// Whether we are still in the "read source fields" phase (vs. the
    /// "emit missing defaults" phase).
    byname_in_defaults: bool,
    byname_default_index: usize,
    /// Index into [`Decoder::missing_arena`], or `NO_MISSING` for Exact mode.
    byname_missing: u32,
}

/// Sentinel for "this frame has no missing-field list".
const NO_MISSING: u32 = u32::MAX;

const MRU_SLOTS: usize = 8;

pub fn decode<'a, T: AsunDecode<'a>>(s: &'a str) -> Result<T> {
    let mut de = Decoder::new(s.as_bytes());
    de.skip_whitespace_and_comments();
    let value = T::decode(&mut de)?;
    de.skip_whitespace_and_comments();
    if de.pos < de.input.len() {
        if de.input[de.pos..].iter().all(|&b| b.is_ascii_whitespace()) {
            Ok(value)
        } else {
            Err(Error::TrailingCharacters)
        }
    } else {
        Ok(value)
    }
}

impl<'de> Decoder<'de> {
    fn new(input: &'de [u8]) -> Self {
        Decoder {
            input,
            pos: 0,
            schema_fields: None,
            field_index: 0,
            vec_schema_active: false,
            depth: 0,
            last_struct_mode: [const { None }; MRU_SLOTS],
            last_struct_mode_head: 0,
            struct_mode_cache_local: HashMap::default(),
            schema_arena: Vec::new(),
            missing_arena: Vec::new(),
            default_depth: 0,
            enum_opened_paren: false,
            struct_frames: Vec::new(),
        }
    }

    #[inline(always)]
    fn enter(&mut self) -> Result<()> {
        self.depth += 1;
        if self.depth > MAX_DEPTH {
            return Err(Error::DepthLimitExceeded);
        }
        Ok(())
    }

    #[inline(always)]
    fn leave(&mut self) {
        self.depth -= 1;
    }

    /// Pin `names` for the rest of this decode and hand back a `'de` borrow.
    ///
    /// The arena holds a strong reference until the `Decoder` is dropped, and
    /// the `[Box<str>]` behind an `Arc` never moves, so the borrow outlives
    /// every use the decoder makes of it.
    #[inline]
    fn intern_schema(&mut self, names: CachedSchemaNames) -> &'de [Box<str>] {
        let slice: *const [Box<str>] = Arc::as_ptr(&names);
        self.schema_arena.push(names);
        unsafe { &*slice }
    }

    #[inline(always)]
    fn is_layout_byte(b: u8) -> bool {
        matches!(b, b' ' | b'\t' | b'\n' | b'\r')
    }

    #[inline(always)]
    fn is_value_delim(b: u8) -> bool {
        matches!(b, b',' | b')' | b']' | b':')
    }

    #[inline(always)]
    fn is_token_end_at(&self, pos: usize) -> bool {
        pos >= self.input.len()
            || Self::is_value_delim(self.input[pos])
            || Self::is_layout_byte(self.input[pos])
    }

    #[inline(always)]
    fn parse_bool_literal(&mut self) -> Option<bool> {
        if self.pos + 4 <= self.input.len()
            && &self.input[self.pos..self.pos + 4] == b"true"
            && self.is_token_end_at(self.pos + 4)
        {
            self.pos += 4;
            return Some(true);
        }
        if self.pos + 5 <= self.input.len()
            && &self.input[self.pos..self.pos + 5] == b"false"
            && self.is_token_end_at(self.pos + 5)
        {
            self.pos += 5;
            return Some(false);
        }
        None
    }

    /// Find the `}` closing the schema opened at `open_pos`.
    ///
    /// Must skip over quoted field names and block comments: a `}` inside
    /// either is not structural. Getting this wrong truncates the cache key, so
    /// two different schemas can collide — and the same input can decode
    /// differently on the second call once the bad key is cached.
    #[inline]
    fn find_schema_end(&self, open_pos: usize) -> Result<usize> {
        let input = self.input;
        let len = input.len();
        let mut brace_depth = 1u32;
        let mut pos = open_pos + 1;
        while pos < len {
            match input[pos] {
                b'"' => {
                    pos += 1;
                    loop {
                        if pos >= len {
                            return Err(Error::UnclosedString);
                        }
                        match input[pos] {
                            b'\\' => pos += 2,
                            b'"' => {
                                pos += 1;
                                break;
                            }
                            _ => pos += 1,
                        }
                    }
                    continue;
                }
                b'/' if pos + 1 < len && input[pos + 1] == b'*' => {
                    pos += 2;
                    loop {
                        if pos + 1 >= len {
                            return Err(Error::UnclosedComment);
                        }
                        if input[pos] == b'*' && input[pos + 1] == b'/' {
                            pos += 2;
                            break;
                        }
                        pos += 1;
                    }
                    continue;
                }
                b'{' => brace_depth += 1,
                b'}' => {
                    brace_depth -= 1;
                    if brace_depth == 0 {
                        return Ok(pos);
                    }
                }
                _ => {}
            }
            pos += 1;
        }
        Err(Error::Eof)
    }

    #[inline(always)]
    fn peek_byte(&self) -> Result<u8> {
        if self.pos < self.input.len() {
            Ok(self.input[self.pos])
        } else {
            Err(Error::Eof)
        }
    }

    #[inline(always)]
    fn next_byte(&mut self) -> Result<u8> {
        if self.pos < self.input.len() {
            let b = self.input[self.pos];
            self.pos += 1;
            Ok(b)
        } else {
            Err(Error::Eof)
        }
    }

    /// Inline scalar whitespace skipping — fastest for ASUN's compact format
    /// where values are separated by commas with no whitespace.
    /// SIMD overhead (splat/compare/movemask) is too costly when the
    /// common case is 0 whitespace bytes.
    #[inline(always)]
    fn skip_whitespace(&mut self) {
        while self.pos < self.input.len() {
            match self.input[self.pos] {
                b' ' | b'\t' | b'\n' | b'\r' => self.pos += 1,
                _ => break,
            }
        }
    }

    #[inline]
    fn skip_whitespace_and_comments(&mut self) {
        loop {
            self.skip_whitespace();
            if self.pos + 1 < self.input.len()
                && self.input[self.pos] == b'/'
                && self.input[self.pos + 1] == b'*'
            {
                self.pos += 2;
                while self.pos + 1 < self.input.len() {
                    if self.input[self.pos] == b'*' && self.input[self.pos + 1] == b'/' {
                        self.pos += 2;
                        break;
                    }
                    self.pos += 1;
                }
            } else {
                break;
            }
        }
    }

    #[inline(always)]
    fn skip_layout(&mut self) {
        self.skip_whitespace();
        if self.pos + 1 < self.input.len()
            && self.input[self.pos] == b'/'
            && self.input[self.pos + 1] == b'*'
        {
            self.skip_whitespace_and_comments();
        }
    }

    fn parse_schema(&mut self) -> Result<SchemaFields<'de>> {
        self.enter()?;
        let r = self.parse_schema_inner();
        self.leave();
        r
    }

    fn parse_schema_inner(&mut self) -> Result<SchemaFields<'de>> {
        let open_pos = self.pos;
        if self.next_byte()? != b'{' {
            return Err(Error::ExpectedOpenBrace);
        }
        let schema_end = self.find_schema_end(open_pos)?;
        // Copy the slice reference out of `self` so the key does not keep an
        // immutable borrow of the decoder alive across the parsing below.
        let schema_key: &'de [u8] = &self.input[open_pos..=schema_end];

        if let Some(names) = SCHEMA_CACHE.with(|c| c.borrow().get(schema_key).cloned()) {
            self.pos = schema_end + 1;
            return Ok(SchemaFields::Cached(self.intern_schema(names)));
        }

        let mut names = Vec::new();
        loop {
            self.skip_layout();
            if self.peek_byte()? == b'}' {
                self.pos += 1;
                break;
            }
            if !names.is_empty() {
                if self.next_byte()? != b',' {
                    return Err(Error::ExpectedComma);
                }
                self.skip_layout();
                if self.peek_byte()? == b'}' {
                    self.pos += 1;
                    break;
                }
            }
            if self.peek_byte()? == b'"' {
                let cow = self.parse_quoted_string_cow()?;
                let name = match cow {
                    CowStr::Borrowed(s) => s.to_owned().into_boxed_str(),
                    CowStr::Owned(s) => s.into_boxed_str(),
                };
                names.push(name);
            } else {
                let start = self.pos;
                while self.pos < self.input.len() {
                    match self.input[self.pos] {
                        // `/` terminates too: it cannot occur in a bare field
                        // name, and starts a comment (`ows` is legal here).
                        b',' | b'}' | b'@' | b':' | b'/' | b' ' | b'\t' | b'\n' | b'\r' => break,
                        _ => self.pos += 1,
                    }
                }
                let name = unsafe { core::str::from_utf8_unchecked(&self.input[start..self.pos]) };
                names.push(name.to_owned().into_boxed_str());
            }
            self.skip_layout();

            // Validate and skip optional @type hint or nested structural scaffold.
            if self.pos < self.input.len() && self.input[self.pos] == b'@' {
                self.pos += 1;
                self.skip_layout();
                self.parse_schema_annotation()?;
            }
        }

        let names: CachedSchemaNames = names.into_boxed_slice().into();
        SCHEMA_CACHE.with(|c| {
            let mut c = c.borrow_mut();
            // Cheapest bounded policy that keeps the common case (a handful of
            // schemas reused forever) allocation-free: drop everything once the
            // cap is hit rather than tracking recency.
            if c.len() >= SCHEMA_CACHE_CAP {
                c.clear();
            }
            c.insert(schema_key.into(), names.clone());
        });
        Ok(SchemaFields::Cached(self.intern_schema(names)))
    }

    fn parse_schema_annotation(&mut self) -> Result<()> {
        self.enter()?;
        let r = self.parse_schema_annotation_inner();
        self.leave();
        r
    }

    fn parse_schema_annotation_inner(&mut self) -> Result<()> {
        if self.pos >= self.input.len() {
            return Err(Error::msg("expected schema type after '@'"));
        }
        match self.input[self.pos] {
            b'{' => {
                let _ = self.parse_schema()?;
                Ok(())
            }
            b'[' => {
                self.pos += 1;
                self.skip_layout();
                if self.pos < self.input.len() && self.input[self.pos] == b']' {
                    self.pos += 1;
                    return Ok(());
                }
                // The element type may itself be a struct `{...}`, a nested
                // array `[...]` (array-of-array, e.g. `@[[]]`), or a scalar.
                match self.input.get(self.pos) {
                    Some(b'{') => {
                        let _ = self.parse_schema()?;
                    }
                    Some(b'[') => {
                        self.parse_schema_annotation()?;
                    }
                    _ => {
                        self.parse_allowed_schema_scalar_type()?;
                    }
                }
                self.skip_layout();
                if self.pos >= self.input.len() || self.input[self.pos] != b']' {
                    return Err(Error::msg("expected ']' in array type annotation"));
                }
                self.pos += 1;
                Ok(())
            }
            _ => self.parse_allowed_schema_scalar_type(),
        }
    }

    fn parse_allowed_schema_scalar_type(&mut self) -> Result<()> {
        let start = self.pos;
        while self.pos < self.input.len() {
            match self.input[self.pos] {
                b',' | b'}' | b']' | b'/' | b' ' | b'\t' | b'\n' | b'\r' => break,
                _ => self.pos += 1,
            }
        }
        if start == self.pos {
            return Err(Error::msg("expected schema type after '@'"));
        }
        let mut token = unsafe { core::str::from_utf8_unchecked(&self.input[start..self.pos]) };
        if let Some(stripped) = token.strip_suffix('?') {
            token = stripped;
        }
        match token {
            "int" | "str" | "float" | "bool" => Ok(()),
            _ => Err(Error::msg(format!(
                "unsupported schema type '{token}'; use int, str, float, or bool"
            ))),
        }
    }

    #[inline]
    fn skip_balanced(&mut self, open: u8, close: u8) -> Result<()> {
        let mut depth = 0u32;
        loop {
            if self.pos >= self.input.len() {
                return Err(Error::Eof);
            }
            let b = self.input[self.pos];
            self.pos += 1;
            if b == open {
                depth += 1;
            } else if b == close {
                if depth == 0 {
                    return Err(Error::msg("unbalanced brackets"));
                }
                depth -= 1;
                if depth == 0 {
                    return Ok(());
                }
            }
        }
    }

    /// Skip a single ASUN value (string, number, bool, tuple, array, etc.)
    fn skip_value(&mut self) -> Result<()> {
        self.skip_layout();
        if self.pos >= self.input.len() {
            return Ok(());
        }
        match self.input[self.pos] {
            b'(' => self.skip_balanced(b'(', b')'),
            b'[' => self.skip_balanced(b'[', b']'),
            b'"' => self.skip_quoted_string(),
            _ => {
                while self.pos < self.input.len() {
                    match self.input[self.pos] {
                        b',' | b')' | b']' | b'}' | b':' => break,
                        _ => self.pos += 1,
                    }
                }
                Ok(())
            }
        }
    }

    /// Skip remaining comma-separated values until ')'.
    /// Used when the source tuple has more fields than the target struct.
    fn skip_remaining_tuple_values(&mut self) -> Result<()> {
        self.skip_layout();
        while self.pos < self.input.len() && self.input[self.pos] != b')' {
            if self.input[self.pos] == b',' {
                self.pos += 1;
                self.skip_layout();
                if self.pos < self.input.len() && self.input[self.pos] == b')' {
                    break;
                }
            }
            if self.pos < self.input.len() && self.input[self.pos] != b')' {
                self.skip_value()?;
                self.skip_layout();
            }
        }
        Ok(())
    }

    /// Parse a plain (unquoted) string value, stopping at delimiters.
    /// Returns zerocopy borrowed str.
    #[inline]
    fn parse_plain_value_meta(&mut self) -> Result<(&'de str, bool)> {
        let start = self.pos;
        let mut has_escape = false;
        while self.pos < self.input.len() {
            let hit = simd::simd_find_plain_delimiter(self.input, self.pos);
            self.pos = hit;
            if self.pos >= self.input.len() {
                break;
            }
            if self.input[self.pos] == b'\\' {
                has_escape = true;
                // A trailing backslash with no following byte is malformed; a
                // bare `self.pos += 2` would push past the end and panic on the
                // trim/slice below. Reject it instead.
                if self.pos + 2 > self.input.len() {
                    return Err(Error::InvalidEscape('\\'));
                }
                self.pos += 2;
            } else {
                break;
            }
        }
        let mut end = self.pos;
        while end > start && Self::is_layout_byte(self.input[end - 1]) {
            end -= 1;
        }
        // When escapes are present the slice may split a multi-byte UTF-8
        // sequence (pos advanced by 2 past a `\`), which would make the
        // `from_utf8_unchecked` reference invalid. Validate in that case; the
        // no-escape fast path is guaranteed valid because the input is `&str`.
        let bytes = &self.input[start..end];
        let raw = if has_escape {
            core::str::from_utf8(bytes).map_err(|_| Error::InvalidEscape('\\'))?
        } else {
            unsafe { core::str::from_utf8_unchecked(bytes) }
        };
        Ok((raw, has_escape))
    }

    /// Parse a quoted string. Zerocopy when no escapes; allocates only when escapes present.
    /// Uses SIMD to scan for `"` or `\` in 16-byte chunks.
    #[inline]
    fn parse_quoted_string_cow(&mut self) -> Result<CowStr<'de>> {
        // Skip opening quote
        self.pos += 1;
        let start = self.pos;

        // SIMD fast scan: look for the closing quote or escape
        let hit = simd::simd_find_quote_or_backslash(self.input, self.pos);
        if hit < self.input.len() && self.input[hit] == b'"' {
            // No escapes found — zerocopy path
            let s = unsafe { core::str::from_utf8_unchecked(&self.input[start..hit]) };
            self.pos = hit + 1;
            return Ok(CowStr::Borrowed(s));
        }

        // Slow path: build owned string with escapes
        let scan = hit;
        let mut result = String::with_capacity(scan - start + 16);
        if scan > start {
            let prefix = unsafe { core::str::from_utf8_unchecked(&self.input[start..scan]) };
            result.push_str(prefix);
        }
        self.pos = scan;

        loop {
            if self.pos >= self.input.len() {
                return Err(Error::UnclosedString);
            }
            let b = self.input[self.pos];
            if b == b'"' {
                self.pos += 1;
                return Ok(CowStr::Owned(result));
            }
            if b == b'\\' {
                self.pos += 1;
                if self.pos >= self.input.len() {
                    return Err(Error::UnclosedString);
                }
                let esc = self.input[self.pos];
                self.pos += 1;
                match esc {
                    b'"' => result.push('"'),
                    b'\\' => result.push('\\'),
                    b'n' => result.push('\n'),
                    b't' => result.push('\t'),
                    b'r' => result.push('\r'),
                    b'b' => result.push('\u{0008}'),
                    b'f' => result.push('\u{000C}'),
                    b',' => result.push(','),
                    b'(' => result.push('('),
                    b')' => result.push(')'),
                    b'[' => result.push('['),
                    b']' => result.push(']'),
                    b'{' => result.push('{'),
                    b'}' => result.push('}'),
                    b':' => result.push(':'),
                    b'@' => result.push('@'),
                    b'u' => {
                        let ch = read_unicode_escape(self.input, &mut self.pos)?;
                        result.push(ch);
                    }
                    _ => return Err(Error::InvalidEscape(esc as char)),
                }
            } else {
                // After an escape sequence, SIMD scan for next quote/backslash
                let next_hit = simd::simd_find_quote_or_backslash(self.input, self.pos);
                // Bulk copy the safe run
                if next_hit > self.pos {
                    let chunk =
                        unsafe { core::str::from_utf8_unchecked(&self.input[self.pos..next_hit]) };
                    result.push_str(chunk);
                    self.pos = next_hit;
                } else {
                    result.push(b as char);
                    self.pos += 1;
                }
            }
        }
    }

    #[inline]
    fn skip_quoted_string(&mut self) -> Result<()> {
        self.pos += 1;
        loop {
            if self.pos >= self.input.len() {
                return Err(Error::Eof);
            }
            let hit = simd::simd_find_quote_or_backslash(self.input, self.pos);
            if hit >= self.input.len() {
                return Err(Error::Eof);
            }
            self.pos = hit;
            match self.input[self.pos] {
                b'"' => {
                    self.pos += 1;
                    return Ok(());
                }
                b'\\' => self.pos += 2,
                _ => unreachable!(),
            }
        }
    }

    /// Parse any value as a string.
    #[inline]
    fn parse_any_value_str(&mut self) -> Result<CowStr<'de>> {
        self.skip_layout();
        if self.pos >= self.input.len() {
            return Ok(CowStr::Borrowed(""));
        }
        if self.input[self.pos] == b'"' {
            self.parse_quoted_string_cow()
        } else {
            let (v, has_escape) = self.parse_plain_value_meta()?;
            if has_escape {
                Ok(CowStr::Owned(unescape_plain(v)?))
            } else {
                Ok(CowStr::Borrowed(v))
            }
        }
    }

    /// Parse number directly without intermediate string::parse for integers.
    /// Optimized loop with minimal branching.
    #[inline]
    fn parse_i64(&mut self) -> Result<i64> {
        let negative = self.pos < self.input.len() && self.input[self.pos] == b'-';
        if negative {
            self.pos += 1;
        }
        let mut val: u64 = 0;
        let mut digits = 0u32;
        while self.pos < self.input.len() {
            let d = self.input[self.pos].wrapping_sub(b'0');
            if d > 9 {
                break;
            }
            // Detect overflow instead of silently wrapping; an out-of-range
            // integer is a decode error, not corrupt data.
            val = val
                .checked_mul(10)
                .and_then(|v| v.checked_add(d as u64))
                .ok_or(Error::InvalidNumber)?;
            self.pos += 1;
            digits += 1;
        }
        if digits == 0 {
            return Err(Error::InvalidNumber);
        }
        if negative {
            // Magnitude fits in i64 (>= -2^63) exactly when val <= 2^63.
            if val > (i64::MAX as u64) + 1 {
                return Err(Error::InvalidNumber);
            }
            Ok((val as i64).wrapping_neg())
        } else {
            if val > i64::MAX as u64 {
                return Err(Error::InvalidNumber);
            }
            Ok(val as i64)
        }
    }

    /// Parse u64 directly. Optimized loop with wrapping_sub for digit check.
    #[inline]
    fn parse_u64(&mut self) -> Result<u64> {
        let mut val: u64 = 0;
        let mut digits = 0u32;
        while self.pos < self.input.len() {
            let d = self.input[self.pos].wrapping_sub(b'0');
            if d > 9 {
                break;
            }
            val = val
                .checked_mul(10)
                .and_then(|v| v.checked_add(d as u64))
                .ok_or(Error::InvalidNumber)?;
            self.pos += 1;
            digits += 1;
        }
        if digits == 0 {
            return Err(Error::InvalidNumber);
        }
        Ok(val)
    }

    /// Parse f64 directly using fast-float for speed.
    ///
    /// Enforces ABNF `float = ["-"] 1*DIGIT ( "." 1*DIGIT [exponent] / exponent )`:
    /// the integer part must have ≥1 digit, and if a decimal point is present
    /// the fractional part must also have ≥1 digit. Tokens like `"5."`, `".5"`,
    /// or `"+5"` are rejected so the caller can fall back to plain-string.
    #[inline]
    fn parse_f64_direct(&mut self) -> Result<f64> {
        let start = self.pos;
        if self.pos < self.input.len() && self.input[self.pos] == b'-' {
            self.pos += 1;
        }
        let int_start = self.pos;
        while self.pos < self.input.len() && self.input[self.pos].is_ascii_digit() {
            self.pos += 1;
        }
        let int_digits = self.pos - int_start;
        let mut had_dot_or_exp = false;
        if self.pos < self.input.len() && self.input[self.pos] == b'.' {
            had_dot_or_exp = true;
            self.pos += 1;
            let frac_start = self.pos;
            while self.pos < self.input.len() && self.input[self.pos].is_ascii_digit() {
                self.pos += 1;
            }
            // ABNF requires at least one digit after the decimal point.
            if self.pos == frac_start {
                return Err(Error::InvalidNumber);
            }
        }
        // Handle scientific notation (e.g. 1.5e10)
        if self.pos < self.input.len()
            && (self.input[self.pos] == b'e' || self.input[self.pos] == b'E')
        {
            had_dot_or_exp = true;
            self.pos += 1;
            if self.pos < self.input.len()
                && (self.input[self.pos] == b'+' || self.input[self.pos] == b'-')
            {
                self.pos += 1;
            }
            let exp_start = self.pos;
            while self.pos < self.input.len() && self.input[self.pos].is_ascii_digit() {
                self.pos += 1;
            }
            // ABNF requires at least one digit in the exponent.
            if self.pos == exp_start {
                return Err(Error::InvalidNumber);
            }
        }
        // Must have at least one integer digit, and must actually be a float
        // (contain "." or "e"/"E"); otherwise it's an integer / not a number.
        if int_digits == 0 || !had_dot_or_exp {
            return Err(Error::InvalidNumber);
        }
        let s = &self.input[start..self.pos];
        fast_float2::parse(s).map_err(|_| Error::InvalidNumber)
    }

    #[inline(always)]
    fn at_value_end(&self) -> bool {
        if self.pos >= self.input.len() {
            return true;
        }
        matches!(self.input[self.pos], b',' | b')' | b']')
    }

    #[inline(always)]
    fn struct_plan_uncached(&mut self, target_fields: &'static [&'static str]) -> StructPlan {
        let Some(source_fields) = self.schema_fields else {
            return StructPlan::Exact;
        };
        if source_fields.matches_exact(target_fields) {
            StructPlan::Exact
        } else {
            let missing = source_fields.missing_target_fields(target_fields);
            let idx = self.missing_arena.len() as u32;
            self.missing_arena.push(missing);
            StructPlan::ByName(idx)
        }
    }

    #[inline]
    fn struct_plan(&mut self, target_fields: &'static [&'static str]) -> StructPlan {
        let Some(source_fields) = self.schema_fields else {
            return StructPlan::Exact;
        };
        let source_key = source_fields.cache_key();
        let cache_key = StructModeCacheKey {
            source_ptr: source_key.ptr,
            source_len: source_key.len,
            target_ptr: target_fields.as_ptr() as usize,
            target_len: target_fields.len(),
        };

        // MRU fast path: linear-search a small fixed-size array. Skips the
        // `HashMap::get` when the same handful of struct shapes alternate
        // (typical in deeply nested data, e.g.
        // Company > Division > Team > Project > Task on every row).
        for slot in self.last_struct_mode.iter().flatten() {
            if slot.cache_key == cache_key {
                return slot.plan;
            }
        }

        let plan = match self.struct_mode_cache_local.get(&cache_key) {
            Some(&plan) => plan,
            None => {
                let plan = self.struct_plan_uncached(target_fields);
                self.struct_mode_cache_local.insert(cache_key, plan);
                plan
            }
        };
        self.mru_put(cache_key, plan);
        plan
    }

    #[inline]
    fn mru_put(&mut self, cache_key: StructModeCacheKey, plan: StructPlan) {
        let slot = self.last_struct_mode_head;
        self.last_struct_mode[slot] = Some(CachedStructMode { cache_key, plan });
        self.last_struct_mode_head = (slot + 1) % MRU_SLOTS;
    }

    // =======================================================================
    // Scalar decode primitives (called by the derive + built-in trait impls)
    //
    // Each honours `default_depth`: when in default mode (a missing struct
    // field being materialised), it returns the type default instead of
    // reading the input.
    // =======================================================================

    #[inline]
    pub fn decode_bool(&mut self) -> Result<bool> {
        if self.default_depth > 0 {
            return Ok(false);
        }
        self.skip_layout();
        if let Some(value) = self.parse_bool_literal() {
            return Ok(value);
        }
        Err(Error::InvalidBool)
    }

    #[inline]
    pub fn decode_i8(&mut self) -> Result<i8> {
        if self.default_depth > 0 {
            return Ok(0);
        }
        self.skip_layout();
        i8::try_from(self.parse_i64()?).map_err(|_| Error::IntegerOutOfRange)
    }

    #[inline]
    pub fn decode_i16(&mut self) -> Result<i16> {
        if self.default_depth > 0 {
            return Ok(0);
        }
        self.skip_layout();
        i16::try_from(self.parse_i64()?).map_err(|_| Error::IntegerOutOfRange)
    }

    #[inline]
    pub fn decode_i32(&mut self) -> Result<i32> {
        if self.default_depth > 0 {
            return Ok(0);
        }
        self.skip_layout();
        i32::try_from(self.parse_i64()?).map_err(|_| Error::IntegerOutOfRange)
    }

    #[inline]
    pub fn decode_i64(&mut self) -> Result<i64> {
        if self.default_depth > 0 {
            return Ok(0);
        }
        self.skip_layout();
        self.parse_i64()
    }

    #[inline]
    pub fn decode_u8(&mut self) -> Result<u8> {
        if self.default_depth > 0 {
            return Ok(0);
        }
        self.skip_layout();
        u8::try_from(self.parse_u64()?).map_err(|_| Error::IntegerOutOfRange)
    }

    #[inline]
    pub fn decode_u16(&mut self) -> Result<u16> {
        if self.default_depth > 0 {
            return Ok(0);
        }
        self.skip_layout();
        u16::try_from(self.parse_u64()?).map_err(|_| Error::IntegerOutOfRange)
    }

    #[inline]
    pub fn decode_u32(&mut self) -> Result<u32> {
        if self.default_depth > 0 {
            return Ok(0);
        }
        self.skip_layout();
        u32::try_from(self.parse_u64()?).map_err(|_| Error::IntegerOutOfRange)
    }

    #[inline]
    pub fn decode_u64(&mut self) -> Result<u64> {
        if self.default_depth > 0 {
            return Ok(0);
        }
        self.skip_layout();
        self.parse_u64()
    }

    #[inline]
    pub fn decode_f32(&mut self) -> Result<f32> {
        if self.default_depth > 0 {
            return Ok(0.0);
        }
        self.skip_layout();
        Ok(self.parse_f64_direct()? as f32)
    }

    #[inline]
    pub fn decode_f64(&mut self) -> Result<f64> {
        if self.default_depth > 0 {
            return Ok(0.0);
        }
        self.skip_layout();
        self.parse_f64_direct()
    }

    #[inline]
    pub fn decode_char(&mut self) -> Result<char> {
        if self.default_depth > 0 {
            return Ok('\0');
        }
        self.skip_layout();
        let cow = self.parse_any_value_str()?;
        let s = cow.as_str();
        let mut chars = s.chars();
        chars.next().ok_or(Error::ExpectedValue)
    }

    #[inline]
    pub fn decode_string(&mut self) -> Result<String> {
        if self.default_depth > 0 {
            return Ok(String::new());
        }
        self.skip_layout();
        if self.pos < self.input.len() && self.input[self.pos] == b'"' {
            let cow = self.parse_quoted_string_cow()?;
            Ok(match cow {
                CowStr::Borrowed(s) => s.to_owned(),
                CowStr::Owned(s) => s,
            })
        } else {
            let (v, has_escape) = self.parse_plain_value_meta()?;
            if has_escape {
                unescape_plain(v)
            } else {
                Ok(v.to_owned())
            }
        }
    }

    /// Zero-copy borrowed str decode.
    #[inline]
    pub fn decode_borrowed_str(&mut self) -> Result<&'de str> {
        if self.default_depth > 0 {
            return Ok("");
        }
        self.skip_layout();
        if self.pos < self.input.len() && self.input[self.pos] == b'"' {
            let cow = self.parse_quoted_string_cow()?;
            match cow {
                CowStr::Borrowed(s) => Ok(s),
                // An escaped string cannot be borrowed; the previous serde impl
                // handed serde an owned String which it copied. Here the target
                // is `&'de str`, which fundamentally cannot hold an unescaped
                // owned buffer — reject, matching the borrow contract.
                CowStr::Owned(_) => Err(Error::msg("cannot borrow &str from an escaped string")),
            }
        } else {
            let (v, _has_escape) = self.parse_plain_value_meta()?;
            Ok(v)
        }
    }

    #[inline]
    pub fn decode_option<T: AsunDecode<'de>>(&mut self) -> Result<Option<T>> {
        if self.default_depth > 0 {
            return Ok(None);
        }
        self.skip_layout();
        if self.at_value_end() {
            Ok(None)
        } else {
            Ok(Some(T::decode(self)?))
        }
    }

    #[inline]
    pub fn decode_unit(&mut self) -> Result<()> {
        if self.default_depth > 0 {
            return Ok(());
        }
        self.skip_layout();
        if self.pos + 1 < self.input.len()
            && self.input[self.pos] == b'('
            && self.input[self.pos + 1] == b')'
        {
            self.pos += 2;
            Ok(())
        } else if self.at_value_end() {
            Ok(())
        } else {
            Err(Error::ExpectedValue)
        }
    }

    /// Decode a homogeneous sequence `Vec<T>`.
    ///
    /// Handles both `[v1,v2,...]` (plain array) and `[{schema}]:(row),(row)`
    /// (struct array with a shared schema).
    pub fn decode_vec<T: AsunDecode<'de>>(&mut self) -> Result<Vec<T>> {
        if self.default_depth > 0 {
            return Ok(Vec::new());
        }
        self.enter()?;
        let r = self.decode_vec_inner();
        self.leave();
        r
    }

    fn decode_vec_inner<T: AsunDecode<'de>>(&mut self) -> Result<Vec<T>> {
        self.skip_layout();
        // [{schema}]:(v1,...),(v2,...) — struct array with shared schema
        if self.peek_byte()? == b'['
            && self.pos + 1 < self.input.len()
            && self.input[self.pos + 1] == b'{'
        {
            self.pos += 1; // skip '['
            let fields = self.parse_schema()?;
            self.skip_layout();
            if self.next_byte()? != b']' {
                return Err(Error::ExpectedCloseBracket);
            }
            self.skip_layout();
            if self.next_byte()? != b':' {
                return Err(Error::ExpectedColon);
            }
            self.schema_fields = Some(fields);
            self.vec_schema_active = true;

            let mut out = Vec::new();
            let mut first = true;
            loop {
                self.skip_layout();
                if self.pos >= self.input.len() {
                    break;
                }
                if !first {
                    if self.input[self.pos] == b',' {
                        self.pos += 1;
                        self.skip_layout();
                    } else {
                        break;
                    }
                }
                first = false;
                if self.pos >= self.input.len() || self.input[self.pos] != b'(' {
                    break;
                }
                self.field_index = 0;
                self.vec_schema_active = true;
                out.push(T::decode(self)?);
            }

            self.vec_schema_active = false;
            self.schema_fields = None;
            Ok(out)
        } else {
            if self.next_byte()? != b'[' {
                return Err(Error::ExpectedOpenBracket);
            }
            let mut out = Vec::new();
            let mut first = true;
            loop {
                self.skip_layout();
                if self.pos >= self.input.len() {
                    break;
                }
                if self.input[self.pos] == b']' {
                    break;
                }
                if !first {
                    if self.input[self.pos] == b',' {
                        self.pos += 1;
                        self.skip_layout();
                        if self.pos < self.input.len() && self.input[self.pos] == b']' {
                            break;
                        }
                    } else {
                        break;
                    }
                }
                first = false;
                out.push(T::decode(self)?);
            }
            self.skip_layout();
            if self.pos < self.input.len() && self.input[self.pos] == b']' {
                self.pos += 1;
            }
            Ok(out)
        }
    }

    // -----------------------------------------------------------------------
    // Tuple seam (plain tuples, tuple structs, tuple enum variants)
    // -----------------------------------------------------------------------

    /// Begin decoding a tuple `(`. In default mode this is a no-op.
    #[inline]
    pub fn begin_tuple(&mut self) -> Result<()> {
        if self.default_depth > 0 {
            return Ok(());
        }
        self.skip_layout();
        if self.next_byte()? != b'(' {
            return Err(Error::ExpectedOpenParen);
        }
        self.field_index = 0;
        Ok(())
    }

    /// Decode one tuple element in positional order.
    ///
    /// Mirrors `AsunTupleAccess`: elements are comma separated and terminated
    /// by `)`. A missing element (premature `)`) yields `T`'s default.
    #[inline]
    pub fn tuple_element<T: AsunDecode<'de>>(&mut self) -> Result<T> {
        if self.default_depth > 0 {
            return T::decode(self);
        }
        self.skip_layout();
        let first = self.field_index == 0;
        // End-of-tuple or EOF: emit default.
        if self.pos >= self.input.len() || self.input[self.pos] == b')' {
            self.field_index += 1;
            return self.decode_default::<T>();
        }
        if !first {
            if self.input[self.pos] == b',' {
                self.pos += 1;
                self.skip_layout();
                if self.pos < self.input.len() && self.input[self.pos] == b')' {
                    self.field_index += 1;
                    return self.decode_default::<T>();
                }
            } else {
                // No further comma-separated element — treat remaining as default.
                self.field_index += 1;
                return self.decode_default::<T>();
            }
        }
        self.field_index += 1;
        T::decode(self)
    }

    /// Finish decoding a tuple of `_count` declared elements: skip any extra
    /// source values and consume the closing `)`.
    #[inline]
    pub fn end_tuple(&mut self, _count: usize) -> Result<()> {
        if self.default_depth > 0 {
            return Ok(());
        }
        self.skip_layout();
        if self.pos < self.input.len() && self.input[self.pos] == b')' {
            self.pos += 1;
        }
        Ok(())
    }

    /// Produce a type default for `T` by running its decode logic in default
    /// mode. This is the direct analog of the previous `DefaultValueDeserializer`.
    #[inline]
    fn decode_default<T: AsunDecode<'de>>(&mut self) -> Result<T> {
        self.default_depth += 1;
        let r = T::decode(self);
        self.default_depth -= 1;
        r
    }

    // -----------------------------------------------------------------------
    // Struct seam (used by derived AsunDecode impls)
    // -----------------------------------------------------------------------

    /// Begin decoding a struct with the given target field list.
    ///
    /// Parses/consumes any schema header and the opening `(`, sets up schema
    /// alignment, and returns the [`StructDecodeMode`] the derive should follow.
    /// Must be paired with [`Decoder::end_struct_decode`].
    pub fn begin_struct_decode(
        &mut self,
        target_fields: &'static [&'static str],
    ) -> Result<StructDecodeMode> {
        if self.default_depth > 0 {
            // Missing struct field: every leaf recurses in default mode. We
            // still push a frame so end_struct_decode stays balanced, but it
            // performs no input reads.
            self.struct_frames.push(StructFrame {
                parent_schema: None,
                parent_field_index: self.field_index,
                restore_parent: false,
                close_paren: false,
                byname_source_index: 0,
                byname_in_defaults: false,
                byname_default_index: 0,
                byname_missing: NO_MISSING,
            });
            return Ok(StructDecodeMode::Exact);
        }

        self.enter()?;
        self.skip_layout();

        // Resolve schema + opening paren + parent bookkeeping, mirroring the
        // previous `deserialize_struct` state machine exactly.
        let (parent_schema, restore_parent, close_paren);

        if self.schema_fields.is_some() {
            if self.peek_byte()? == b'(' {
                self.pos += 1;
                self.field_index = 0;
                let ps = self.schema_fields.take();
                let from_vec_header = self.vec_schema_active;
                if from_vec_header {
                    // Vec row: schema_fields holds the source field names from
                    // the vec header — keep them active for this row so we can
                    // match by name. The vec loop owns this schema across all
                    // rows, so we stash a copy in the frame and restore it on
                    // `end_struct_decode`; otherwise nested structs (which
                    // replace `schema_fields`) or the end-clear path would drop
                    // it, and every row after the first would decode with the
                    // wrong schema.
                    self.schema_fields = ps;
                    self.vec_schema_active = false;
                    parent_schema = ps;
                    restore_parent = true;
                } else {
                    self.schema_fields = Some(SchemaFields::Static(target_fields));
                    parent_schema = ps;
                    restore_parent = true;
                }
                close_paren = true;
            } else {
                parent_schema = self.schema_fields.take();
                self.schema_fields = Some(SchemaFields::Static(target_fields));
                self.field_index = 0;
                restore_parent = true;
                close_paren = false;
            }
        } else if self.peek_byte()? == b'{' {
            let parsed_fields = self.parse_schema()?;
            self.skip_layout();
            if self.next_byte()? != b':' {
                return Err(Error::ExpectedColon);
            }
            self.skip_layout();
            self.schema_fields = Some(parsed_fields);
            if self.next_byte()? != b'(' {
                return Err(Error::ExpectedOpenParen);
            }
            self.field_index = 0;
            parent_schema = None;
            restore_parent = false; // schema_fields cleared to None on end
            close_paren = true;
        } else if self.peek_byte()? == b'(' {
            self.pos += 1;
            self.schema_fields = Some(SchemaFields::Static(target_fields));
            self.field_index = 0;
            parent_schema = None;
            restore_parent = false; // schema_fields cleared to None on end
            close_paren = true;
        } else {
            return Err(Error::ExpectedOpenBrace);
        }

        let (decode_mode, byname_missing) = match self.struct_plan(target_fields) {
            StructPlan::Exact => (StructDecodeMode::Exact, NO_MISSING),
            StructPlan::ByName(idx) => (StructDecodeMode::ByName, idx),
        };

        self.struct_frames.push(StructFrame {
            parent_schema,
            parent_field_index: 0,
            restore_parent,
            close_paren,
            byname_source_index: 0,
            byname_in_defaults: false,
            byname_default_index: 0,
            byname_missing,
        });
        Ok(decode_mode)
    }

    /// Exact mode: read the field at positional index `_index`.
    /// Mirrors `AsunStructSeqAccess`.
    #[inline]
    pub fn struct_field_positional<T: AsunDecode<'de>>(&mut self, index: usize) -> Result<T> {
        if self.default_depth > 0 {
            return T::decode(self);
        }
        // Hot path: the derive passes the positional index directly and calls
        // this once per field with `index` = 0,1,2,…, exactly tracking the
        // frame's `exact_index`. We therefore drive comma/paren logic off
        // `index` alone and never touch `self.struct_frames` here — the frame
        // is only read/restored in `end_struct_decode`. `self.field_index` is
        // kept in sync so a nested `T::decode` saves the right parent index.
        self.skip_layout();
        self.field_index = index + 1;

        if self.pos >= self.input.len() {
            // Ran out of input; remaining fields are defaults.
            return self.decode_default::<T>();
        }

        if self.input[self.pos] == b')' {
            return self.decode_default::<T>();
        }

        if index > 0 {
            if self.input[self.pos] == b',' {
                self.pos += 1;
                self.skip_layout();
                if self.pos < self.input.len() && self.input[self.pos] == b')' {
                    return self.decode_default::<T>();
                }
            } else {
                // No further field: emit default without advancing input.
                return self.decode_default::<T>();
            }
        }

        T::decode(self)
    }

    /// ByName mode: return the next source field's name, or `None` when the
    /// source tuple is exhausted. After the source is drained, this emits the
    /// names of missing target fields (whose values decode as defaults).
    /// Mirrors `AsunStructAccessWithDefaults::next_key_seed`.
    pub fn next_struct_key(&mut self) -> Result<Option<&'de str>> {
        let frame_idx = self.struct_frames.len() - 1;
        self.skip_layout();

        loop {
            if self.pos >= self.input.len() {
                // Source exhausted at EOF: fall into defaults phase.
                return self.next_missing_default_key(frame_idx);
            }

            // At ')' — source tuple done; emit missing defaults.
            if self.input[self.pos] == b')' {
                return self.next_missing_default_key(frame_idx);
            }

            let field_count = match &self.schema_fields {
                Some(f) => f.len(),
                None => return Ok(None),
            };

            let source_index = self.struct_frames[frame_idx].byname_source_index;
            if source_index >= field_count {
                return Ok(None);
            }

            if source_index > 0 {
                if self.pos < self.input.len() && self.input[self.pos] == b',' {
                    self.pos += 1;
                    self.skip_layout();
                    if self.pos < self.input.len() && self.input[self.pos] == b')' {
                        // Trailing comma then ')': retry loop → defaults phase.
                        continue;
                    }
                } else {
                    // No comma: end of source tuple.
                    return Ok(None);
                }
            }

            let field_name = self.schema_fields.unwrap().name_at(source_index);
            let frame = &mut self.struct_frames[frame_idx];
            frame.byname_source_index += 1;
            self.field_index = frame.byname_source_index;
            return Ok(Some(field_name));
        }
    }

    #[inline]
    fn next_missing_default_key(&mut self, frame_idx: usize) -> Result<Option<&'de str>> {
        let frame = &mut self.struct_frames[frame_idx];
        frame.byname_in_defaults = true;
        if frame.byname_missing == NO_MISSING {
            return Ok(None);
        }
        let idx = frame.byname_missing as usize;
        let k = frame.byname_default_index;
        let missing = &self.missing_arena[idx];
        if k >= missing.len() {
            return Ok(None);
        }
        // Elements are `&'static str`, so this is a copy, not a lifetime cast.
        let name: &'static str = missing[k];
        self.struct_frames[frame_idx].byname_default_index += 1;
        Ok(Some(name))
    }

    /// ByName mode: decode the value corresponding to the key just returned by
    /// `next_struct_key`. Mirrors `AsunStructAccessWithDefaults::next_value_seed`.
    #[inline]
    pub fn struct_field_value<T: AsunDecode<'de>>(&mut self) -> Result<T> {
        let frame_idx = self.struct_frames.len() - 1;
        if self.struct_frames[frame_idx].byname_in_defaults {
            return self.decode_default::<T>();
        }
        self.skip_layout();
        if self.pos < self.input.len() && self.input[self.pos] == b')' {
            self.decode_default::<T>()
        } else {
            T::decode(self)
        }
    }

    /// ByName mode: skip the value for an unmatched source key.
    #[inline]
    pub fn skip_struct_value(&mut self) -> Result<()> {
        let frame_idx = self.struct_frames.len() - 1;
        if self.struct_frames[frame_idx].byname_in_defaults {
            // A missing-target default key: nothing in the input to skip.
            return Ok(());
        }
        self.skip_layout();
        if self.pos < self.input.len() && self.input[self.pos] == b')' {
            return Ok(());
        }
        self.skip_value()
    }

    /// ByName mode: produce a type default for an unmatched target field.
    #[inline]
    pub fn struct_field_default<T: AsunDecode<'de>>(&mut self) -> Result<T> {
        self.decode_default::<T>()
    }

    /// Finish decoding a struct: skip any trailing source fields, close the
    /// tuple, and restore parent schema state.
    pub fn end_struct_decode(&mut self) -> Result<()> {
        let frame = self
            .struct_frames
            .pop()
            .expect("end_struct_decode without begin_struct_decode");

        if self.default_depth > 0 {
            self.field_index = frame.parent_field_index;
            return Ok(());
        }

        self.leave();

        if frame.close_paren {
            self.skip_layout();
            // Exact mode with a full field match leaves the cursor right on the
            // closing paren, which is the overwhelmingly common case.
            if self.pos < self.input.len() && self.input[self.pos] == b')' {
                self.pos += 1;
            } else {
                self.skip_remaining_tuple_values()?;
                self.skip_layout();
                if self.pos < self.input.len() && self.input[self.pos] == b')' {
                    self.pos += 1;
                }
            }
        }

        if frame.restore_parent {
            self.schema_fields = frame.parent_schema;
        } else if frame.close_paren && frame.parent_schema.is_none() {
            // Top-level `{schema}:(...)` and bare `(...)` paths clear to None.
            self.schema_fields = None;
        }
        Ok(())
    }

    // -----------------------------------------------------------------------
    // Enum seam (used by derived AsunDecode impls)
    // -----------------------------------------------------------------------

    /// Begin decoding an enum: consume an optional opening `(` and read the
    /// variant name. Mirrors `deserialize_enum` + `variant_seed`.
    pub fn begin_enum(&mut self) -> Result<String> {
        if self.default_depth > 0 {
            return Err(Error::ExpectedValue);
        }
        self.skip_layout();
        let opened = if self.peek_byte()? == b'(' {
            self.pos += 1;
            true
        } else {
            false
        };
        self.enum_opened_paren = opened;
        self.skip_layout();
        let cow = self.parse_any_value_str()?;
        Ok(match cow {
            CowStr::Borrowed(s) => s.to_owned(),
            CowStr::Owned(s) => s,
        })
    }

    /// Unit variant: nothing to read (mirrors `VariantAccess::unit_variant`).
    #[inline]
    pub fn finish_unit_variant(&mut self) -> Result<()> {
        Ok(())
    }

    /// Newtype variant: skip the comma after the variant name, then decode the
    /// inner value. Mirrors `VariantAccess::newtype_variant_seed`.
    #[inline]
    pub fn newtype_variant_value<T: AsunDecode<'de>>(&mut self) -> Result<T> {
        self.skip_layout();
        if self.pos < self.input.len() && self.input[self.pos] == b',' {
            self.pos += 1;
        }
        T::decode(self)
    }

    /// Tuple / struct variant body: skip the comma after the variant name, then
    /// read elements positionally via `tuple_element`. Mirrors
    /// `VariantAccess::tuple_variant` / `struct_variant` element reads.
    #[inline]
    pub fn begin_tuple_variant_body(&mut self) -> Result<()> {
        self.skip_layout();
        if self.pos < self.input.len() && self.input[self.pos] == b',' {
            self.pos += 1;
        }
        self.field_index = 0;
        Ok(())
    }

    /// Finish decoding an enum: consume the closing `)` if we opened one.
    #[inline]
    pub fn end_enum(&mut self) -> Result<()> {
        if self.enum_opened_paren {
            self.skip_layout();
            if self.pos < self.input.len() && self.input[self.pos] == b')' {
                self.pos += 1;
            }
            self.enum_opened_paren = false;
        }
        Ok(())
    }
}

#[derive(Clone, Copy)]
enum SchemaFields<'de> {
    /// Borrowed from a schema pinned in [`Decoder::schema_arena`].
    Cached(&'de [Box<str>]),
    Static(&'static [&'static str]),
}

#[derive(Clone, Copy, PartialEq, Eq)]
struct SchemaFieldsKey {
    ptr: usize,
    len: usize,
}

#[derive(Clone, Copy)]
struct CachedStructMode {
    cache_key: StructModeCacheKey,
    plan: StructPlan,
}

impl<'de> SchemaFields<'de> {
    #[inline(always)]
    fn len(&self) -> usize {
        match self {
            Self::Cached(names) => names.len(),
            Self::Static(fields) => fields.len(),
        }
    }

    #[inline(always)]
    fn name_at(&self, index: usize) -> &'de str {
        match self {
            Self::Cached(names) => &names[index],
            Self::Static(fields) => fields[index],
        }
    }

    #[inline(always)]
    fn cache_key(&self) -> SchemaFieldsKey {
        match self {
            Self::Cached(names) => SchemaFieldsKey {
                ptr: names.as_ptr() as usize,
                len: names.len(),
            },
            Self::Static(fields) => SchemaFieldsKey {
                ptr: fields.as_ptr() as usize,
                len: fields.len(),
            },
        }
    }

    #[inline]
    fn matches_exact(&self, target_fields: &'static [&'static str]) -> bool {
        if self.len() != target_fields.len() {
            return false;
        }
        target_fields
            .iter()
            .enumerate()
            .all(|(idx, target)| self.name_at(idx) == *target)
    }

    #[inline]
    fn contains_name(&self, target: &str) -> bool {
        match self {
            Self::Cached(names) => names.iter().any(|n| &**n == target),
            Self::Static(fields) => fields.contains(&target),
        }
    }

    #[inline]
    fn missing_target_fields(&self, target_fields: &'static [&'static str]) -> Box<[&'static str]> {
        target_fields
            .iter()
            .copied()
            .filter(|target| !self.contains_name(target))
            .collect()
    }
}

/// How a struct row lines up with the source schema. `Copy` on purpose: it is
/// looked up once per struct value, and the previous refcounted representation
/// cost an atomic increment/decrement pair on every single row.
#[derive(Clone, Copy, PartialEq, Eq)]
enum StructPlan {
    Exact,
    /// Index into [`Decoder::missing_arena`].
    ByName(u32),
}

/// Lightweight Cow-like enum to avoid std::borrow::Cow overhead
enum CowStr<'a> {
    Borrowed(&'a str),
    Owned(String),
}

impl<'a> CowStr<'a> {
    #[inline]
    fn as_str(&self) -> &str {
        match self {
            CowStr::Borrowed(s) => s,
            CowStr::Owned(s) => s,
        }
    }
}

/// Parse exactly four hex digits at `at`.
///
/// Deliberately byte-wise: the previous `from_utf8_unchecked` over four raw
/// input bytes could build an invalid `&str` when the escape was followed by a
/// multi-byte character, and `from_str_radix` additionally accepted junk like
/// `+123`.
#[inline]
fn hex4(bytes: &[u8], at: usize) -> Result<u32> {
    if at + 4 > bytes.len() {
        return Err(Error::InvalidUnicodeEscape);
    }
    let mut cp = 0u32;
    for k in 0..4 {
        let d = match bytes[at + k] {
            c @ b'0'..=b'9' => c - b'0',
            c @ b'a'..=b'f' => c - b'a' + 10,
            c @ b'A'..=b'F' => c - b'A' + 10,
            _ => return Err(Error::InvalidUnicodeEscape),
        };
        cp = (cp << 4) | d as u32;
    }
    Ok(cp)
}

/// Read a `\uXXXX` escape whose first hex digit is at `*pos`, joining a
/// UTF-16 surrogate pair when present. `*pos` ends just past the escape.
#[inline]
fn read_unicode_escape(input: &[u8], pos: &mut usize) -> Result<char> {
    let hi = hex4(input, *pos)?;
    *pos += 4;
    let cp = if (0xD800..0xDC00).contains(&hi) {
        if input.get(*pos) != Some(&b'\\') || input.get(*pos + 1) != Some(&b'u') {
            return Err(Error::InvalidUnicodeEscape);
        }
        let lo = hex4(input, *pos + 2)?;
        if !(0xDC00..0xE000).contains(&lo) {
            return Err(Error::InvalidUnicodeEscape);
        }
        *pos += 6;
        0x1_0000 + ((hi - 0xD800) << 10) + (lo - 0xDC00)
    } else if (0xDC00..0xE000).contains(&hi) {
        // Unpaired low surrogate.
        return Err(Error::InvalidUnicodeEscape);
    } else {
        hi
    };
    char::from_u32(cp).ok_or(Error::InvalidUnicodeEscape)
}

/// Unescape a plain (unquoted) value.
///
/// Works on bytes and bulk-copies the runs between escapes. The previous
/// implementation pushed `bytes[i] as char`, which reinterprets each byte as a
/// code point and therefore corrupted every multi-byte character in any value
/// that also contained an escape.
fn unescape_plain(s: &str) -> Result<String> {
    let bytes = s.as_bytes();
    let mut out: Vec<u8> = Vec::with_capacity(bytes.len());
    let mut i = 0;
    while i < bytes.len() {
        let run_start = i;
        while i < bytes.len() && bytes[i] != b'\\' {
            i += 1;
        }
        out.extend_from_slice(&bytes[run_start..i]);
        if i >= bytes.len() {
            break;
        }
        i += 1;
        if i >= bytes.len() {
            return Err(Error::Eof);
        }
        match bytes[i] {
            b @ (b',' | b'(' | b')' | b'[' | b']' | b'{' | b'}' | b':' | b'@' | b'"' | b'\\') => {
                out.push(b)
            }
            b'n' => out.push(b'\n'),
            b't' => out.push(b'\t'),
            b'r' => out.push(b'\r'),
            b'b' => out.push(0x08),
            b'f' => out.push(0x0c),
            b'u' => {
                let mut p = i + 1;
                let ch = read_unicode_escape(bytes, &mut p)?;
                let mut tmp = [0u8; 4];
                out.extend_from_slice(ch.encode_utf8(&mut tmp).as_bytes());
                i = p - 1;
            }
            other => return Err(Error::InvalidEscape(other as char)),
        }
        i += 1;
    }
    // SAFETY: `s` is valid UTF-8; runs are cut at ASCII `\` so they stay on
    // character boundaries, and every pushed replacement is valid UTF-8.
    Ok(unsafe { String::from_utf8_unchecked(out) })
}