rustbinary 0.1.7

A bounded nextjson binary codec with adaptive frames, zero-allocation paths, schema evolution, and authenticated pipelines
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
//! Streaming RFC 8949 CBOR codec implementing [`nextjson::FormatEncoder`]
//! and [`nextjson::FormatDecoder`] directly over the wire bytes.
//!
//! This module is RustBinary's own CBOR implementation. It is the native
//! `T -> events -> CBOR bytes` / `CBOR bytes -> events -> T` path: values are
//! decoded straight from the input slice into the requested type with no
//! intermediate value tree and no JSON text round-trip, so the memory peak of
//! a decoded value is the decoded value itself.
//!
//! # Wire profile
//!
//! The profile is deliberately small and follows the JSON-compatible
//! subset of RFC 8949 plus explicit native byte strings, matching what the
//! rest of the crate's cross-format surface accepts:
//!
//! - `null` → `0xf6`, `false`/`true` → `0xf4`/`0xf5`;
//! - integers encode with the shortest definite head (major 0/1); values
//!   beyond `u64`/`i64` use the RFC 8949 bignum tags `2`/`3`;
//! - `f32`/`f64` → `0xfa`/`0xfb`; half-precision `0xf9` decodes to `f64`;
//! - text strings → major 3, byte strings → major 2 (native byte-string wire
//!   type used by [`FormatEncoder::write_bytes`]);
//! - arrays and maps use the indefinite-length forms (`0x9f`/`0xbf` …
//!   `0xff`), matching the crate's streaming encoder (no length patching);
//! - object keys are text strings, matching the JSON data model;
//! - `Option` maps to the JSON shape: `None` → `null`, `Some` → payload.
//!
//! Decoding accepts both definite- and indefinite-length containers and text
//! strings. Non-`2/3` semantic tags are ignored (their payload is decoded),
//! matching RFC 8949's model that tags annotate, never replace, a value.
//!
//! # Resource limits
//!
//! The same [`crate::Config`] policies as the native codec apply: the byte
//! limit bounds every read/write, the collection limit bounds the element
//! count of one sequence or map (checked eagerly for definite containers),
//! and nesting is capped at [`crate::tags::MAX_DEPTH`].
//!
//! # Semantics of `bytes`
//!
//! [`FormatDecoder::bytes`] is overridden to read the native major-2 byte
//! string (or an array of `u8`, matching the JSON spelling). Because the
//! nextjson token model has no byte-string token, `peek_token`/`next_token`
//! reject a raw major-2 value as outside the JSON-compatible value model;
//! `bytes()` and `option_tag` inspect the wire directly so `Option<Bytes>`
//! style payloads keep working.

use alloc::borrow::Cow;
use alloc::vec::Vec;

use nextjson::de::Mark;
use nextjson::Error as NextjsonError;
use nextjson::{FormatDecoder, FormatEncoder, Number, OptionTag, Token};

use crate::{
    config::Config,
    error::{Error, Result},
    tags::MAX_DEPTH,
    writer::EncodeWriter,
};

type NextjsonResult<T> = core::result::Result<T, NextjsonError>;

// ---------------------------------------------------------------------------
// Encoding
// ---------------------------------------------------------------------------

/// Streaming RFC 8949 CBOR encoder driven by nextjson's event contract.
pub(crate) struct CborEncoder<W: EncodeWriter> {
    writer: W,
    config: Config,
    written: u64,
    depth: usize,
    counts: [u64; MAX_DEPTH],
    wire_error: Option<Error>,
}

impl<W: EncodeWriter> CborEncoder<W> {
    /// Creates an encoder over `writer` with the resource policies of
    /// `config`. Only the byte and collection limits are honored; CBOR itself
    /// fixes byte order (big endian) and shortest-head integers, so
    /// `config`'s endian/integer profile does not apply here.
    pub(crate) fn new(writer: W, config: Config) -> Self {
        Self {
            writer,
            config,
            written: 0,
            depth: 0,
            counts: [0; MAX_DEPTH],
            wire_error: None,
        }
    }

    /// Encodes `value` and returns the number of bytes written.
    pub(crate) fn finish<T: nextjson::NsonSerialize + ?Sized>(mut self, value: &T) -> Result<u64> {
        nextjson::NsonSerialize::nextencode(value, &mut self).map_err(|error| {
            self.wire_error
                .take()
                .unwrap_or_else(|| Error::from_nextjson(error))
        })?;
        if self.depth != 0 {
            return Err(Error::Custom(
                "encoder finished inside an unclosed container".into(),
            ));
        }
        Ok(self.written)
    }

    fn fail(&mut self, error: Error) -> NextjsonError {
        self.wire_error = Some(error);
        NextjsonError::custom("rustbinary cbor wire error")
    }

    fn emit(&mut self, bytes: &[u8]) -> Result<()> {
        let amount =
            u64::try_from(bytes.len()).map_err(|_| Error::IntegerOverflow { target: "u64" })?;
        let next = self
            .written
            .checked_add(amount)
            .ok_or(Error::SizeLimit { limit: u64::MAX })?;
        if let Some(limit) = self.config.limit {
            if next > limit {
                return Err(Error::SizeLimit { limit });
            }
        }
        self.writer.write_all(bytes)?;
        self.written = next;
        Ok(())
    }

    /// Writes the shortest RFC 8949 head for `(major, argument)`.
    fn head(&mut self, major: u8, argument: u64) -> NextjsonResult<()> {
        let prefix = major << 5;
        let mut buffer = [0_u8; 9];
        let len = if argument < 24 {
            buffer[0] = prefix | argument as u8;
            1
        } else if argument <= u8::MAX as u64 {
            buffer[0] = prefix | 24;
            buffer[1] = argument as u8;
            2
        } else if argument <= u16::MAX as u64 {
            buffer[0] = prefix | 25;
            buffer[1..3].copy_from_slice(&(argument as u16).to_be_bytes());
            3
        } else if argument <= u32::MAX as u64 {
            buffer[0] = prefix | 26;
            buffer[1..5].copy_from_slice(&(argument as u32).to_be_bytes());
            5
        } else {
            buffer[0] = prefix | 27;
            buffer[1..9].copy_from_slice(&argument.to_be_bytes());
            9
        };
        self.emit(&buffer[..len]).map_err(|error| self.fail(error))
    }

    fn unsigned(&mut self, value: u128) -> NextjsonResult<()> {
        match u64::try_from(value) {
            Ok(value) => self.head(0, value),
            Err(_) => self.bignum(2, value),
        }
    }

    fn signed(&mut self, value: i128) -> NextjsonResult<()> {
        if value >= 0 {
            return self.unsigned(value as u128);
        }
        let argument = (-1 - value) as u128;
        match u64::try_from(argument) {
            Ok(argument) => self.head(1, argument),
            Err(_) => self.bignum(3, argument),
        }
    }

    fn bignum(&mut self, tag: u64, value: u128) -> NextjsonResult<()> {
        self.head(6, tag)?;
        let bytes = value.to_be_bytes();
        let first = bytes
            .iter()
            .position(|byte| *byte != 0)
            .unwrap_or(bytes.len() - 1);
        let magnitude = &bytes[first..];
        self.head(2, magnitude.len() as u64)?;
        self.emit(magnitude).map_err(|error| self.fail(error))
    }

    fn write_text(&mut self, value: &str) -> NextjsonResult<()> {
        self.head(3, value.len() as u64)?;
        self.emit(value.as_bytes())
            .map_err(|error| self.fail(error))
    }

    fn enter_container(&mut self) -> NextjsonResult<()> {
        if self.depth >= MAX_DEPTH {
            return Err(self.fail(Error::Custom("encoder nesting depth limit exceeded".into())));
        }
        self.depth += 1;
        Ok(())
    }

    fn exit_container(&mut self) -> NextjsonResult<()> {
        if self.depth == 0 {
            return Err(self.fail(Error::Custom("container end without matching start".into())));
        }
        self.depth -= 1;
        self.counts[self.depth] = 0;
        self.emit(&[0xff]).map_err(|error| self.fail(error))
    }

    fn count_element(&mut self) -> NextjsonResult<()> {
        let index = self
            .depth
            .checked_sub(1)
            .ok_or_else(|| self.fail(Error::Custom("element outside any container".into())))?;
        let count = self.counts[index]
            .checked_add(1)
            .ok_or_else(|| self.fail(Error::CollectionLimit { limit: u64::MAX }))?;
        if let Some(limit) = self.config.collection_limit {
            if count > limit {
                return Err(self.fail(Error::CollectionLimit { limit }));
            }
        }
        self.counts[index] = count;
        Ok(())
    }
}

impl<W: EncodeWriter> FormatEncoder for CborEncoder<W> {
    type Error = NextjsonError;

    fn begin_array(&mut self) -> NextjsonResult<()> {
        self.enter_container()?;
        self.emit(&[0x9f]).map_err(|error| self.fail(error))
    }

    fn separator(&mut self) -> NextjsonResult<()> {
        self.count_element()
    }

    fn end_array(&mut self) -> NextjsonResult<()> {
        self.exit_container()
    }

    fn begin_object(&mut self) -> NextjsonResult<()> {
        self.enter_container()?;
        self.emit(&[0xbf]).map_err(|error| self.fail(error))
    }

    fn key(&mut self, key: &str) -> NextjsonResult<()> {
        self.count_element()?;
        self.write_text(key)
    }

    fn end_object(&mut self) -> NextjsonResult<()> {
        self.exit_container()
    }

    fn write_null(&mut self) -> NextjsonResult<()> {
        self.emit(&[0xf6]).map_err(|error| self.fail(error))
    }

    fn write_bool(&mut self, value: bool) -> NextjsonResult<()> {
        self.emit(&[if value { 0xf5 } else { 0xf4 }])
            .map_err(|error| self.fail(error))
    }

    fn write_str(&mut self, value: &str) -> NextjsonResult<()> {
        self.write_text(value)
    }

    fn write_char(&mut self, value: char) -> NextjsonResult<()> {
        let mut buffer = [0_u8; 4];
        self.write_text(value.encode_utf8(&mut buffer))
    }

    fn write_number(&mut self, value: &Number) -> NextjsonResult<()> {
        match *value {
            Number::U64(value) => self.write_u64(value),
            Number::U128(value) => self.write_u128(value),
            Number::I64(value) => self.write_i64(value),
            Number::I128(value) => self.write_i128(value),
            Number::F64(value) => self.write_f64(value),
        }
    }

    fn write_i64(&mut self, value: i64) -> NextjsonResult<()> {
        self.signed(value as i128)
    }

    fn write_u64(&mut self, value: u64) -> NextjsonResult<()> {
        self.unsigned(value as u128)
    }

    fn write_i128(&mut self, value: i128) -> NextjsonResult<()> {
        self.signed(value)
    }

    fn write_u128(&mut self, value: u128) -> NextjsonResult<()> {
        self.unsigned(value)
    }

    fn write_f64(&mut self, value: f64) -> NextjsonResult<()> {
        self.emit(&[0xfb]).map_err(|error| self.fail(error))?;
        self.emit(&value.to_bits().to_be_bytes())
            .map_err(|error| self.fail(error))
    }

    fn write_f32(&mut self, value: f32) -> NextjsonResult<()> {
        self.emit(&[0xfa]).map_err(|error| self.fail(error))?;
        self.emit(&value.to_bits().to_be_bytes())
            .map_err(|error| self.fail(error))
    }

    fn write_bytes(&mut self, value: &[u8]) -> NextjsonResult<()> {
        self.head(2, value.len() as u64)?;
        self.emit(value).map_err(|error| self.fail(error))
    }

    fn write_none(&mut self) -> NextjsonResult<()> {
        self.write_null()
    }

    fn write_some(&mut self) -> NextjsonResult<()> {
        Ok(())
    }

    fn is_human_readable(&self) -> bool {
        false
    }
}

// ---------------------------------------------------------------------------
// Decoding
// ---------------------------------------------------------------------------

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
enum ContainerKind {
    Array,
    Object,
}

#[derive(Clone, Copy, Debug)]
struct Frame {
    kind: ContainerKind,
    /// `Some(remaining)` for definite containers, `None` for indefinite ones.
    definite: Option<u64>,
}

#[derive(Clone, Copy, Debug)]
enum HeadArg {
    Value(u64),
    /// Major type 7 `ai == 25`: IEEE 754 half-precision float.
    Float16,
    /// Major type 7 `ai == 26`: IEEE 754 single-precision float.
    Float32,
    /// Major type 7 `ai == 27`: IEEE 754 double-precision float.
    Float64,
    Indefinite,
}

impl HeadArg {
    fn value(self) -> u64 {
        match self {
            HeadArg::Value(value) => value,
            _ => unreachable!("non-value argument where a value is required"),
        }
    }
}

/// Streaming RFC 8949 CBOR decoder implementing [`nextjson::FormatDecoder`].
///
/// Pull-based: every read advances over the input slice directly, so decoded
/// text strings borrow from the input and no value tree is materialized.
pub(crate) struct CborDecoder<'de> {
    input: &'de [u8],
    cursor: usize,
    config: Config,
    depth: usize,
    counts: [u64; MAX_DEPTH],
    frames: Vec<Frame>,
    lookahead: Option<Token<'de>>,
    wire_error: Option<Error>,
    expecting: Option<&'static str>,
}

impl<'de> CborDecoder<'de> {
    pub(crate) fn new(input: &'de [u8], config: Config) -> Self {
        Self {
            input,
            cursor: 0,
            config,
            depth: 0,
            counts: [0; MAX_DEPTH],
            frames: Vec::new(),
            lookahead: None,
            wire_error: None,
            expecting: None,
        }
    }

    /// Decodes one value that may borrow from `input`, then validates the
    /// trailing-byte policy.
    pub(crate) fn decode<T: nextjson::NsonDeserialize<'de>>(mut self) -> Result<T> {
        let value = T::nextdecode(&mut self).map_err(|error| {
            self.wire_error
                .take()
                .unwrap_or_else(|| Error::from_nextjson(error))
        })?;
        if self.depth != 0 {
            return Err(Error::Custom(
                "decoder finished inside an unclosed container".into(),
            ));
        }
        if self.config.trailing == crate::config::TrailingBytes::Reject
            && self.cursor != self.input.len()
        {
            return Err(Error::TrailingBytes {
                remaining: self.input.len() - self.cursor,
            });
        }
        Ok(value)
    }

    fn fail(&mut self, error: Error) -> NextjsonError {
        self.wire_error = Some(error);
        NextjsonError::custom("rustbinary cbor wire error")
    }

    fn invalid_type(&self, expected: &'static str, found: &Token<'_>) -> NextjsonError {
        let expected = match (self.expecting, expected) {
            (Some(expecting), "an object" | "an array") => expecting,
            _ => expected,
        };
        NextjsonError::invalid_type(expected, token_name(found))
    }

    fn take(&mut self, len: usize) -> Result<&'de [u8]> {
        let end = self.cursor.checked_add(len).ok_or(Error::UnexpectedEnd)?;
        if let Some(limit) = self.config.limit {
            if end as u64 > limit {
                return Err(Error::SizeLimit { limit });
            }
        }
        let bytes = self
            .input
            .get(self.cursor..end)
            .ok_or(Error::UnexpectedEnd)?;
        self.cursor = end;
        Ok(bytes)
    }

    fn peek_byte(&mut self) -> Result<u8> {
        let byte = *self.input.get(self.cursor).ok_or(Error::UnexpectedEnd)?;
        if let Some(limit) = self.config.limit {
            if (self.cursor as u64).saturating_add(1) > limit {
                return Err(Error::SizeLimit { limit });
            }
        }
        Ok(byte)
    }

    /// Reads one RFC 8949 head (major type + argument), consuming it.
    ///
    /// Major type 7 is special: `ai` 25/26/27 denote the float types
    /// themselves (no argument follows), `ai` 24 stores a simple value in the
    /// next byte, and 20/21/22 are `false`/`true`/`null`.
    fn read_head(&mut self) -> NextjsonResult<(u8, HeadArg)> {
        let initial = self.take(1).map_err(|error| self.fail(error))?[0];
        let major = initial >> 5;
        let ai = initial & 0x1f;
        let argument = if major == 7 {
            match ai {
                0..=23 => HeadArg::Value(ai as u64),
                24 => HeadArg::Value(24),
                25 => HeadArg::Float16,
                26 => HeadArg::Float32,
                27 => HeadArg::Float64,
                28..=30 => {
                    return Err(self.fail(Error::Custom(
                        "reserved CBOR additional information value".into(),
                    )))
                }
                31 => HeadArg::Indefinite,
                _ => unreachable!("additional information is five bits"),
            }
        } else {
            match ai {
                0..=23 => HeadArg::Value(ai as u64),
                24 => HeadArg::Value(self.take(1).map_err(|error| self.fail(error))?[0] as u64),
                25 => {
                    let bytes: [u8; 2] = self
                        .take(2)
                        .map_err(|error| self.fail(error))?
                        .try_into()
                        .expect("two bytes");
                    HeadArg::Value(u16::from_be_bytes(bytes) as u64)
                }
                26 => {
                    let bytes: [u8; 4] = self
                        .take(4)
                        .map_err(|error| self.fail(error))?
                        .try_into()
                        .expect("four bytes");
                    HeadArg::Value(u32::from_be_bytes(bytes) as u64)
                }
                27 => {
                    let bytes: [u8; 8] = self
                        .take(8)
                        .map_err(|error| self.fail(error))?
                        .try_into()
                        .expect("eight bytes");
                    HeadArg::Value(u64::from_be_bytes(bytes))
                }
                28..=30 => {
                    return Err(self.fail(Error::Custom(
                        "reserved CBOR additional information value".into(),
                    )))
                }
                31 => HeadArg::Indefinite,
                _ => unreachable!("additional information is five bits"),
            }
        };
        Ok((major, argument))
    }

    /// Decrements the definite element counter of the current container.
    fn dec_remaining(&mut self) -> NextjsonResult<()> {
        let underflow = match self.frames.last_mut() {
            Some(frame) => match &mut frame.definite {
                Some(remaining) => match remaining.checked_sub(1) {
                    Some(next) => {
                        *remaining = next;
                        false
                    }
                    None => true,
                },
                None => false,
            },
            None => false,
        };
        if underflow {
            return Err(self.fail(Error::Custom("too many container elements".into())));
        }
        Ok(())
    }

    /// Pushes a container frame after validating nesting depth and, for
    /// definite containers, the collection limit.
    fn enter_container(
        &mut self,
        kind: ContainerKind,
        definite: Option<u64>,
    ) -> NextjsonResult<()> {
        if self.depth >= MAX_DEPTH {
            return Err(self.fail(Error::Custom("decoder nesting depth limit exceeded".into())));
        }
        if let Some(count) = definite {
            if let Some(limit) = self.config.collection_limit {
                if count > limit {
                    return Err(self.fail(Error::CollectionLimit { limit }));
                }
            }
        }
        self.frames.push(Frame { kind, definite });
        self.depth += 1;
        Ok(())
    }

    fn pop_frame(&mut self, expected: ContainerKind) -> NextjsonResult<()> {
        let frame = self.frames.last().copied().ok_or_else(|| {
            self.fail(Error::Custom("container end without matching start".into()))
        })?;
        if frame.kind != expected {
            return Err(self.fail(Error::Custom(
                "container end does not match its start".into(),
            )));
        }
        self.frames.pop();
        self.depth -= 1;
        self.counts[self.depth] = 0;
        Ok(())
    }

    fn count_element(&mut self) -> NextjsonResult<()> {
        let index = self
            .depth
            .checked_sub(1)
            .ok_or_else(|| self.fail(Error::Custom("separator outside any container".into())))?;
        let count = self.counts[index]
            .checked_add(1)
            .ok_or_else(|| self.fail(Error::CollectionLimit { limit: u64::MAX }))?;
        if let Some(limit) = self.config.collection_limit {
            if count > limit {
                return Err(self.fail(Error::CollectionLimit { limit }));
            }
        }
        self.counts[index] = count;
        Ok(())
    }

    /// Whether the current container still has elements (does not count).
    fn has_more(&mut self) -> NextjsonResult<bool> {
        if self.lookahead.is_some() {
            return Ok(true);
        }
        let frame = match self.frames.last().copied() {
            Some(frame) => frame,
            None => return Err(self.fail(Error::Custom("separator outside any container".into()))),
        };
        match frame.definite {
            Some(remaining) => Ok(remaining > 0),
            None => Ok(self.peek_byte().map_err(|error| self.fail(error))? != 0xff),
        }
    }

    /// Reads the next token, consuming it. Container heads push a frame; the
    /// indefinite break (`0xff`) returns an end token without popping (the
    /// `end_array`/`end_object` methods own the pop).
    fn read_token(&mut self) -> NextjsonResult<Token<'de>> {
        loop {
            let (major, argument) = self.read_head()?;
            match major {
                0 => {
                    self.dec_remaining()?;
                    return Ok(Token::Number(Number::U64(argument.value())));
                }
                1 => {
                    self.dec_remaining()?;
                    let negative = argument.value();
                    // Value = -1 - negative, widened to i128 so i64::MIN.. works
                    // and values below i64::MIN stay exact as i128.
                    if negative <= i64::MAX as u64 {
                        return Ok(Token::Number(Number::I64(-1 - negative as i64)));
                    }
                    return Ok(Token::Number(Number::I128(-1 - negative as i128)));
                }
                2 => {
                    // Byte strings are outside the JSON-compatible value
                    // model; `FormatDecoder::bytes` reads them directly.
                    return Err(self.fail(Error::Custom(
                        "byte string (major type 2) is outside the value model; use FormatDecoder::bytes"
                            .into(),
                    )));
                }
                3 => {
                    let token = self.read_text(argument)?;
                    self.dec_remaining()?;
                    return Ok(token);
                }
                4 => {
                    self.dec_remaining()?;
                    let definite = match argument {
                        HeadArg::Value(count) => Some(count),
                        HeadArg::Indefinite => None,
                        _ => return Err(self.fail(Error::Custom("invalid array length".into()))),
                    };
                    self.enter_container(ContainerKind::Array, definite)?;
                    return Ok(Token::BeginArray);
                }
                5 => {
                    self.dec_remaining()?;
                    let definite = match argument {
                        HeadArg::Value(count) => Some(count),
                        HeadArg::Indefinite => None,
                        _ => return Err(self.fail(Error::Custom("invalid object length".into()))),
                    };
                    self.enter_container(ContainerKind::Object, definite)?;
                    return Ok(Token::BeginObject);
                }
                6 => {
                    let tag = argument.value();
                    match tag {
                        2 => {
                            let token = self.read_bignum(false)?;
                            self.dec_remaining()?;
                            return Ok(token);
                        }
                        3 => {
                            let token = self.read_bignum(true)?;
                            self.dec_remaining()?;
                            return Ok(token);
                        }
                        // Other semantic tags annotate rather than replace a
                        // value: skip the tag and decode the payload.
                        _ => continue,
                    }
                }
                7 => match argument {
                    HeadArg::Value(20) => {
                        self.dec_remaining()?;
                        return Ok(Token::Bool(false));
                    }
                    HeadArg::Value(21) => {
                        self.dec_remaining()?;
                        return Ok(Token::Bool(true));
                    }
                    HeadArg::Value(22) => {
                        self.dec_remaining()?;
                        return Ok(Token::Null);
                    }
                    HeadArg::Value(23) => {
                        return Err(self.fail(Error::Custom(
                            "undefined (0xf7) is outside the value model".into(),
                        )))
                    }
                    HeadArg::Value(24) => {
                        // Simple value stored in the following byte; none of
                        // the defined simple values are in the JSON model.
                        let _ = self.take(1).map_err(|error| self.fail(error))?;
                        return Err(
                            self.fail(Error::Custom("unsupported CBOR simple value".into()))
                        );
                    }
                    HeadArg::Float16 => {
                        self.dec_remaining()?;
                        let bytes: [u8; 2] = self
                            .take(2)
                            .map_err(|error| self.fail(error))?
                            .try_into()
                            .expect("two bytes");
                        return Ok(Token::Number(Number::F64(half_to_f64(u16::from_be_bytes(
                            bytes,
                        )))));
                    }
                    HeadArg::Float32 => {
                        self.dec_remaining()?;
                        let bytes: [u8; 4] = self
                            .take(4)
                            .map_err(|error| self.fail(error))?
                            .try_into()
                            .expect("four bytes");
                        let value = f32::from_be_bytes(bytes);
                        return Ok(Token::Number(Number::F64(value as f64)));
                    }
                    HeadArg::Float64 => {
                        self.dec_remaining()?;
                        let bytes: [u8; 8] = self
                            .take(8)
                            .map_err(|error| self.fail(error))?
                            .try_into()
                            .expect("eight bytes");
                        return Ok(Token::Number(Number::F64(f64::from_be_bytes(bytes))));
                    }
                    HeadArg::Value(_) => {
                        return Err(self.fail(Error::Custom("unsupported CBOR simple value".into())))
                    }
                    // The indefinite break that terminates an indefinite
                    // container.
                    HeadArg::Indefinite => {
                        let kind = match self.frames.last().copied() {
                            Some(frame) => frame.kind,
                            None => {
                                return Err(self.fail(Error::Custom(
                                    "indefinite break outside any container".into(),
                                )))
                            }
                        };
                        return Ok(match kind {
                            ContainerKind::Array => Token::EndArray,
                            ContainerKind::Object => Token::EndObject,
                        });
                    }
                },
                _ => unreachable!("major type is three bits"),
            }
        }
    }

    /// Reads a text string whose head has already been consumed.
    fn read_text(&mut self, argument: HeadArg) -> NextjsonResult<Token<'de>> {
        match argument {
            HeadArg::Value(len) => {
                let bytes = self.take(len as usize).map_err(|error| self.fail(error))?;
                let text = core::str::from_utf8(bytes).map_err(|_| {
                    self.fail(Error::Custom("CBOR text string is not valid UTF-8".into()))
                })?;
                Ok(Token::Str(Cow::Borrowed(text)))
            }
            HeadArg::Indefinite => {
                // Chunked text string: definite-length chunks terminated by
                // 0xff. Decoded chunks must be materialized.
                let mut out = Vec::new();
                loop {
                    let byte = self.peek_byte().map_err(|error| self.fail(error))?;
                    if byte == 0xff {
                        self.cursor += 1;
                        break;
                    }
                    let (major, argument) = self.read_head()?;
                    if major != 3 {
                        return Err(self.fail(Error::Custom(
                            "indefinite text string contains a non-text chunk".into(),
                        )));
                    }
                    let len = match argument {
                        HeadArg::Value(len) => len,
                        _ => {
                            return Err(
                                self.fail(Error::Custom("invalid text string length".into()))
                            )
                        }
                    };
                    out.extend_from_slice(
                        self.take(len as usize).map_err(|error| self.fail(error))?,
                    );
                }
                let text = core::str::from_utf8(&out).map_err(|_| {
                    self.fail(Error::Custom("CBOR text string is not valid UTF-8".into()))
                })?;
                Ok(Token::Str(Cow::Owned(text.to_owned())))
            }
            _ => Err(self.fail(Error::Custom("invalid text string length".into()))),
        }
    }
    /// Reads an RFC 8949 bignum (tag 2 or 3). The tag head is already
    /// consumed; the payload is a major-2 byte string holding the big-endian
    /// magnitude.
    fn read_bignum(&mut self, negative: bool) -> NextjsonResult<Token<'de>> {
        let (major, argument) = self.read_head()?;
        if major != 2 {
            return Err(self.fail(Error::Custom("bignum payload must be a byte string".into())));
        }
        let len = match argument {
            HeadArg::Value(len) => len,
            HeadArg::Indefinite => {
                return Err(self.fail(Error::Custom(
                    "indefinite bignum payload is not supported".into(),
                )))
            }
            _ => {
                return Err(self.fail(Error::Custom("bignum payload must be a byte string".into())))
            }
        };
        let bytes = self.take(len as usize).map_err(|error| self.fail(error))?;
        if bytes.len() > 16 {
            return Err(self.fail(Error::Custom(
                "bignum wider than 128 bits is not supported".into(),
            )));
        }
        let mut magnitude = [0_u8; 16];
        magnitude[16 - bytes.len()..].copy_from_slice(bytes);
        let value = u128::from_be_bytes(magnitude);
        if negative {
            // -1 - value
            Ok(Token::Number(Number::I128(-1 - value as i128)))
        } else {
            Ok(Token::Number(Number::U128(value)))
        }
    }

    /// Reads the next token without consuming it (honoring any pending
    /// lookahead).
    fn peek_token_inner(&mut self) -> NextjsonResult<Token<'de>> {
        if self.lookahead.is_none() {
            self.lookahead = Some(self.read_token()?);
        }
        Ok(self.lookahead.clone().expect("lookahead initialized"))
    }

    /// Consumes and returns the next token.
    fn take_token(&mut self) -> NextjsonResult<Token<'de>> {
        match self.lookahead.take() {
            Some(token) => Ok(token),
            None => self.read_token(),
        }
    }

    /// Skips one complete value (used for unknown object fields).
    fn skip_value_inner(&mut self) -> NextjsonResult<()> {
        let saved_depth = self.depth;
        self.take_token()?;
        while self.depth > saved_depth {
            let frame = match self.frames.last().copied() {
                Some(frame) => frame,
                None => return Err(self.fail(Error::Custom("unbalanced container in skip".into()))),
            };
            let finished = match frame.definite {
                Some(0) => true,
                Some(_) => false,
                None => self.peek_byte().map_err(|error| self.fail(error))? == 0xff,
            };
            if finished {
                if frame.definite.is_none() {
                    self.cursor += 1;
                }
                self.pop_frame(frame.kind)?;
                continue;
            }
            self.take_token()?;
        }
        Ok(())
    }
}

impl<'de> FormatDecoder<'de> for CborDecoder<'de> {
    type Error = NextjsonError;

    fn begin_object(&mut self) -> NextjsonResult<()> {
        match self.take_token()? {
            Token::BeginObject => Ok(()),
            other => Err(self.invalid_type("an object", &other)),
        }
    }

    fn end_object(&mut self) -> NextjsonResult<()> {
        self.end_container(ContainerKind::Object)
    }

    fn object_key(&mut self) -> NextjsonResult<Option<Cow<'de, str>>> {
        if let Some(token) = self.lookahead.take() {
            return match token {
                Token::Str(key) => Ok(Some(key)),
                Token::EndObject => Ok(None),
                other => Err(self.invalid_type("an object key string", &other)),
            };
        }
        let frame = match self.frames.last().copied() {
            Some(frame) => frame,
            None => return Err(self.fail(Error::Custom("object key outside any object".into()))),
        };
        if frame.kind != ContainerKind::Object {
            return Err(self.fail(Error::Custom(
                "object key outside an object container".into(),
            )));
        }
        match frame.definite {
            Some(0) => return Ok(None),
            Some(_) => {}
            None => {
                if self.peek_byte().map_err(|error| self.fail(error))? == 0xff {
                    // Leave the break for `end_object`.
                    return Ok(None);
                }
            }
        }
        let (major, argument) = self.read_head()?;
        if major != 3 {
            let found = match major {
                0 | 1 | 7 => Token::Number(Number::U64(0)),
                4 => Token::BeginArray,
                5 => Token::BeginObject,
                _ => Token::Null,
            };
            return Err(self.invalid_type("an object key string", &found));
        }
        match self.read_text(argument)? {
            Token::Str(key) => Ok(Some(key)),
            _ => unreachable!("read_text always returns a string token"),
        }
    }

    fn object_entry_sep(&mut self) -> NextjsonResult<bool> {
        self.count_element()?;
        self.has_more()
    }

    fn begin_array(&mut self) -> NextjsonResult<()> {
        match self.take_token()? {
            Token::BeginArray => Ok(()),
            other => Err(self.invalid_type("an array", &other)),
        }
    }

    fn end_array(&mut self) -> NextjsonResult<()> {
        self.end_container(ContainerKind::Array)
    }

    fn array_has_more(&mut self) -> NextjsonResult<bool> {
        self.has_more()
    }

    fn array_entry_sep(&mut self) -> NextjsonResult<bool> {
        self.count_element()?;
        self.has_more()
    }

    fn unit(&mut self) -> NextjsonResult<()> {
        match self.take_token()? {
            Token::Null => Ok(()),
            other => Err(self.invalid_type("null", &other)),
        }
    }

    fn bool(&mut self) -> NextjsonResult<bool> {
        match self.take_token()? {
            Token::Bool(value) => Ok(value),
            other => Err(self.invalid_type("a boolean", &other)),
        }
    }

    fn number(&mut self) -> NextjsonResult<Number> {
        match self.take_token()? {
            Token::Number(value) => Ok(value),
            other => Err(self.invalid_type("a number", &other)),
        }
    }

    fn string(&mut self) -> NextjsonResult<Cow<'de, str>> {
        match self.take_token()? {
            Token::Str(value) => Ok(value),
            other => Err(self.invalid_type("a string", &other)),
        }
    }

    fn char(&mut self) -> NextjsonResult<char> {
        match self.take_token()? {
            Token::Str(value) => {
                let mut chars = value.chars();
                match (chars.next(), chars.next()) {
                    (Some(ch), None) => Ok(ch),
                    _ => Err(self.fail(Error::InvalidChar)),
                }
            }
            other => Err(self.invalid_type("a character", &other)),
        }
    }

    fn bytes(&mut self) -> NextjsonResult<Cow<'de, [u8]>> {
        // Inspect the wire directly (bypassing the token model, which has no
        // byte-string token): major 2 is the native byte string, major 3 the
        // JSON spelling (text), major 4 an array of bytes.
        let byte = self.peek_byte().map_err(|error| self.fail(error))?;
        let major = byte >> 5;
        match major {
            2 => {
                let (major2, argument) = self.read_head()?;
                debug_assert_eq!(major2, 2);
                match argument {
                    HeadArg::Value(len) => {
                        let bytes = self.take(len as usize).map_err(|error| self.fail(error))?;
                        Ok(Cow::Borrowed(bytes))
                    }
                    HeadArg::Indefinite => {
                        let mut out = Vec::new();
                        loop {
                            let next = self.peek_byte().map_err(|error| self.fail(error))?;
                            if next == 0xff {
                                self.cursor += 1;
                                break;
                            }
                            let (maj, arg) = self.read_head()?;
                            if maj != 2 {
                                return Err(self.fail(Error::Custom(
                                    "indefinite byte string contains a non-byte chunk".into(),
                                )));
                            }
                            let len = match arg {
                                HeadArg::Value(len) => len,
                                _ => {
                                    return Err(self
                                        .fail(Error::Custom("invalid byte string length".into())))
                                }
                            };
                            out.extend_from_slice(
                                self.take(len as usize).map_err(|error| self.fail(error))?,
                            );
                        }
                        Ok(Cow::Owned(out))
                    }
                    _ => unreachable!("byte string head cannot be a float"),
                }
            }
            3 => match self.string()? {
                Cow::Borrowed(text) => Ok(Cow::Borrowed(text.as_bytes())),
                Cow::Owned(text) => Ok(Cow::Owned(text.into_bytes())),
            },
            4 => {
                self.begin_array()?;
                let mut out = Vec::new();
                while self.array_has_more()? {
                    out.push(self.u8()?);
                    if !self.array_entry_sep()? {
                        break;
                    }
                }
                self.end_array()?;
                Ok(Cow::Owned(out))
            }
            _ => Err(self.fail(Error::Custom(
                "expected a byte string or an array of bytes".into(),
            ))),
        }
    }

    fn option_tag(&mut self) -> NextjsonResult<OptionTag> {
        // Direct wire probe so `Option<Bytes>` keeps working even though the
        // token model rejects raw byte strings.
        if self.lookahead.is_none() && self.peek_byte().map_err(|error| self.fail(error))? == 0xf6 {
            self.cursor += 1;
            return Ok(OptionTag::None);
        }
        if matches!(self.lookahead, Some(Token::Null)) {
            self.lookahead = None;
            return Ok(OptionTag::None);
        }
        Ok(OptionTag::Some)
    }

    fn skip_value(&mut self) -> NextjsonResult<()> {
        self.skip_value_inner()
    }

    fn peek_token(&mut self) -> NextjsonResult<Token<'de>> {
        self.peek_token_inner()
    }

    fn next_token(&mut self) -> NextjsonResult<Token<'de>> {
        self.take_token()
    }

    fn save(&self) -> Mark {
        Mark::new(self.cursor, self.depth as u32)
    }

    fn restore(&mut self, mark: Mark) {
        let depth = (mark.depth() as usize).min(self.counts.len());
        self.cursor = mark.pos();
        self.depth = depth;
        self.frames.truncate(depth);
        for slot in &mut self.counts[depth..] {
            *slot = 0;
        }
        self.lookahead = None;
        self.wire_error = None;
    }

    fn set_expecting(&mut self, expecting: &'static str) -> Option<&'static str> {
        self.expecting.replace(expecting)
    }

    fn is_human_readable(&self) -> bool {
        false
    }
}

impl<'de> CborDecoder<'de> {
    /// Shared container-close logic for `end_array` / `end_object`.
    ///
    /// Definite containers must have zero remaining elements (the caller
    /// drained them through the separators); indefinite containers consume the
    /// `0xff` break unless a pending lookahead already consumed it.
    fn end_container(&mut self, kind: ContainerKind) -> NextjsonResult<()> {
        match self.lookahead.take() {
            Some(Token::EndArray) | Some(Token::EndObject) => {}
            Some(Token::BeginArray) | Some(Token::BeginObject) => {}
            Some(_) => {
                return Err(self.fail(Error::Custom("container ended with unread elements".into())))
            }
            None => {
                let frame = match self.frames.last().copied() {
                    Some(frame) => frame,
                    None => {
                        return Err(
                            self.fail(Error::Custom("container end without matching start".into()))
                        )
                    }
                };
                if frame.definite.is_none() {
                    let byte = self.peek_byte().map_err(|error| self.fail(error))?;
                    if byte != 0xff {
                        return Err(
                            self.fail(Error::Custom("container ended with unread elements".into()))
                        );
                    }
                    self.cursor += 1;
                }
            }
        }
        let frame = match self.frames.last().copied() {
            Some(frame) => frame,
            None => {
                return Err(self.fail(Error::Custom("container end without matching start".into())))
            }
        };
        if let Some(remaining) = frame.definite {
            if remaining != 0 {
                return Err(self.fail(Error::Custom("container ended with unread elements".into())));
            }
        }
        self.pop_frame(kind)
    }
}

fn token_name(token: &Token<'_>) -> &'static str {
    match token {
        Token::Null => "null",
        Token::Bool(_) => "bool",
        Token::Number(_) => "number",
        Token::Str(_) => "string",
        Token::BeginObject => "object",
        Token::EndObject => "end of object",
        Token::BeginArray => "array",
        Token::EndArray => "end of array",
    }
}

/// Converts an IEEE 754 half-precision value to `f64` (no `std` needed).
fn half_to_f64(half: u16) -> f64 {
    let sign = if half & 0x8000 != 0 { -1.0 } else { 1.0 };
    let exponent = ((half >> 10) & 0x1f) as i32;
    let fraction = (half & 0x03ff) as u32;
    match exponent {
        0 => {
            if fraction == 0 {
                sign * 0.0
            } else {
                // Subnormal: value = fraction * 2^-24.
                sign * (fraction as f64) * 2f64.powi(-24)
            }
        }
        0x1f => {
            if fraction == 0 {
                sign * f64::INFINITY
            } else {
                f64::NAN
            }
        }
        _ => sign * (fraction as f64 + 1024.0) * 2f64.powi(exponent - 25),
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use alloc::collections::BTreeMap;
    use alloc::string::{String, ToString};
    use alloc::vec;
    use nextjson::NsonDeserialize;

    fn encode<T: nextjson::NsonSerialize + ?Sized>(value: &T) -> Vec<u8> {
        let mut out = Vec::new();
        CborEncoder::new(&mut out, Config::standard())
            .finish(value)
            .expect("encode");
        out
    }

    fn decode<'de, T: nextjson::NsonDeserialize<'de>>(bytes: &'de [u8]) -> T {
        CborDecoder::new(bytes, Config::standard())
            .decode()
            .expect("decode")
    }

    fn decode_err(bytes: &[u8]) -> Error {
        CborDecoder::new(bytes, Config::standard())
            .decode::<nextjson::Value>()
            .expect_err("expected decode failure")
    }

    #[test]
    fn encodes_rfc_8949_vectors() {
        assert_eq!(encode(&0u8), [0x00]);
        assert_eq!(encode(&23u8), [0x17]);
        assert_eq!(encode(&24u8), [0x18, 0x18]);
        assert_eq!(encode(&255u8), [0x18, 0xff]);
        assert_eq!(encode(&256u16), [0x19, 0x01, 0x00]);
        assert_eq!(encode(&65536u32), [0x1a, 0x00, 0x01, 0x00, 0x00]);
        assert_eq!(
            encode(&u64::MAX),
            [0x1b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]
        );
        assert_eq!(encode(&-1i64), [0x20]);
        assert_eq!(encode(&-24i64), [0x37]);
        assert_eq!(encode(&-25i64), [0x38, 0x18]);
        assert_eq!(encode(&-256i64), [0x38, 0xff]);
        assert_eq!(encode(&-257i64), [0x39, 0x01, 0x00]);
        assert_eq!(encode(&true), [0xf5]);
        assert_eq!(encode(&false), [0xf4]);
        assert_eq!(encode(&Option::<u8>::None), [0xf6]);
        assert_eq!(encode(&Option::Some(5u8)), [0x05]);
        assert_eq!(encode("a"), [0x61, b'a']);
        assert_eq!(encode("IETF"), [0x64, b'I', b'E', b'T', b'F']);
        assert_eq!(encode(&vec![1u8, 2, 3]), [0x9f, 0x01, 0x02, 0x03, 0xff]);
        assert_eq!(
            encode(&1.0f64),
            [0xfb, 0x3f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
        );
        assert_eq!(encode(&1.0f32), [0xfa, 0x3f, 0x80, 0x00, 0x00]);
        // Bignum: u128 beyond u64 uses tag 2.
        let big = encode(&u128::MAX);
        assert_eq!(big[0], 0xc2);
        // The magnitude byte string holds 16 bytes (0x50 = major 2, ai 16).
        assert_eq!(big[1], 0x50);
        assert_eq!(&big[2..], &[0xff; 16]);
        // Native byte strings use major 2.
        let bytes = encode(&nextjson::Bytes(b"xyz"));
        assert_eq!(bytes, [0x43, b'x', b'y', b'z']);
    }

    #[test]
    fn decodes_definite_and_indefinite_containers() {
        // Definite array of three.
        assert_eq!(decode::<Vec<u8>>(&[0x83, 0x01, 0x02, 0x03]), vec![1, 2, 3]);
        // Indefinite array.
        assert_eq!(
            decode::<Vec<u8>>(&[0x9f, 0x01, 0x02, 0x03, 0xff]),
            vec![1, 2, 3]
        );
        // Definite map.
        let map: BTreeMap<String, u8> = decode(&[0xa2, 0x61, b'a', 0x01, 0x61, b'b', 0x02]);
        assert_eq!(
            map,
            BTreeMap::from([("a".to_string(), 1), ("b".to_string(), 2)])
        );
        // Indefinite map.
        let map: BTreeMap<String, u8> = decode(&[0xbf, 0x61, b'a', 0x01, 0x61, b'b', 0x02, 0xff]);
        assert_eq!(
            map,
            BTreeMap::from([("a".to_string(), 1), ("b".to_string(), 2)])
        );
        // Indefinite text string (chunked).
        assert_eq!(
            decode::<String>(&[0x7f, 0x62, b'a', b'b', 0x61, b'c', 0xff]),
            "abc"
        );
        // Nested definite-in-indefinite.
        let nested: Vec<Vec<u8>> = decode(&[0x9f, 0x82, 0x01, 0x02, 0x81, 0x03, 0xff]);
        assert_eq!(nested, vec![vec![1, 2], vec![3]]);
    }

    #[test]
    fn roundtrips_values_across_formats() {
        let data = (
            7u64,
            "hello",
            vec![1i32, -2, 3],
            Some("x".to_string()),
            Option::<u8>::None,
            -42i128,
            true,
        );
        let encoded = encode(&data);
        let decoded: (
            u64,
            String,
            Vec<i32>,
            Option<String>,
            Option<u8>,
            i128,
            bool,
        ) = decode(&encoded);
        assert_eq!(
            decoded,
            (
                7,
                "hello".to_string(),
                vec![1, -2, 3],
                Some("x".to_string()),
                None,
                -42,
                true
            )
        );
    }

    #[test]
    fn decodes_half_precision_and_ignores_annotating_tags() {
        assert_eq!(decode::<f64>(&[0xf9, 0x3c, 0x00]), 1.0);
        assert_eq!(decode::<f64>(&[0xf9, 0xc0, 0x00]), -2.0);
        assert_eq!(decode::<f64>(&[0xf9, 0x7b, 0xff]), 65504.0);
        assert_eq!(decode::<f64>(&[0xf9, 0x00, 0x01]), 5.960464477539063e-8);
        // Tag 1 (epoch) annotates a number; the payload is decoded as-is.
        assert_eq!(
            decode::<u64>(&[0xc1, 0x1a, 0x51, 0x4b, 0xb6, 0x70]),
            1363916400
        );
        // Tag 0 (date-time) annotates a 20-byte text string.
        assert_eq!(
            decode::<String>(&[
                0xc0, 0x74, b'2', b'0', b'1', b'3', b'-', b'0', b'3', b'-', b'2', b'1', b'T', b'2',
                b'0', b':', b'0', b'4', b':', b'0', b'0', b'Z'
            ]),
            "2013-03-21T20:04:00Z"
        );
    }

    #[test]
    fn bignums_roundtrip_beyond_64_bits() {
        for value in [
            u128::MAX,
            u128::from(u64::MAX) + 1,
            0x1234_5678_9abc_def0_1122_3344_5566_7788,
        ] {
            let encoded = encode(&value);
            assert_eq!(decode::<u128>(&encoded), value);
        }
        for value in [
            i128::MIN,
            i128::from(i64::MIN) - 1,
            -0x1234_5678_9abc_def0_1122_3344_5566_7789i128,
        ] {
            let encoded = encode(&value);
            assert_eq!(decode::<i128>(&encoded), value);
        }
        // A 16-byte magnitude round-trips exactly.
        let encoded = encode(&0x8000_0000_0000_0000u128);
        assert_eq!(decode::<u128>(&encoded), 0x8000_0000_0000_0000);
    }

    #[test]
    fn bytes_reads_native_and_array_spellings() {
        let encoded = encode(&nextjson::Bytes(b"abc"));
        let out: nextjson::Bytes<'_> = decode(&encoded);
        assert_eq!(out.as_bytes(), b"abc");
        // Array-of-u8 spelling is also accepted by `bytes()`.
        assert_eq!(decode::<Vec<u8>>(&[0x83, 0x01, 0x02, 0x03]), vec![1, 2, 3]);
    }

    #[test]
    fn cross_checks_against_nextjson_relay() {
        use nextjson::formats::{Cbor, Format};
        // Our encoder -> nextjson relay decoder.
        let ours = encode(&("value", 42u64, vec![1u8, 2, 3]));
        let decoded: (String, u64, Vec<u8>) = Cbor.decode(&ours).expect("nextjson decodes ours");
        assert_eq!(decoded, ("value".to_string(), 42, vec![1, 2, 3]));
        // nextjson relay encoder -> our decoder (indefinite forms).
        let theirs = Cbor
            .encode(&("value", 42u64, vec![1u8, 2, 3]))
            .expect("nextjson encodes");
        let decoded: (String, u64, Vec<u8>) = decode(&theirs);
        assert_eq!(decoded, ("value".to_string(), 42, vec![1, 2, 3]));
    }

    #[test]
    fn rejects_malformed_and_truncated_input() {
        // Truncated definite array.
        assert!(matches!(
            decode_err(&[0x83, 0x01, 0x02]),
            Error::UnexpectedEnd
        ));
        // Invalid UTF-8 text.
        assert!(matches!(decode_err(&[0x62, 0xff, 0xfe]), Error::Custom(_)));
        // Break outside any container.
        assert!(matches!(decode_err(&[0xff]), Error::Custom(_)));
        // Reserved additional information.
        assert!(matches!(decode_err(&[0x1e]), Error::Custom(_)));
        // Byte string in the value model position.
        assert!(matches!(decode_err(&[0x41, 0x01]), Error::Custom(_)));
        // Undefined simple value.
        assert!(matches!(decode_err(&[0xf7]), Error::Custom(_)));
        // Simple value 0xf8 0x20.
        assert!(matches!(decode_err(&[0xf8, 0x20]), Error::Custom(_)));
        // Definite container ended early by the caller's type.
        assert!(matches!(decode_err(&[0x82, 0x01]), Error::UnexpectedEnd));
        // Bignum wider than 128 bits.
        let mut wide = vec![0xc2, 0x59, 0x00, 0x11];
        wide.extend_from_slice(&[0u8; 17]);
        assert!(matches!(decode_err(&wide), Error::Custom(_)));
    }

    #[test]
    fn enforces_byte_collection_and_depth_limits() {
        // Byte limit on decode.
        let err = CborDecoder::new(
            &[0x1b, 0, 0, 0, 0, 0, 0, 0, 0],
            Config::standard().with_limit(4),
        )
        .decode::<u64>()
        .expect_err("byte limit");
        assert!(matches!(err, Error::SizeLimit { .. }));
        // Collection limit on a definite container.
        let err = CborDecoder::new(&[0x98, 0x64], Config::standard().with_collection_limit(3))
            .decode::<Vec<u8>>()
            .expect_err("collection limit");
        assert!(matches!(err, Error::CollectionLimit { limit: 3 }));
        // Depth limit.
        let mut deep = Vec::new();
        #[allow(clippy::same_item_push)]
        for _ in 0..(crate::tags::MAX_DEPTH + 2) {
            deep.push(0x81);
        }
        deep.push(0x00);
        #[allow(clippy::same_item_push)]
        for _ in 0..(crate::tags::MAX_DEPTH + 2) {
            deep.push(0xff);
        }
        let err = CborDecoder::new(&deep, Config::standard())
            .decode::<Vec<u8>>()
            .expect_err("depth limit");
        assert!(matches!(err, Error::Custom(_)));
        // Byte limit on encode.
        let err = CborEncoder::new(&mut Vec::new(), Config::standard().with_limit(2))
            .finish(&vec![1u8, 2, 3])
            .expect_err("encode byte limit");
        assert!(matches!(err, Error::SizeLimit { limit: 2 }));
    }

    #[test]
    fn restore_supports_untagged_backtracking() {
        let bytes = encode(&("abc", 9u64));
        let mut decoder = CborDecoder::new(&bytes, Config::standard());
        // Enter the tuple's array, then fail one variant before restoring.
        decoder.begin_array().expect("array");
        let mark = decoder.save();
        assert!(u64::nextdecode(&mut decoder).is_err());
        decoder.restore(mark);
        let first: String = String::nextdecode(&mut decoder).expect("retry");
        assert_eq!(first, "abc");
        assert!(decoder.array_entry_sep().expect("sep"));
        let second: u64 = u64::nextdecode(&mut decoder).expect("second value");
        assert_eq!(second, 9);
        assert!(!decoder.array_entry_sep().expect("last sep"));
        decoder.end_array().expect("end");
    }
}