ayaml 1.0.0

A safe, pure-Rust YAML parser, event stream, value model, Serde layer, and emitter
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
//! Structured errors with a stable kind, a stable machine-readable code, and, when the source
//! provides one, a span.
//!
//! [`ErrorKind`] groups errors into broad pipeline categories (scanner, parser, limits, Serde,
//! emitter, and so on). [`ErrorCode`] identifies the specific failure within those categories
//! using a stable `yaml.<kebab-name>` string (returned by [`ErrorCode::as_str`]). Match on
//! [`Error::code`] or [`Error::kind`] rather than message text; in `libyaml-compat` mode, a few
//! failures remap [`Error::kind`] to match libyaml (for example, depth limits report
//! [`ErrorKind::Serde`]) while keeping their specific [`ErrorCode`] (such as
//! [`ErrorCode::DepthLimit`]).
//!
//! # Masking sensitive documents
//!
//! Normal [`Display`](fmt::Display) and [`Debug`](fmt::Debug) output on [`Error`] (as well as
//! [`Error::message`] and [`std::error::Error::source`]) may include text from the YAML document,
//! such as scalar values, mapping keys and typed error paths, tags, directives, anchor or alias
//! names, or underlying I/O error text. When logging or exposing errors from untrusted or
//! sensitive inputs, format [`Error::masked`] ([`MaskedError`]) instead: its `Display` and
//! `Debug` forms report only the kind, code, and source location (plus byte span and numeric
//! resource-limit metadata in `Debug`) and never include document text or source error strings.

use std::error::Error as StdError;
use std::fmt;
use std::sync::Arc;

use crate::limits::LimitKind;
use crate::span::{Location, Span};

/// The longest path a message holds. A longer one keeps its deepest steps, after [`ELIDED`].
const MAX_PATH_BYTES: usize = 1024;
/// The most characters of one key a path shows.
const MAX_KEY_CHARS: usize = 64;
/// What starts a path that lost its outer steps.
const ELIDED: &str = "…";

/// Stable coarse error categories.
///
/// Use [`ErrorKind`] when handling failures by pipeline stage, and [`ErrorCode`] (via
/// [`Error::code`]) when matching a specific failure reason. Multiple [`ErrorCode`] variants can
/// share one [`ErrorKind`], and in `libyaml-compat` mode a few failures remap [`ErrorKind`] to
/// match libyaml while preserving their specific [`ErrorCode`].
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum ErrorKind {
    /// Input is not valid UTF-8 or uses an unsupported encoding.
    InvalidEncoding,
    /// The scanner could not produce a token.
    Scanner,
    /// The parser could not build an event.
    Parser,
    /// A mapping contains a duplicate semantic key.
    DuplicateKey,
    /// A tag is malformed or unsupported.
    Tag,
    /// A scalar could not be resolved under the active profile.
    Resolution,
    /// A configured resource limit was exceeded.
    Limit,
    /// Composition or value-model construction failed.
    Composition,
    /// Serde deserialization or serialization failed.
    Serde,
    /// The emitter failed.
    Emitter,
    /// I/O failed.
    Io,
}

impl fmt::Display for ErrorKind {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        let name = match self {
            Self::InvalidEncoding => "invalid encoding",
            Self::Scanner => "scanner error",
            Self::Parser => "parser error",
            Self::DuplicateKey => "duplicate key",
            Self::Tag => "tag error",
            Self::Resolution => "scalar resolution error",
            Self::Limit => "resource limit",
            Self::Composition => "composition error",
            Self::Serde => "serde error",
            Self::Emitter => "emitter error",
            Self::Io => "I/O error",
        };
        formatter.write_str(name)
    }
}

/// Stable, machine-readable failure codes in `yaml.<kebab-name>` form.
///
/// Every error returned by `ayaml` carries an [`ErrorCode`] accessible through [`Error::code`],
/// alongside its broader [`ErrorKind`] from [`Error::kind`]. Match on [`ErrorCode`] rather than
/// message text. See `docs/error-codes.md` for the full reference table.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum ErrorCode {
    // -- Encoding --
    /// Input bytes are not valid UTF-8 (`yaml.invalid-utf8`).
    InvalidUtf8,

    // -- Scanner --
    /// Disallowed non-printable character or invalid character boundary (`yaml.non-printable-char`).
    NonPrintableChar,
    /// Byte order mark in a position where YAML forbids one (`yaml.invalid-bom`).
    InvalidBom,
    /// Reserved indicator (`%`, `@`, or `` ` ``) starting a plain scalar
    /// (`yaml.reserved-indicator`).
    ReservedIndicator,
    /// Tab used where YAML forbids tabs as indentation or structural separation
    /// (`yaml.invalid-tab-indentation`).
    InvalidTabIndentation,
    /// Single- or double-quoted scalar was not closed (`yaml.unterminated-quoted-scalar`).
    UnterminatedQuotedScalar,
    /// Quoted scalar continuation line violates indentation or contains a document marker
    /// (`yaml.invalid-quoted-continuation`).
    InvalidQuotedContinuation,
    /// Malformed or unknown escape sequence in a double-quoted scalar (`yaml.invalid-escape`).
    InvalidEscape,
    /// Malformed block scalar header (`yaml.invalid-block-scalar-header`).
    InvalidBlockScalarHeader,
    /// Block scalar indentation violates its parent or leading-empty-line rules
    /// (`yaml.invalid-block-scalar-indentation`).
    InvalidBlockScalarIndentation,
    /// Flow collection continuation is not indented past its enclosing block collection
    /// (`yaml.invalid-flow-indentation`).
    InvalidFlowIndentation,
    /// Document marker (`---` or `...`) inside a flow collection (`yaml.document-marker-in-flow`).
    DocumentMarkerInFlow,
    /// Unseparated `?` before a flow indicator (`yaml.invalid-flow-key`).
    InvalidFlowKey,
    /// `#` comment not separated by whitespace from the preceding token
    /// (`yaml.missing-comment-whitespace`).
    MissingCommentWhitespace,
    /// Malformed `%YAML`, `%TAG`, or unknown directive syntax (`yaml.invalid-directive`).
    InvalidDirective,
    /// Duplicate `%YAML` directive or duplicate `%TAG` handle in one document
    /// (`yaml.duplicate-directive`).
    DuplicateDirective,
    /// Empty or improperly terminated anchor or alias name (`yaml.invalid-anchor`).
    InvalidAnchor,
    /// Malformed tag syntax, verbatim URI, or percent-escape (`yaml.invalid-tag`).
    InvalidTag,
    /// Tag uses a handle not declared by `%TAG` (`yaml.undefined-tag-handle`).
    UndefinedTagHandle,

    // -- Parser --
    /// Directive not followed by a document start marker (`yaml.directive-without-document`).
    DirectiveWithoutDocument,
    /// Invalid node or block collection on a `---` document start line
    /// (`yaml.invalid-document-start`).
    InvalidDocumentStart,
    /// Unexpected non-comment tokens after a `...` document end marker
    /// (`yaml.content-after-document-end`).
    ContentAfterDocumentEnd,
    /// Unexpected content after the document root (`yaml.trailing-content`).
    TrailingContent,
    /// Block sequence or mapping entry is misindented relative to its parent
    /// (`yaml.invalid-block-indentation`).
    InvalidBlockIndentation,
    /// Mapping entry line is missing a `:` separator (`yaml.missing-mapping-colon`).
    MissingMappingColon,
    /// Single-document reader encountered another document (`yaml.trailing-documents`).
    TrailingDocuments,
    /// Unexpected token in block or flow grammar (`yaml.unexpected-token`).
    UnexpectedToken,
    /// Unexpected token or mapping value in a mapping value position
    /// (`yaml.unexpected-mapping-value`).
    UnexpectedMappingValue,
    /// Flow sequence or flow mapping reached end of input without a closing bracket
    /// (`yaml.unterminated-flow-collection`).
    UnterminatedFlowCollection,
    /// Missing separator, unexpected bracket, or empty entry in a flow collection
    /// (`yaml.invalid-flow-collection`).
    InvalidFlowCollection,
    /// Node carries more than one anchor or more than one tag (`yaml.duplicate-node-properties`).
    DuplicateNodeProperties,
    /// Node properties placed before a compact block sequence or explicit key
    /// (`yaml.misplaced-node-properties`).
    MisplacedNodeProperties,
    /// Implicit mapping key spans multiple lines or exceeds 1,024 characters
    /// (`yaml.invalid-implicit-key`).
    InvalidImplicitKey,
    /// Alias node carries a tag or anchor (`yaml.alias-with-properties`).
    AliasWithProperties,

    // -- Composition, aliases, and duplicate keys --
    /// Alias refers to an anchor not defined earlier in the document (`yaml.unknown-anchor`).
    UnknownAnchor,
    /// Alias refers to an enclosing collection that is still open (`yaml.cyclic-alias`).
    CyclicAlias,
    /// Mapping contains two semantically equal keys (`yaml.duplicate-key`).
    DuplicateKey,
    /// Merge key (`<<`) operand is neither a mapping nor a sequence of mappings
    /// (`yaml.invalid-merge-operand`).
    InvalidMergeOperand,
    /// Structural mismatch in an event stream during composition (`yaml.malformed-event-stream`).
    MalformedEventStream,

    // -- Scalar resolution --
    /// Scalar does not match the YAML 1.2 Core Schema `!!int` rule (`yaml.invalid-int-scalar`).
    InvalidIntScalar,
    /// Scalar does not match the YAML 1.2 Core Schema `!!float` rule (`yaml.invalid-float-scalar`).
    InvalidFloatScalar,
    /// Scalar does not match the YAML 1.2 Core Schema `!!bool` rule (`yaml.invalid-bool-scalar`).
    InvalidBoolScalar,
    /// Scalar does not match the YAML 1.2 Core Schema `!!null` rule (`yaml.invalid-null-scalar`).
    InvalidNullScalar,
    /// Generic scalar resolution failure (`yaml.invalid-scalar`).
    InvalidScalar,

    // -- Resource limits --
    /// Generic resource limit exceeded (`yaml.limit-exceeded`).
    LimitExceeded,
    /// Invalid [`Limits`](crate::Limits) configuration (`yaml.invalid-limits`).
    InvalidLimits,
    /// Input byte length exceeded `max_input_bytes` (`yaml.input-size-limit`).
    InputSizeLimit,
    /// Nesting depth exceeded `max_depth` (`yaml.depth-limit`).
    DepthLimit,
    /// Document count exceeded `max_documents` (`yaml.document-count-limit`).
    DocumentCountLimit,
    /// Scalar size exceeded `max_scalar_bytes` (`yaml.scalar-size-limit`).
    ScalarSizeLimit,
    /// Collection entry count exceeded `max_collection_entries`
    /// (`yaml.collection-entries-limit`).
    CollectionEntriesLimit,
    /// Event or token count exceeded `max_events` (`yaml.event-count-limit`).
    EventCountLimit,
    /// Anchor count exceeded `max_anchors` (`yaml.anchor-count-limit`).
    AnchorCountLimit,
    /// Alias dereference count exceeded `max_alias_dereferences`
    /// (`yaml.alias-dereference-limit`).
    AliasDereferenceLimit,
    /// Alias expansion budget exceeded `max_alias_expansion_bytes` (`yaml.alias-expansion-limit`).
    AliasExpansionLimit,
    /// Emitted byte count exceeded `max_output_bytes` (`yaml.output-size-limit`).
    OutputSizeLimit,
    /// Expanded `%TAG` bytes exceeded `max_tag_expansion_bytes` (`yaml.tag-expansion-limit`).
    TagExpansionLimit,

    // -- Serde --
    /// Serde type mismatch (`yaml.invalid-type`).
    InvalidType,
    /// Serde value mismatch (`yaml.invalid-value`).
    InvalidValue,
    /// Serde sequence or tuple length mismatch (`yaml.invalid-length`).
    InvalidLength,
    /// Unknown enum variant name (`yaml.unknown-variant`).
    UnknownVariant,
    /// Unknown struct field name (`yaml.unknown-field`).
    UnknownField,
    /// Required struct field missing (`yaml.missing-field`).
    MissingField,
    /// Duplicate struct field (`yaml.duplicate-field`).
    DuplicateField,
    /// YAML node cannot be decoded as the requested enum representation
    /// (`yaml.invalid-enum-representation`).
    InvalidEnumRepresentation,
    /// Serde map visitor or serializer handled a value before its key (`yaml.map-value-before-key`).
    MapValueBeforeKey,
    /// Custom Serde deserialization or serialization error (`yaml.serde-custom`).
    SerdeCustom,

    // -- Emitter and I/O --
    /// Invalid emitter option such as out-of-range indentation (`yaml.invalid-emitter-options`).
    InvalidEmitterOptions,
    /// Tag cannot be emitted or does not match its scalar's core-schema rule
    /// (`yaml.unsupported-tag`).
    UnsupportedTag,
    /// Event stream handed to an event writer is malformed or has a dangling alias or invalid
    /// directive (`yaml.invalid-emit-event`).
    InvalidEmitEvent,
    /// Underlying `std::io::Read` or `std::io::Write` returned an I/O error (`yaml.io`).
    Io,
}

impl ErrorCode {
    /// Stable `yaml.<kebab-name>` identifier for this error code.
    #[must_use]
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::InvalidUtf8 => "yaml.invalid-utf8",
            Self::NonPrintableChar => "yaml.non-printable-char",
            Self::InvalidBom => "yaml.invalid-bom",
            Self::ReservedIndicator => "yaml.reserved-indicator",
            Self::InvalidTabIndentation => "yaml.invalid-tab-indentation",
            Self::UnterminatedQuotedScalar => "yaml.unterminated-quoted-scalar",
            Self::InvalidQuotedContinuation => "yaml.invalid-quoted-continuation",
            Self::InvalidEscape => "yaml.invalid-escape",
            Self::InvalidBlockScalarHeader => "yaml.invalid-block-scalar-header",
            Self::InvalidBlockScalarIndentation => "yaml.invalid-block-scalar-indentation",
            Self::InvalidFlowIndentation => "yaml.invalid-flow-indentation",
            Self::DocumentMarkerInFlow => "yaml.document-marker-in-flow",
            Self::InvalidFlowKey => "yaml.invalid-flow-key",
            Self::MissingCommentWhitespace => "yaml.missing-comment-whitespace",
            Self::InvalidDirective => "yaml.invalid-directive",
            Self::DuplicateDirective => "yaml.duplicate-directive",
            Self::InvalidAnchor => "yaml.invalid-anchor",
            Self::InvalidTag => "yaml.invalid-tag",
            Self::UndefinedTagHandle => "yaml.undefined-tag-handle",
            Self::DirectiveWithoutDocument => "yaml.directive-without-document",
            Self::InvalidDocumentStart => "yaml.invalid-document-start",
            Self::ContentAfterDocumentEnd => "yaml.content-after-document-end",
            Self::TrailingContent => "yaml.trailing-content",
            Self::InvalidBlockIndentation => "yaml.invalid-block-indentation",
            Self::MissingMappingColon => "yaml.missing-mapping-colon",
            Self::TrailingDocuments => "yaml.trailing-documents",
            Self::UnexpectedToken => "yaml.unexpected-token",
            Self::UnexpectedMappingValue => "yaml.unexpected-mapping-value",
            Self::UnterminatedFlowCollection => "yaml.unterminated-flow-collection",
            Self::InvalidFlowCollection => "yaml.invalid-flow-collection",
            Self::DuplicateNodeProperties => "yaml.duplicate-node-properties",
            Self::MisplacedNodeProperties => "yaml.misplaced-node-properties",
            Self::InvalidImplicitKey => "yaml.invalid-implicit-key",
            Self::AliasWithProperties => "yaml.alias-with-properties",
            Self::UnknownAnchor => "yaml.unknown-anchor",
            Self::CyclicAlias => "yaml.cyclic-alias",
            Self::DuplicateKey => "yaml.duplicate-key",
            Self::InvalidMergeOperand => "yaml.invalid-merge-operand",
            Self::MalformedEventStream => "yaml.malformed-event-stream",
            Self::InvalidIntScalar => "yaml.invalid-int-scalar",
            Self::InvalidFloatScalar => "yaml.invalid-float-scalar",
            Self::InvalidBoolScalar => "yaml.invalid-bool-scalar",
            Self::InvalidNullScalar => "yaml.invalid-null-scalar",
            Self::InvalidScalar => "yaml.invalid-scalar",
            Self::LimitExceeded => "yaml.limit-exceeded",
            Self::InvalidLimits => "yaml.invalid-limits",
            Self::InputSizeLimit => "yaml.input-size-limit",
            Self::DepthLimit => "yaml.depth-limit",
            Self::DocumentCountLimit => "yaml.document-count-limit",
            Self::ScalarSizeLimit => "yaml.scalar-size-limit",
            Self::CollectionEntriesLimit => "yaml.collection-entries-limit",
            Self::EventCountLimit => "yaml.event-count-limit",
            Self::AnchorCountLimit => "yaml.anchor-count-limit",
            Self::AliasDereferenceLimit => "yaml.alias-dereference-limit",
            Self::AliasExpansionLimit => "yaml.alias-expansion-limit",
            Self::OutputSizeLimit => "yaml.output-size-limit",
            Self::TagExpansionLimit => "yaml.tag-expansion-limit",
            Self::InvalidType => "yaml.invalid-type",
            Self::InvalidValue => "yaml.invalid-value",
            Self::InvalidLength => "yaml.invalid-length",
            Self::UnknownVariant => "yaml.unknown-variant",
            Self::UnknownField => "yaml.unknown-field",
            Self::MissingField => "yaml.missing-field",
            Self::DuplicateField => "yaml.duplicate-field",
            Self::InvalidEnumRepresentation => "yaml.invalid-enum-representation",
            Self::MapValueBeforeKey => "yaml.map-value-before-key",
            Self::SerdeCustom => "yaml.serde-custom",
            Self::InvalidEmitterOptions => "yaml.invalid-emitter-options",
            Self::UnsupportedTag => "yaml.unsupported-tag",
            Self::InvalidEmitEvent => "yaml.invalid-emit-event",
            Self::Io => "yaml.io",
        }
    }

    /// Default code used when [`Error::new`] constructs an error with only an [`ErrorKind`].
    #[must_use]
    pub(crate) const fn default_for_kind(kind: ErrorKind) -> Self {
        match kind {
            ErrorKind::InvalidEncoding => Self::InvalidUtf8,
            ErrorKind::Scanner | ErrorKind::Parser => Self::UnexpectedToken,
            ErrorKind::DuplicateKey => Self::DuplicateKey,
            ErrorKind::Tag => Self::InvalidTag,
            ErrorKind::Resolution => Self::InvalidScalar,
            ErrorKind::Limit => Self::LimitExceeded,
            ErrorKind::Composition => Self::MalformedEventStream,
            ErrorKind::Serde => Self::SerdeCustom,
            ErrorKind::Emitter => Self::InvalidEmitEvent,
            ErrorKind::Io => Self::Io,
        }
    }

    /// Code corresponding to a structured [`LimitKind`].
    #[must_use]
    pub(crate) const fn from_limit_kind(kind: LimitKind) -> Self {
        match kind {
            LimitKind::InputBytes => Self::InputSizeLimit,
            LimitKind::Depth => Self::DepthLimit,
            LimitKind::Documents => Self::DocumentCountLimit,
            LimitKind::ScalarBytes => Self::ScalarSizeLimit,
            LimitKind::CollectionEntries => Self::CollectionEntriesLimit,
            LimitKind::Events => Self::EventCountLimit,
            LimitKind::Anchors => Self::AnchorCountLimit,
            LimitKind::AliasDereferences => Self::AliasDereferenceLimit,
            LimitKind::AliasExpansion => Self::AliasExpansionLimit,
            LimitKind::OutputBytes => Self::OutputSizeLimit,
            LimitKind::TagExpansion => Self::TagExpansionLimit,
        }
    }
}

impl fmt::Display for ErrorCode {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str(self.as_str())
    }
}

impl AsRef<str> for ErrorCode {
    fn as_ref(&self) -> &str {
        self.as_str()
    }
}

#[derive(Clone)]
struct ErrorInner {
    kind: ErrorKind,
    code: ErrorCode,
    /// Whether a `ReservedIndicator` followed a byte-order mark at line start, for compat wording.
    reserved_after_bom: bool,
    /// How much of `message` is the path to what a typed reader rejected, with the `: ` after it.
    /// A path is at most `MAX_PATH_BYTES` and one step long.
    path_len: u16,
    /// How much of the end of `message` is context that the error's location goes before.
    context_len: u16,
    span: Option<Span>,
    location: Option<Location>,
    message: String,
    limit: Option<crate::limits::LimitExceeded>,
    source: Option<Arc<dyn StdError + Send + Sync + 'static>>,
}

/// An ayaml error with a stable category, a stable failure code, optional source span, and, for
/// resource-limit failures, the structured limit that was exceeded.
///
/// Normal [`Display`](fmt::Display) and [`Debug`](fmt::Debug) formatting may include text from
/// the YAML document (such as scalar values, mapping keys, tags, directives, or anchor names).
/// Use [`Error::masked`] when logging or returning errors from sensitive or untrusted documents.
#[derive(Clone)]
pub struct Error {
    inner: Box<ErrorInner>,
}

impl fmt::Debug for Error {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_struct("Error")
            .field("kind", &self.inner.kind)
            .field("code", &self.inner.code)
            .field("span", &self.inner.span)
            .field("location", &self.inner.location)
            .field("message", &self.inner.message)
            .field("limit", &self.inner.limit)
            .field("source", &self.inner.source)
            .finish()
    }
}

impl PartialEq for Error {
    fn eq(&self, other: &Self) -> bool {
        self.inner.kind == other.inner.kind
            && self.inner.code == other.inner.code
            && self.inner.path_len == other.inner.path_len
            && self.inner.context_len == other.inner.context_len
            && self.inner.span == other.inner.span
            && self.inner.location == other.inner.location
            && self.inner.message == other.inner.message
            && self.inner.limit == other.inner.limit
            && sources_eq(self.inner.source.as_ref(), other.inner.source.as_ref())
    }
}

impl Eq for Error {}

fn sources_eq(
    left: Option<&Arc<dyn StdError + Send + Sync + 'static>>,
    right: Option<&Arc<dyn StdError + Send + Sync + 'static>>,
) -> bool {
    match (left, right) {
        (None, None) => true,
        (Some(a), Some(b)) => Arc::ptr_eq(a, b) || a.to_string() == b.to_string(),
        _ => false,
    }
}

impl Error {
    /// Create an error with no source location and the default [`ErrorCode`] for `kind`.
    #[must_use]
    pub fn new(kind: ErrorKind, message: impl Into<String>) -> Self {
        Self::coded(kind, ErrorCode::default_for_kind(kind), message)
    }

    /// Create an error with an explicit [`ErrorCode`].
    #[cold]
    #[must_use]
    pub(crate) fn coded(kind: ErrorKind, code: ErrorCode, message: impl Into<String>) -> Self {
        Self {
            inner: Box::new(ErrorInner {
                kind,
                code,
                reserved_after_bom: false,
                path_len: 0,
                context_len: 0,
                span: None,
                location: None,
                message: message.into(),
                limit: None,
                source: None,
            }),
        }
    }

    /// Override the stable failure code on this error.
    #[must_use]
    pub fn with_code(mut self, code: ErrorCode) -> Self {
        self.inner.code = code;
        self
    }

    /// The stable machine-readable failure code.
    ///
    /// Prefer matching on [`ErrorCode`] (or [`ErrorCode::as_str`]) over inspecting
    /// [`Error::message`].
    #[must_use]
    pub fn code(&self) -> ErrorCode {
        self.inner.code
    }

    /// Mark that a reserved indicator appeared immediately after a byte order mark.
    #[must_use]
    pub(crate) fn with_reserved_after_bom(mut self) -> Self {
        self.inner.reserved_after_bom = true;
        self
    }

    /// Whether this error is a reserved indicator immediately after a byte order mark.
    #[cfg(feature = "libyaml-compat")]
    #[must_use]
    pub(crate) fn is_reserved_after_bom(&self) -> bool {
        self.inner.reserved_after_bom
    }

    /// Attach a source span.
    ///
    /// A span carries byte offsets. [`location`](Self::location) is filled in when the error
    /// crosses a reader, which has the input's line index; a manually built
    /// error has no location until [`with_location`](Self::with_location) supplies one. This
    /// clears a location that was set before, which was the earlier span's, so set the location
    /// after the span.
    #[must_use]
    pub fn with_span(mut self, span: Span) -> Self {
        self.inner.span = Some(span);
        self.inner.location = None;
        self
    }

    /// Attach the one-based line and column for this error.
    #[must_use]
    pub fn with_location(mut self, location: Location) -> Self {
        self.inner.location = Some(location);
        self
    }

    /// Fill in the line and column from this error's byte span, using `input` directly without
    /// allocating a line index.
    ///
    /// This is the boundary step for an error that was built from a byte span deeper in a
    /// reader. It leaves an error that already has a location, or has no span, unchanged.
    #[cold]
    #[must_use]
    pub(crate) fn with_input_location(mut self, input: &str, compat: bool) -> Self {
        if self.inner.location.is_some() {
            return self;
        }
        let Some(span) = self.inner.span else {
            return self;
        };
        if let Some(position) = crate::position::location_at(input, span.start(), compat) {
            self.inner.location = Some(position);
        }
        self
    }

    /// End the message with `context`, which follows the location when the error is displayed.
    #[cold]
    pub(crate) fn with_context(mut self, context: &str) -> Self {
        self.inner.message.push_str(context);
        // The parser's contexts are short. A longer one would stay before the location.
        self.inner.context_len = u16::try_from(context.len()).unwrap_or(0);
        self
    }

    /// Reword the message body, keeping any path prefix and trailing context intact.
    #[cold]
    pub(crate) fn map_message(mut self, update: impl FnOnce(&str) -> String) -> Self {
        let start = self.inner.path_len as usize;
        let end = self
            .inner
            .message
            .len()
            .saturating_sub(self.inner.context_len as usize);
        if start <= end {
            let replacement = update(&self.inner.message[start..end]);
            self.inner.message.replace_range(start..end, &replacement);
        }
        self
    }

    /// Attach a source error.
    #[must_use]
    pub fn with_source(mut self, source: impl StdError + Send + Sync + 'static) -> Self {
        self.inner.source = Some(Arc::new(source));
        self
    }

    /// Attach the structured limit that was exceeded.
    ///
    /// When the error still holds the generic [`ErrorCode::LimitExceeded`] code (or the code from
    /// an earlier limit attached by this method), this updates the code to match `limit.kind()`.
    #[must_use]
    pub fn with_limit(mut self, limit: crate::limits::LimitExceeded) -> Self {
        if self.inner.code == ErrorCode::LimitExceeded
            || self
                .inner
                .limit
                .is_some_and(|prev| self.inner.code == ErrorCode::from_limit_kind(prev.kind()))
        {
            self.inner.code = ErrorCode::from_limit_kind(limit.kind());
        }
        self.inner.limit = Some(limit);
        self
    }

    /// The structured resource-limit failure, when this is a limit error.
    #[must_use]
    pub fn limit(&self) -> Option<crate::limits::LimitExceeded> {
        self.inner.limit
    }

    /// The stable category.
    #[must_use]
    pub fn kind(&self) -> ErrorKind {
        self.inner.kind
    }

    /// The source span, if any.
    #[must_use]
    pub fn span(&self) -> Option<Span> {
        self.inner.span
    }

    /// The one-based line/column, if the error has a location.
    #[must_use]
    pub fn location(&self) -> Option<Location> {
        self.inner.location
    }

    /// Return a wrapper whose [`Display`](fmt::Display) and [`Debug`](fmt::Debug) output hold no
    /// text from the YAML document.
    ///
    /// Normal [`Display`](fmt::Display) and [`Debug`](fmt::Debug) output on [`Error`] may include
    /// scalar values, mapping keys, typed error paths, tags, directives, anchor or alias names,
    /// or underlying source error text. Use `masked()` when logging, recording telemetry, or
    /// returning diagnostics from sensitive or untrusted documents.
    ///
    /// ```
    /// use std::collections::HashMap;
    ///
    /// let error = ayaml::from_str::<HashMap<String, u32>>("secret_token: hunter2\n").unwrap_err();
    /// assert!(error.to_string().contains("secret_token"));
    /// assert!(error.to_string().contains("hunter2"));
    ///
    /// let masked = error.masked();
    /// assert!(!masked.to_string().contains("secret_token"));
    /// assert!(!masked.to_string().contains("hunter2"));
    /// assert_eq!(
    ///     masked.to_string(),
    ///     "serde error (yaml.invalid-type) at line 1 column 15"
    /// );
    /// ```
    #[must_use]
    pub const fn masked(&self) -> MaskedError<'_> {
        MaskedError(self)
    }

    /// The human-readable message without location decoration.
    ///
    /// This message may include text from the YAML document. When logging or displaying errors
    /// from sensitive or untrusted inputs, use [`Error::masked`] instead.
    ///
    /// A flow collection's error ends with where the collection opened, as in
    /// `unterminated flow sequence, opened at line 1 column 1`. That is part of the message, and
    /// the error's own location goes before it when the error is displayed.
    ///
    /// When a typed reader rejects something below the document's root, the message starts with
    /// the path to it, as in `tasks[0].limits.cpu: invalid type: string "x", expected u32`. A
    /// field is its name, an element is its index in brackets, and a key that isn't a short name
    /// made of ASCII letters, digits, `_` and `-` is quoted in brackets, as in `ports["80"]`. A
    /// key that is a collection is `[?]`. An enum's content adds the variant's name. A long key,
    /// or a long path, is cut short with `…`.
    #[must_use]
    pub fn message(&self) -> &str {
        &self.inner.message
    }

    /// Say that this error came from inside element `index` of a sequence.
    #[cold]
    pub(crate) fn inside_index(self, index: usize) -> Self {
        self.inside(&format!("[{index}]"))
    }

    /// Say that this error came from inside the value of the mapping key with this text.
    #[cold]
    pub(crate) fn inside_key(self, key: &str) -> Self {
        if is_short_name(key) {
            self.inside(key)
        } else {
            self.inside(&quoted_key(key))
        }
    }

    /// Say that this error came from inside the value of a key that is a collection.
    #[cold]
    pub(crate) fn inside_collection_key(self) -> Self {
        self.inside("[?]")
    }

    /// Put one step in front of the path. Errors travel outwards, so the path grows from its
    /// deepest step, and only the step that reads a value adds to it.
    fn inside(mut self, step: &str) -> Self {
        prepend_path_step(&mut self.inner.message, &mut self.inner.path_len, step);
        self
    }
}

/// Prepend one path step onto `message` and update `path_len`, eliding once past `MAX_PATH_BYTES`.
fn prepend_path_step(message: &mut String, path_len: &mut u16, step: &str) {
    let current_len = *path_len as usize;
    if current_len == 0 {
        message.insert_str(0, ": ");
        message.insert_str(0, step);
        *path_len = path_bytes(step.len().saturating_add(2));
        return;
    }
    if message.starts_with(ELIDED) {
        return;
    }
    let (chosen, separator, next_len) =
        resolve_path_step(current_len, message.starts_with('['), step);
    message.insert_str(0, separator);
    message.insert_str(0, chosen);
    *path_len = next_len;
}

/// Decide the step text, separator, and updated saturating path byte length for one outer path step.
fn resolve_path_step(
    path_len: usize,
    starts_with_bracket: bool,
    step: &str,
) -> (&str, &'static str, u16) {
    let separator = if starts_with_bracket { "" } else { "." };
    let candidate = path_len
        .saturating_add(step.len())
        .saturating_add(separator.len());
    let chosen = if candidate > MAX_PATH_BYTES {
        ELIDED
    } else {
        step
    };
    let next_len = path_bytes(
        path_len
            .saturating_add(chosen.len())
            .saturating_add(separator.len()),
    );
    (chosen, separator, next_len)
}

/// A path's length as kept. One past `MAX_PATH_BYTES` has the same effect as any more: the next
/// step is elided.
fn path_bytes(len: usize) -> u16 {
    u16::try_from(len).unwrap_or(u16::MAX)
}

/// Whether a key can stand in a path as it is, with no doubt about where it ends.
fn is_short_name(key: &str) -> bool {
    let mut chars = key.chars();
    key.len() <= MAX_KEY_CHARS
        && chars
            .next()
            .is_some_and(|first| first.is_ascii_alphabetic() || first == '_')
        && chars.all(|next| next.is_ascii_alphanumeric() || next == '_' || next == '-')
}

/// Format `\u{<hex>}` for a Unicode scalar value into a fixed stack buffer (max 10 ASCII bytes).
const fn format_unicode_escape(ch: char) -> ([u8; 10], usize) {
    const HEX: &[u8; 16] = b"0123456789abcdef";
    let mut buf = [0_u8; 10];
    buf[0] = b'\\';
    buf[1] = b'u';
    buf[2] = b'{';
    let mut len = 3;
    let code = ch as u32;
    if code == 0 {
        buf[len] = b'0';
        len += 1;
    } else {
        let mut started = false;
        let mut shift = 20_u32;
        loop {
            let nibble = ((code >> shift) & 0xf) as usize;
            if nibble != 0 || started {
                started = true;
                buf[len] = HEX[nibble];
                len += 1;
            }
            if shift == 0 {
                break;
            }
            shift -= 4;
        }
    }
    buf[len] = b'}';
    len += 1;
    (buf, len)
}

/// Append `\u{<hex>}` for a Unicode scalar value without allocating an intermediate `String`.
fn write_unicode_escape(out: &mut impl fmt::Write, ch: char) {
    let (buf, len) = format_unicode_escape(ch);
    for &byte in &buf[..len] {
        let _ = out.write_char(char::from(byte));
    }
}

/// A key in brackets and quotes, with anything that could change how a terminal shows it escaped.
fn quoted_key(key: &str) -> String {
    let mut quoted = String::with_capacity(key.len().min(MAX_KEY_CHARS * 4) + 8);
    write_quoted_key(&mut quoted, key, MAX_KEY_CHARS);
    quoted
}

/// Append a single character of a quoted error-path key, escaping quotes, backslashes, and hidden
/// characters.
fn write_quoted_key_char(quoted: &mut impl fmt::Write, next: char) {
    match next {
        '"' | '\\' => {
            let _ = quoted.write_char('\\');
            let _ = quoted.write_char(next);
        }
        _ if hides_text(next) => write_unicode_escape(quoted, next),
        _ => {
            let _ = quoted.write_char(next);
        }
    }
}

/// Append `key` in brackets and quotes to `quoted`, keeping at most `max_chars` Unicode scalars.
fn write_quoted_key(quoted: &mut impl fmt::Write, key: &str, max_chars: usize) {
    let _ = quoted.write_str("[\"");
    let mut chars = key.chars();
    for next in chars.by_ref().take(max_chars) {
        write_quoted_key_char(quoted, next);
    }
    if chars.next().is_some() {
        let _ = quoted.write_str(ELIDED);
    }
    let _ = quoted.write_str("\"]");
}

/// Control characters, line separators, the byte order mark, and the marks that reorder text.
fn hides_text(character: char) -> bool {
    character.is_control()
        || matches!(
            character,
            '\u{61c}'
                | '\u{200e}'
                | '\u{200f}'
                | '\u{2028}'
                | '\u{2029}'
                | '\u{202a}'..='\u{202e}'
                | '\u{2066}'..='\u{2069}'
                | '\u{feff}'
        )
}

impl fmt::Display for Error {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        let Some(location) = self.inner.location else {
            return formatter.write_str(&self.inner.message);
        };
        let (problem, context) = self.inner.message.split_at(
            self.inner
                .message
                .len()
                .saturating_sub(self.inner.context_len as usize),
        );
        write!(
            formatter,
            "{problem} at line {} column {}{context}",
            location.line(),
            location.column()
        )
    }
}

impl StdError for Error {
    #[allow(
        trivial_casts,
        reason = "Arc<dyn Error + Send + Sync> is widened to the plain error trait object"
    )]
    fn source(&self) -> Option<&(dyn StdError + 'static)> {
        self.inner
            .source
            .as_deref()
            .map(|source| source as &(dyn StdError + 'static))
    }
}

/// A view of an [`Error`] whose [`Display`](fmt::Display) and [`Debug`](fmt::Debug) forms exclude
/// all document text.
///
/// Returned by [`Error::masked`]. Normal [`Error`] formatting may include values, mapping keys,
/// paths, tags, directives, anchor or alias names, or underlying source error messages from the
/// document. `MaskedError` reports only the coarse [`ErrorKind`], stable [`ErrorCode`], and
/// one-based [`Location`] (plus byte [`Span`] and numeric [`LimitExceeded`](crate::LimitExceeded)
/// metadata in `Debug`), and its [`std::error::Error::source`] always returns `None` so an
/// unmasked cause cannot leak through error-chain formatters.
///
/// Use `MaskedError` whenever errors from untrusted or secret-bearing YAML documents are written
/// to logs, metrics, crash reports, or user-visible responses.
#[derive(Clone, Copy)]
pub struct MaskedError<'a>(&'a Error);

impl MaskedError<'_> {
    /// The stable coarse error category.
    #[must_use]
    pub fn kind(&self) -> ErrorKind {
        self.0.inner.kind
    }

    /// The stable machine-readable failure code.
    #[must_use]
    pub fn code(&self) -> ErrorCode {
        self.0.inner.code
    }

    /// The byte span of the failure, if any.
    #[must_use]
    pub fn span(&self) -> Option<Span> {
        self.0.inner.span
    }

    /// The one-based line and column of the failure, if any.
    #[must_use]
    pub fn location(&self) -> Option<Location> {
        self.0.inner.location
    }

    /// The structured resource-limit metadata, when this is a limit error.
    #[must_use]
    pub fn limit(&self) -> Option<crate::limits::LimitExceeded> {
        self.0.limit()
    }
}

impl PartialEq for MaskedError<'_> {
    fn eq(&self, other: &Self) -> bool {
        self.0.inner.kind == other.0.inner.kind
            && self.0.inner.code == other.0.inner.code
            && self.0.inner.span == other.0.inner.span
            && self.0.inner.location == other.0.inner.location
            && self.0.inner.limit == other.0.inner.limit
    }
}

impl Eq for MaskedError<'_> {}

impl fmt::Display for MaskedError<'_> {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self.0.inner.location {
            Some(location) => write!(
                formatter,
                "{} ({}) at line {} column {}",
                self.0.inner.kind,
                self.0.inner.code,
                location.line(),
                location.column()
            ),
            None => write!(formatter, "{} ({})", self.0.inner.kind, self.0.inner.code),
        }
    }
}

impl fmt::Debug for MaskedError<'_> {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_struct("MaskedError")
            .field("kind", &self.0.inner.kind)
            .field("code", &self.0.inner.code)
            .field("span", &self.0.inner.span)
            .field("location", &self.0.inner.location)
            .field("limit", &self.0.inner.limit)
            .finish()
    }
}

impl StdError for MaskedError<'_> {}

/// Minimal `serde::de::Error` adapter that formats Serde's default error messages verbatim so
/// `Error` can assign specific [`ErrorCode`] variants without altering wording.
#[derive(Debug)]
struct SerdeMessage(String);

impl fmt::Display for SerdeMessage {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str(&self.0)
    }
}

impl StdError for SerdeMessage {}

impl serde::de::Error for SerdeMessage {
    fn custom<T: fmt::Display>(message: T) -> Self {
        Self(message.to_string())
    }
}

impl serde::de::Error for Error {
    fn custom<T: fmt::Display>(message: T) -> Self {
        Self::coded(
            ErrorKind::Serde,
            ErrorCode::SerdeCustom,
            message.to_string(),
        )
    }

    fn invalid_type(unexp: serde::de::Unexpected<'_>, exp: &dyn serde::de::Expected) -> Self {
        Self::coded(
            ErrorKind::Serde,
            ErrorCode::InvalidType,
            SerdeMessage::invalid_type(unexp, exp).0,
        )
    }

    fn invalid_value(unexp: serde::de::Unexpected<'_>, exp: &dyn serde::de::Expected) -> Self {
        Self::coded(
            ErrorKind::Serde,
            ErrorCode::InvalidValue,
            SerdeMessage::invalid_value(unexp, exp).0,
        )
    }

    fn invalid_length(len: usize, exp: &dyn serde::de::Expected) -> Self {
        Self::coded(
            ErrorKind::Serde,
            ErrorCode::InvalidLength,
            SerdeMessage::invalid_length(len, exp).0,
        )
    }

    fn unknown_variant(variant: &str, expected: &'static [&'static str]) -> Self {
        Self::coded(
            ErrorKind::Serde,
            ErrorCode::UnknownVariant,
            SerdeMessage::unknown_variant(variant, expected).0,
        )
    }

    fn unknown_field(field: &str, expected: &'static [&'static str]) -> Self {
        Self::coded(
            ErrorKind::Serde,
            ErrorCode::UnknownField,
            SerdeMessage::unknown_field(field, expected).0,
        )
    }

    fn missing_field(field: &'static str) -> Self {
        Self::coded(
            ErrorKind::Serde,
            ErrorCode::MissingField,
            SerdeMessage::missing_field(field).0,
        )
    }

    fn duplicate_field(field: &'static str) -> Self {
        Self::coded(
            ErrorKind::Serde,
            ErrorCode::DuplicateField,
            SerdeMessage::duplicate_field(field).0,
        )
    }
}

impl serde::ser::Error for Error {
    fn custom<T: fmt::Display>(message: T) -> Self {
        Self::coded(
            ErrorKind::Serde,
            ErrorCode::SerdeCustom,
            message.to_string(),
        )
    }
}

#[cfg(test)]
mod tests;

#[cfg(kani)]
mod kani_proofs {
    use super::{
        ELIDED, MAX_KEY_CHARS, MAX_PATH_BYTES, format_unicode_escape, hides_text, is_short_name,
        path_bytes, prepend_path_step, resolve_path_step, write_quoted_key, write_quoted_key_char,
    };

    #[kani::proof]
    fn verify_path_bytes_saturates() {
        let len: usize = kani::any();
        let kept = path_bytes(len);
        if len <= usize::from(u16::MAX) {
            assert_eq!(usize::from(kept), len);
        } else {
            assert_eq!(kept, u16::MAX);
        }
    }

    #[kani::proof]
    #[kani::unwind(6)]
    fn verify_is_short_name_ascii_sound() {
        let bytes: [u8; 4] = kani::any();
        let len: usize = kani::any();
        kani::assume(len <= bytes.len());
        // Restrict to ASCII bytes to keep UTF-8 validation symbolic-free; non-ASCII is also
        // rejected immediately by `is_short_name`.
        let mut i = 0;
        while i < len {
            kani::assume(bytes[i] < 0x80);
            i += 1;
        }
        let text = std::str::from_utf8(&bytes[..len]).unwrap();
        if is_short_name(text) {
            assert!(!text.is_empty());
            assert!(text.len() <= MAX_KEY_CHARS);
            let first = bytes[0];
            assert!(first.is_ascii_alphabetic() || first == b'_');
            let mut j = 1;
            while j < len {
                let b = bytes[j];
                assert!(b.is_ascii_alphanumeric() || b == b'_' || b == b'-');
                j += 1;
            }
        }
    }

    #[kani::proof]
    #[kani::unwind(12)]
    fn verify_quoted_key_escapes_control_and_valid_utf8() {
        // Exhaustively verify `hides_text` and `format_unicode_escape` over ALL Unicode scalar
        // values U+0000..=U+10FFFF: escaped output is pure printable ASCII without hidden chars.
        let ch: char = kani::any();
        if ch.is_control()
            || matches!(
                ch,
                '\u{61c}'
                    | '\u{200e}'
                    | '\u{200f}'
                    | '\u{2028}'
                    | '\u{2029}'
                    | '\u{202a}'..='\u{202e}'
                    | '\u{2066}'..='\u{2069}'
                    | '\u{feff}'
            )
        {
            assert!(hides_text(ch));
            let (buf, len) = format_unicode_escape(ch);
            assert!(len >= 5 && len <= 10);
            assert!(buf[0] == b'\\' && buf[1] == b'u' && buf[2] == b'{');
            assert!(buf[len - 1] == b'}');
            let mut i = 3;
            while i + 1 < len {
                let b = buf[i];
                assert!(b.is_ascii_hexdigit());
                assert!(!hides_text(char::from(b)));
                i += 1;
            }
        }
    }

    #[kani::proof]
    #[kani::unwind(6)]
    fn verify_quoted_key_truncation_keeps_utf8_boundaries() {
        // Symbolically verify across EVERY Unicode scalar U+0000..=U+10FFFF that taking at most
        // `cap` scalars from a multi-byte UTF-8 key always stops on a valid char boundary.
        let mut buf = [0_u8; 5];
        let ch: char = kani::any();
        let first_len = ch.encode_utf8(&mut buf).len();
        buf[first_len] = b'z';
        let text = std::str::from_utf8(&buf[..first_len + 1]).unwrap();
        let cap: usize = kani::any();
        kani::assume(cap <= 2);
        let mut kept_bytes = 0_usize;
        let mut kept_chars = 0_usize;
        for c in text.chars().take(cap) {
            kept_bytes += c.len_utf8();
            kept_chars += 1;
        }
        assert!(kept_bytes <= text.len());
        assert!(text.is_char_boundary(kept_bytes));
        assert_eq!(kept_chars, cap);
        if cap < 2 {
            assert!(kept_bytes < text.len());
        } else {
            assert_eq!(kept_bytes, text.len());
        }
    }

    struct StackBuf {
        bytes: [u8; 64],
        len: usize,
    }
    impl StackBuf {
        const fn new() -> Self {
            Self {
                bytes: [0; 64],
                len: 0,
            }
        }
        fn push_byte(&mut self, b: u8) {
            assert!(self.len < self.bytes.len());
            self.bytes[self.len] = b;
            self.len += 1;
        }
        fn as_slice(&self) -> &[u8] {
            &self.bytes[..self.len]
        }
    }
    impl core::fmt::Write for StackBuf {
        fn write_str(&mut self, s: &str) -> core::fmt::Result {
            for &b in s.as_bytes() {
                self.push_byte(b);
            }
            Ok(())
        }
        fn write_char(&mut self, c: char) -> core::fmt::Result {
            let cp = c as u32;
            if cp < 0x80 {
                self.push_byte(cp as u8);
            } else if cp < 0x800 {
                self.push_byte((0xC0 | (cp >> 6)) as u8);
                self.push_byte((0x80 | (cp & 0x3F)) as u8);
            } else if cp < 0x1_0000 {
                self.push_byte((0xE0 | (cp >> 12)) as u8);
                self.push_byte((0x80 | ((cp >> 6) & 0x3F)) as u8);
                self.push_byte((0x80 | (cp & 0x3F)) as u8);
            } else {
                self.push_byte((0xF0 | (cp >> 18)) as u8);
                self.push_byte((0x80 | ((cp >> 12) & 0x3F)) as u8);
                self.push_byte((0x80 | ((cp >> 6) & 0x3F)) as u8);
                self.push_byte((0x80 | (cp & 0x3F)) as u8);
            }
            Ok(())
        }
    }

    #[kani::proof]
    #[kani::unwind(12)]
    fn verify_write_quoted_key_char_all_unicode() {
        // Exhaustive symbolic proof over ALL 1,112,064 Unicode scalars through `write_quoted_key_char`.
        let ch: char = kani::any();
        let mut single = StackBuf::new();
        write_quoted_key_char(&mut single, ch);
        let sbytes = single.as_slice();
        assert!(!sbytes.is_empty() && sbytes.len() <= 10);
        if matches!(ch, '"' | '\\') {
            assert_eq!(sbytes, &[b'\\', ch as u8]);
        } else if hides_text(ch) {
            assert!(sbytes.len() >= 5 && sbytes.starts_with(b"\\u{") && sbytes.ends_with(b"}"));
            for &b in sbytes {
                assert!((0x20..0x7f).contains(&b));
            }
        } else {
            assert_eq!(sbytes.len(), ch.len_utf8());
        }
    }

    #[kani::proof]
    #[kani::unwind(36)]
    fn verify_push_quoted_key_escapes_and_truncates() {
        // Concrete table across empty, ASCII, quotes/backslashes, controls, BiDi, BOM, and
        // 2-/3-/4-byte UTF-8 at boundary caps `0..=4` keeps write indices concrete (paired with
        // the exhaustive symbolic `verify_write_quoted_key_char_all_unicode` proof above).
        for (sample, cap, expect_elided) in [
            ("", 0_usize, false),
            ("a\"\\\0", 0, true),
            ("a\"\\\0", 2, true),
            ("a\"\\\0", 4, false),
            ("é\u{202e}🚀", 2, true),
            ("é\u{202e}🚀", 3, false),
            ("\u{feff}€\n\x7f", 3, true),
            ("\u{feff}€\n\x7f", 4, false),
        ] {
            let mut out = StackBuf::new();
            write_quoted_key(&mut out, sample, cap);
            let qbytes = out.as_slice();
            assert!(qbytes.len() >= 4 && qbytes.starts_with(b"[\"") && qbytes.ends_with(b"\"]"));
            if expect_elided {
                assert_eq!(
                    &qbytes[qbytes.len() - 2 - ELIDED.len()..qbytes.len() - 2],
                    ELIDED.as_bytes()
                );
            } else {
                assert!(!qbytes[..qbytes.len() - 2].ends_with(ELIDED.as_bytes()));
            }
            for &b in qbytes {
                assert!(b >= 0x20 && b != 0x7f);
            }
        }
    }

    #[kani::proof]
    #[kani::unwind(8)]
    fn verify_inside_path_truncation_bounds_and_idempotence() {
        // 1. Exhaustively verify `resolve_path_step` over all path lengths (0..=usize::MAX),
        //    bracket/non-bracket separators, and representative step lengths: zero overflow,
        //    exact transition to `ELIDED` past `MAX_PATH_BYTES`, and saturating `u16` length.
        let path_len: usize = kani::any();
        let starts_with_bracket: bool = kani::any();
        let step_choice: u8 = kani::any();
        let step = match step_choice % 4 {
            0 => "",
            1 => "a",
            2 => "[0]",
            _ => "segment_step",
        };
        let (chosen, sep, next_len) = resolve_path_step(path_len, starts_with_bracket, step);
        if starts_with_bracket {
            assert_eq!(sep.len(), 0);
        } else {
            assert_eq!(sep.as_bytes(), b".");
        }
        let candidate = path_len
            .saturating_add(step.len())
            .saturating_add(sep.len());
        if candidate > MAX_PATH_BYTES {
            assert_eq!(chosen.as_ptr(), ELIDED.as_ptr());
            assert_eq!(chosen.len(), ELIDED.len());
        } else {
            assert_eq!(chosen.as_ptr(), step.as_ptr());
            assert_eq!(chosen.len(), step.len());
            assert!(usize::from(next_len) <= MAX_PATH_BYTES);
        }
        let expected_len = path_bytes(
            path_len
                .saturating_add(chosen.len())
                .saturating_add(sep.len()),
        );
        assert_eq!(next_len, expected_len);

        // 2. Verify `prepend_path_step` (used directly by `Error::inside`) at concrete lengths
        //    straddling `MAX_PATH_BYTES`, using pre-sized small buffers without boxing `ErrorInner`
        //    on Kani's symbolic heap: covers initial attachment, exact elision transition, and
        //    idempotence of subsequent steps once elided.
        let mut fresh_msg = String::with_capacity(16);
        fresh_msg.push_str("bad");
        let mut fresh_path_len = 0_u16;
        prepend_path_step(&mut fresh_msg, &mut fresh_path_len, "k");
        assert_eq!(fresh_path_len, 3);
        assert_eq!(fresh_msg.as_bytes(), b"k: bad");

        for cap in (MAX_PATH_BYTES - 4)..=MAX_PATH_BYTES {
            let mut msg = String::with_capacity(48);
            msg.push_str("a: b");
            let mut cur_path_len = cap as u16;
            // Step "xy" + "." = 3 bytes: fits when cap <= MAX_PATH_BYTES - 3, elides above.
            prepend_path_step(&mut msg, &mut cur_path_len, "xy");
            if cap + 3 > MAX_PATH_BYTES {
                assert!(msg.starts_with(ELIDED));
                let elided_len = msg.len();
                let elided_path_len = cur_path_len;
                prepend_path_step(&mut msg, &mut cur_path_len, "outer_ignored");
                assert_eq!(msg.len(), elided_len);
                assert_eq!(cur_path_len, elided_path_len);
                assert!(msg.starts_with(ELIDED));
            } else {
                assert!(msg.starts_with("xy."));
                assert_eq!(usize::from(cur_path_len), cap + 3);
                // One more 10-byte step pushes every remaining cap over MAX_PATH_BYTES and elides.
                prepend_path_step(&mut msg, &mut cur_path_len, "over_limit");
                assert!(msg.starts_with(ELIDED));
                let elided_len = msg.len();
                let elided_path_len = cur_path_len;
                prepend_path_step(&mut msg, &mut cur_path_len, "outer_ignored");
                assert_eq!(msg.len(), elided_len);
                assert_eq!(cur_path_len, elided_path_len);
            }
        }
    }
}