codec_cbor/general/error.rs
1// SPDX-FileCopyrightText: 2026 ReallyMe Technologies Limited
2//
3// SPDX-License-Identifier: MIT OR Apache-2.0
4
5use thiserror::Error;
6
7/// Fixed, payload-free reasons for general CBOR rejection.
8#[derive(Clone, Copy, Debug, Eq, Error, PartialEq)]
9#[non_exhaustive]
10pub enum ErrorReason {
11 /// No top-level item was available.
12 #[error("empty input")]
13 EmptyInput,
14 /// A header or payload ended prematurely.
15 #[error("truncated item")]
16 Truncated,
17 /// Additional-information values 28 through 30 are reserved.
18 #[error("reserved additional information")]
19 ReservedAdditionalInformation,
20 /// Integers and tags cannot have indefinite arguments.
21 #[error("invalid indefinite argument")]
22 InvalidIndefiniteArgument,
23 /// A break occurred outside its permitted container position.
24 #[error("unexpected break")]
25 UnexpectedBreak,
26 /// A simple value used a forbidden two-byte representation.
27 #[error("invalid simple value encoding")]
28 InvalidSimpleValue,
29 /// A chunk was indefinite or had the wrong string type.
30 #[error("invalid string chunk")]
31 InvalidChunk,
32 /// The selected profile permits definite lengths only.
33 #[error("indefinite length is not allowed")]
34 IndefiniteNotAllowed,
35 /// An argument used more octets than necessary.
36 #[error("non-preferred argument encoding")]
37 NonPreferredArgument,
38 /// A float could be represented exactly at a shorter width.
39 #[error("non-preferred floating-point encoding")]
40 NonPreferredFloat,
41 /// The selected profile rejects floating-point values.
42 #[error("floating-point values are not allowed")]
43 FloatNotAllowed,
44 /// The selected profile rejects infinity and NaN.
45 #[error("non-finite floating-point value")]
46 NonFiniteFloat,
47 /// A text string or an individual text chunk was not UTF-8.
48 #[error("invalid UTF-8")]
49 InvalidUtf8,
50 /// Two map keys are equivalent in the basic generic data model.
51 #[error("duplicate map key")]
52 DuplicateMapKey,
53 /// Received keys violate the explicitly selected ordering rule.
54 #[error("map key ordering violation")]
55 MapKeyOrder,
56 /// More bytes followed the requested complete item.
57 #[error("trailing data")]
58 TrailingData,
59 /// An operation was applied to a different CBOR type.
60 #[error("unexpected item type")]
61 WrongType,
62 /// Embedded-item validation requires the standard encoded-CBOR tag.
63 #[error("unexpected tag")]
64 WrongTag,
65 /// A tag-24 byte string is empty, so it holds no embedded data item.
66 #[error("empty embedded CBOR")]
67 EmptyEmbeddedItem,
68 /// Chunked contents do not form one contiguous borrowed payload.
69 #[error("non-contiguous string contents")]
70 NonContiguousString,
71 /// A limit exceeded its supported hard ceiling.
72 #[error("invalid resource limit")]
73 InvalidLimit,
74 /// The profile combination cannot provide the requested guarantee.
75 #[error("unsupported profile combination")]
76 UnsupportedPolicy,
77 /// Input exceeded the configured transport cap.
78 #[error("input limit exceeded")]
79 InputLimit,
80 /// Output would exceed the configured cap.
81 #[error("output limit exceeded")]
82 OutputLimit,
83 /// Too many nested containers or tags were opened.
84 #[error("nesting limit exceeded")]
85 DepthLimit,
86 /// Too many structural nodes were encountered.
87 #[error("node limit exceeded")]
88 NodeLimit,
89 /// A container has too many entries or chunks.
90 #[error("container entry limit exceeded")]
91 EntryLimit,
92 /// Aggregate byte-string contents exceeded the configured cap.
93 #[error("byte-string content limit exceeded")]
94 ByteStringLimit,
95 /// Aggregate text contents exceeded the configured cap.
96 #[error("text content limit exceeded")]
97 TextLimit,
98 /// Parsing or semantic comparison exhausted its explicit work budget.
99 #[error("validation work limit exceeded")]
100 WorkLimit,
101 /// A declared container cannot fit in the remaining received input.
102 #[error("container declaration exceeds remaining input")]
103 ContainerExceedsInput,
104 /// A size or offset calculation could not be represented.
105 #[error("length arithmetic overflow")]
106 ArithmeticOverflow,
107 /// A bounded output allocation could not be reserved.
108 #[error("bounded allocation failed")]
109 AllocationFailure,
110}
111
112/// General CBOR error with no input, offsets, strings, or dependency diagnostics.
113#[derive(Clone, Copy, Debug, Eq, Error, PartialEq)]
114#[error("general CBOR: {reason}")]
115pub struct CborError {
116 reason: ErrorReason,
117}
118
119impl CborError {
120 /// Construct a fixed typed error.
121 #[must_use]
122 pub const fn new(reason: ErrorReason) -> Self {
123 Self { reason }
124 }
125
126 /// Return the stable rejection reason.
127 #[must_use]
128 pub const fn reason(self) -> ErrorReason {
129 self.reason
130 }
131}
132
133pub(super) const fn error(reason: ErrorReason) -> CborError {
134 CborError::new(reason)
135}