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
use core::fmt;
/// A structured error code identifying the reason a CBOR item was rejected.
///
/// This enum is intentionally stable and string-free to support `no_std` and to remain hot-path friendly.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum ErrorCode {
/// Invalid configured limits.
InvalidLimits,
/// Unexpected end-of-input while decoding.
UnexpectedEof,
/// Arithmetic overflow while computing a length/offset.
LengthOverflow,
/// Input contains trailing bytes after the single CBOR data item.
TrailingBytes,
/// Memory allocation failed while decoding into owned structures.
AllocationFailed,
/// Array builder length mismatch (encoder).
ArrayLenMismatch,
/// Map builder length mismatch (encoder).
MapLenMismatch,
/// Nesting depth limit exceeded.
DepthLimitExceeded,
/// Total items limit exceeded.
TotalItemsLimitExceeded,
/// Array length exceeds limits.
ArrayLenLimitExceeded,
/// Map length exceeds limits.
MapLenLimitExceeded,
/// Byte string length exceeds limits.
BytesLenLimitExceeded,
/// Text string length exceeds limits.
TextLenLimitExceeded,
/// Total input length exceeds limits.
MessageLenLimitExceeded,
/// Reserved additional-info value (28..30) was used.
ReservedAdditionalInfo,
/// Indefinite-length encoding was used where forbidden.
IndefiniteLengthForbidden,
/// Non-canonical (non-shortest) integer/length encoding was used.
NonCanonicalEncoding,
/// Map key was not a CBOR text string.
MapKeyMustBeText,
/// Duplicate map key detected.
DuplicateMapKey,
/// Map keys are not in canonical order.
NonCanonicalMapOrder,
/// Set elements are not in strict canonical set order.
NonCanonicalSetOrder,
/// A forbidden tag was used, or the tag structure is malformed.
ForbiddenOrMalformedTag,
/// Bignum magnitude is not canonical (empty or leading zero).
BignumNotCanonical,
/// Bignum was used for a value within the safe integer range.
BignumMustBeOutsideSafeRange,
/// Unsupported CBOR simple value.
UnsupportedSimpleValue,
/// Integer outside the `int_safe` range.
IntegerOutsideSafeRange,
/// Invalid UTF-8 in a text string.
Utf8Invalid,
/// Float64 negative zero encoding is forbidden.
NegativeZeroForbidden,
/// Float64 NaN encoding is not canonical.
NonCanonicalNaN,
/// Serde conversion failed.
SerdeError,
/// Expected a map at the current location.
ExpectedMap,
/// Expected an array at the current location.
ExpectedArray,
/// Expected an integer (safe or bignum) at the current location.
ExpectedInteger,
/// Expected a text string at the current location.
ExpectedText,
/// Expected a byte string at the current location.
ExpectedBytes,
/// Expected a boolean at the current location.
ExpectedBool,
/// Expected a null at the current location.
ExpectedNull,
/// Expected a float64 at the current location.
ExpectedFloat,
/// Expected a CBOR value matching an untagged enum variant.
ExpectedEnum,
/// Unknown enum variant key.
UnknownEnumVariant,
/// Patch operations overlap or conflict.
PatchConflict,
/// Array index is out of bounds.
IndexOutOfBounds,
/// Invalid query arguments (e.g., output slice length mismatch).
InvalidQuery,
/// Required key missing from map.
MissingKey,
/// Malformed canonical CBOR during query traversal.
MalformedCanonical,
}
/// An SACP-CBOR/1 error with structured classification, a stable code, and a byte offset.
///
/// Offsets refer to the byte position where the error was detected.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct CborError {
/// The error code.
pub code: ErrorCode,
/// Byte offset into the input where the error was detected.
pub offset: usize,
}
impl CborError {
/// Construct a decode error at `offset`.
#[inline]
#[must_use]
pub const fn new(code: ErrorCode, offset: usize) -> Self {
Self { code, offset }
}
}
impl fmt::Display for CborError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let msg = match self.code {
ErrorCode::InvalidLimits => "invalid CBOR limits",
ErrorCode::UnexpectedEof => "unexpected end of input",
ErrorCode::LengthOverflow => "length overflow",
ErrorCode::TrailingBytes => "trailing bytes after single CBOR item",
ErrorCode::AllocationFailed => "allocation failed",
ErrorCode::ArrayLenMismatch => "array length mismatch",
ErrorCode::MapLenMismatch => "map length mismatch",
ErrorCode::DepthLimitExceeded => "nesting depth limit exceeded",
ErrorCode::TotalItemsLimitExceeded => "total items limit exceeded",
ErrorCode::ArrayLenLimitExceeded => "array length exceeds decode limits",
ErrorCode::MapLenLimitExceeded => "map length exceeds decode limits",
ErrorCode::BytesLenLimitExceeded => "byte string length exceeds decode limits",
ErrorCode::TextLenLimitExceeded => "text string length exceeds decode limits",
ErrorCode::MessageLenLimitExceeded => "input length exceeds decode limits",
ErrorCode::ReservedAdditionalInfo => "reserved additional info value",
ErrorCode::IndefiniteLengthForbidden => "indefinite length forbidden",
ErrorCode::NonCanonicalEncoding => "non-canonical integer/length encoding",
ErrorCode::MapKeyMustBeText => "map keys must be text strings",
ErrorCode::DuplicateMapKey => "duplicate map key",
ErrorCode::NonCanonicalMapOrder => "non-canonical map key order",
ErrorCode::NonCanonicalSetOrder => "non-canonical set element order",
ErrorCode::ForbiddenOrMalformedTag => "forbidden or malformed CBOR tag",
ErrorCode::BignumNotCanonical => {
"bignum magnitude must be canonical (non-empty, no leading zero)"
}
ErrorCode::BignumMustBeOutsideSafeRange => "bignum must be outside int_safe range",
ErrorCode::UnsupportedSimpleValue => "unsupported CBOR simple value",
ErrorCode::IntegerOutsideSafeRange => "integer outside int_safe range",
ErrorCode::Utf8Invalid => "text must be valid UTF-8",
ErrorCode::NegativeZeroForbidden => "negative zero forbidden",
ErrorCode::NonCanonicalNaN => "non-canonical NaN encoding",
ErrorCode::SerdeError => "serde conversion failed",
ErrorCode::ExpectedMap => "expected CBOR map",
ErrorCode::ExpectedArray => "expected CBOR array",
ErrorCode::ExpectedInteger => "expected CBOR integer",
ErrorCode::ExpectedText => "expected CBOR text string",
ErrorCode::ExpectedBytes => "expected CBOR byte string",
ErrorCode::ExpectedBool => "expected CBOR bool",
ErrorCode::ExpectedNull => "expected CBOR null",
ErrorCode::ExpectedFloat => "expected CBOR float64",
ErrorCode::ExpectedEnum => "expected CBOR enum value",
ErrorCode::UnknownEnumVariant => "unknown CBOR enum variant",
ErrorCode::PatchConflict => "patch operations conflict",
ErrorCode::IndexOutOfBounds => "array index out of bounds",
ErrorCode::InvalidQuery => "invalid query arguments",
ErrorCode::MissingKey => "missing required map key",
ErrorCode::MalformedCanonical => "malformed canonical CBOR",
};
write!(f, "cbor error at {}: {msg}", self.offset)
}
}
#[cfg(feature = "std")]
impl std::error::Error for CborError {}