cbor_smol/error.rs
1#![allow(unused_variables)]
2
3use core::fmt::{Display, Formatter};
4
5/// This is the Result type used by cbor-smol.
6pub type Result<T, Err = Error> = core::result::Result<T, Err>;
7
8/// This is the error type used by cbor-smol
9#[derive(Clone, Copy, Debug, Eq, PartialEq)]
10#[repr(u8)]
11#[non_exhaustive]
12pub enum Error {
13 /// This is a feature that cbor-smol will never implement
14 WontImplement,
15 /// This is a feature that cbor-smol intends to support, but does not yet
16 NotYetImplemented,
17 /// The serialize buffer is full
18 SerializeBufferFull(usize),
19 // /// The length of a sequence must be known
20 // SerializeSeqLengthUnknown,
21 /// Hit the end of buffer, expected more data
22 DeserializeUnexpectedEnd,
23 // /// Found a varint that didn't terminate. Is the usize too big for this platform?
24 // DeserializeBadVarint,
25 /// Found a bool that wasn't 0xf4 or 0xf5
26 DeserializeBadBool,
27 // /// Found an invalid unicode char
28 // DeserializeBadChar,
29 /// Tried to parse invalid utf-8
30 DeserializeBadUtf8,
31 // /// Found an Option discriminant that wasn't 0 or 1
32 // DeserializeBadOption,
33 // /// Found an enum discriminant that was > u32::max_value()
34 /// Could not parse an enum
35 DeserializeBadEnum,
36 // /// The original data was not well encoded
37 // DeserializeBadEncoding,
38 /// Expected a different major type
39 DeserializeBadMajor,
40 /// Expected a i8, was too large
41 DeserializeBadI8,
42 /// Expected a i16, was too large
43 DeserializeBadI16,
44 /// Expected a i32, was too large
45 DeserializeBadI32,
46 /// Expected a i64, was too large
47 DeserializeBadI64,
48 /// Expected a u8
49 DeserializeBadU8,
50 /// Expected a u16
51 DeserializeBadU16,
52 /// Expected a u32
53 DeserializeBadU32,
54 /// Expected a u64
55 DeserializeBadU64,
56 /// Expected a NULL marker
57 DeserializeExpectedNull,
58 /// Inexistent slice-to-array cast error. Used here to avoid calling unwrap.
59 InexistentSliceToArrayError,
60 /// Value may be valid, but not encoded in minimal way
61 DeserializeNonMinimal,
62 /// Serde Serialization Error
63 SerdeSerCustom,
64 /// Serde Deserialization Error
65 SerdeDeCustom,
66 /// Serde Missing required value
67 SerdeMissingField,
68}
69
70impl Display for Error {
71 fn fmt(&self, f: &mut Formatter) -> core::fmt::Result {
72 use Error::*;
73 write!(
74 f,
75 "{}",
76 match self {
77 WontImplement => "This is a feature that cbor-smol will never implement",
78 NotYetImplemented => {
79 "This is a feature that cbor-smol intends to support, but does not yet"
80 }
81 SerializeBufferFull(i) => "The serialize buffer is full",
82 // SerializeSeqLengthUnknown => "The length of a sequence must be known",
83 DeserializeUnexpectedEnd => "Hit the end of buffer, expected more data",
84 // DeserializeBadVarint => {
85 // "Found a varint that didn't terminate. Is the usize too big for this platform?"
86 // }
87 DeserializeBadBool => "Found a bool that wasn't 0xf4 or 0xf5",
88 // DeserializeBadChar => "Found an invalid unicode char",
89 DeserializeBadUtf8 => "Tried to parse invalid utf-8",
90 // DeserializeBadOption => "Found an Option discriminant that wasn't 0 or 1",
91 // DeserializeBadEnum => "Found an enum discriminant that was > u32::max_value()",
92 DeserializeBadEnum => "Could not parse an enum",
93 // DeserializeBadEncoding => "The original data was not well encoded",
94 DeserializeBadI8 => "Expected a i8",
95 DeserializeBadI16 => "Expected a i16",
96 DeserializeBadI32 => "Expected a i32",
97 DeserializeBadI64 => "Expected a i64",
98 DeserializeBadMajor => "Expected a different major type",
99 DeserializeBadU8 => "Expected a u8",
100 DeserializeBadU16 => "Expected a u16",
101 DeserializeBadU32 => "Expected a u32",
102 DeserializeBadU64 => "Expected a u64",
103 DeserializeExpectedNull => "Expected 0xf6",
104 InexistentSliceToArrayError => "",
105 DeserializeNonMinimal => "Value may be valid, but not encoded in minimal way",
106 SerdeSerCustom => "Serde Serialization Error",
107 SerdeDeCustom => "Serde Deserialization Error",
108 SerdeMissingField => "Serde Missing Required Field",
109 }
110 )
111 }
112}
113
114impl serde::ser::Error for Error {
115 fn custom<T>(_msg: T) -> Self
116 where
117 T: Display,
118 {
119 Error::SerdeSerCustom
120 }
121}
122
123impl serde::de::Error for Error {
124 fn custom<T>(msg: T) -> Self
125 where
126 T: Display,
127 {
128 // TODO: Would be helpful to log this to system logger
129 // This shows e.g.
130 // - missing fields
131 // - expected sequence, received X
132 // - etc.
133 //
134 // Particularly helpful would be better errors when receiving
135 // structures are undersized.
136 //
137 // E.g. if there is a `Bytes<N>` and more than N bytes are delivered,
138 // currently the error _msg: T is:
139 //
140 // `invalid length 297, expected a sequence`
141 //
142 info_now!("deser error: {}", &msg);
143 Error::SerdeDeCustom
144 }
145 fn missing_field(field: &'static str) -> Self {
146 info_now!("deser missing: {}", field);
147 Error::SerdeMissingField
148 }
149}
150
151impl serde::ser::StdError for Error {}