1use core::fmt;
2use derive_builder::UninitializedFieldError;
3use std::{error::Error, str::Utf8Error};
4
5#[derive(Debug, Clone, Copy)]
8pub struct InvalidValue;
9
10impl fmt::Display for InvalidValue {
11 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
12 write!(f, "invalid value")
13 }
14}
15
16impl Error for InvalidValue {}
17
18#[derive(Debug, Clone, Copy)]
21pub struct ValueIsZero;
22
23impl fmt::Display for ValueIsZero {
24 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
25 write!(f, "value must be other than 0")
26 }
27}
28
29impl Error for ValueIsZero {}
30
31#[derive(Debug, Clone, Copy)]
34pub struct ValueExceedesMaximum;
35
36impl fmt::Display for ValueExceedesMaximum {
37 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
38 write!(f, "value exceedes maximum")
39 }
40}
41
42impl Error for ValueExceedesMaximum {}
43
44#[derive(Debug, Clone, Copy)]
47pub struct InvalidEncoding;
48
49impl fmt::Display for InvalidEncoding {
50 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
51 write!(f, "invalid encoding")
52 }
53}
54
55impl Error for InvalidEncoding {}
56
57#[derive(Debug, Clone, Copy)]
60pub struct InsufficientBufferSize;
61
62impl fmt::Display for InsufficientBufferSize {
63 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
64 write!(f, "insufficient buffer size")
65 }
66}
67
68impl Error for InsufficientBufferSize {}
69
70#[derive(Debug, Clone)]
73pub enum ConversionError {
74 InvalidValue(InvalidValue),
77
78 ValueIsZero(ValueIsZero),
81
82 ValueExceedesMaximum(ValueExceedesMaximum),
85
86 InvalidEncoding(InvalidEncoding),
89
90 Utf8Error(Utf8Error),
93
94 InsufficientBufferSize(InsufficientBufferSize),
97}
98
99impl fmt::Display for ConversionError {
100 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
101 match self {
102 Self::InvalidValue(err) => write!(
103 f,
104 "{{ \"type\": \"ConversionError\", \"message\": \"{}\" }}",
105 err
106 ),
107 Self::ValueIsZero(err) => write!(
108 f,
109 "{{ \"type\": \"ConversionError\", \"message\": \"{}\" }}",
110 err
111 ),
112 Self::ValueExceedesMaximum(err) => write!(
113 f,
114 "{{ \"type\": \"ConversionError\", \"message\": \"{}\" }}",
115 err
116 ),
117 Self::InvalidEncoding(err) => write!(
118 f,
119 "{{ \"type\": \"ConversionError\", \"message\": \"{}\" }}",
120 err
121 ),
122 Self::Utf8Error(err) => write!(
123 f,
124 "{{ \"type\": \"ConversionError\", \"message\": \"{}\" }}",
125 err
126 ),
127 Self::InsufficientBufferSize(err) => write!(
128 f,
129 "{{ \"type\": \"ConversionError\", \"message\": \"{}\" }}",
130 err
131 ),
132 }
133 }
134}
135
136impl Error for ConversionError {}
137
138impl From<InvalidValue> for ConversionError {
139 fn from(err: InvalidValue) -> Self {
140 Self::InvalidValue(err)
141 }
142}
143
144impl From<ValueIsZero> for ConversionError {
145 fn from(err: ValueIsZero) -> Self {
146 Self::ValueIsZero(err)
147 }
148}
149
150impl From<ValueExceedesMaximum> for ConversionError {
151 fn from(err: ValueExceedesMaximum) -> Self {
152 Self::ValueExceedesMaximum(err)
153 }
154}
155
156impl From<InvalidEncoding> for ConversionError {
157 fn from(err: InvalidEncoding) -> Self {
158 Self::InvalidEncoding(err)
159 }
160}
161
162impl From<Utf8Error> for ConversionError {
163 fn from(err: Utf8Error) -> Self {
164 Self::Utf8Error(err)
165 }
166}
167
168impl From<InsufficientBufferSize> for ConversionError {
169 fn from(err: InsufficientBufferSize) -> Self {
170 Self::InsufficientBufferSize(err)
171 }
172}
173
174#[derive(Debug, Clone, Copy)]
177pub struct InvalidPropertyId;
178
179impl fmt::Display for InvalidPropertyId {
180 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
181 write!(f, "invalid property identifier")
182 }
183}
184
185impl Error for InvalidPropertyId {}
186
187#[allow(missing_docs)]
190#[derive(Debug, Clone)]
191pub enum PropertyError {
192 ConversionError(ConversionError),
193 InvalidPropertyId(InvalidPropertyId),
194}
195
196impl fmt::Display for PropertyError {
197 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
198 match self {
199 Self::ConversionError(err) => write!(f, "{}", err),
200 Self::InvalidPropertyId(err) => write!(
201 f,
202 "{{ \"type\": \"PropertyError\", \"message\": \"{}\" }}",
203 err
204 ),
205 }
206 }
207}
208
209impl Error for PropertyError {}
210
211impl From<ConversionError> for PropertyError {
212 fn from(err: ConversionError) -> Self {
213 Self::ConversionError(err)
214 }
215}
216
217impl From<InvalidPropertyId> for PropertyError {
218 fn from(err: InvalidPropertyId) -> Self {
219 Self::InvalidPropertyId(err)
220 }
221}
222
223#[derive(Debug, Clone, Copy)]
226pub struct UnexpectedProperty;
227
228impl fmt::Display for UnexpectedProperty {
229 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
230 write!(f, "unexpected property")
231 }
232}
233
234impl Error for UnexpectedProperty {}
235
236#[derive(Debug, Clone, Copy)]
239pub struct InvalidPacketHeader;
240
241impl fmt::Display for InvalidPacketHeader {
242 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
243 write!(f, "invalid packet header")
244 }
245}
246
247impl Error for InvalidPacketHeader {}
248
249#[derive(Debug, Clone, Copy)]
252pub struct InvalidPacketSize;
253
254impl fmt::Display for InvalidPacketSize {
255 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
256 write!(f, "invalid packet size")
257 }
258}
259
260impl Error for InvalidPacketSize {}
261
262#[derive(Debug, Clone, Copy)]
265pub struct InvalidPropertyLength;
266
267impl fmt::Display for InvalidPropertyLength {
268 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
269 write!(f, "invalid property length")
270 }
271}
272
273impl Error for InvalidPropertyLength {}
274
275#[derive(Debug, Clone, Copy)]
278pub struct MandatoryPropertyMissing;
279
280impl fmt::Display for MandatoryPropertyMissing {
281 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
282 write!(f, "mandatory property missing")
283 }
284}
285
286impl Error for MandatoryPropertyMissing {}
287
288#[allow(missing_docs)]
291#[derive(Debug, Clone)]
292pub enum CodecError {
293 ConversionError(ConversionError),
294 PropertyError(PropertyError),
295 UnexpectedProperty(UnexpectedProperty),
296 InvalidPacketHeader(InvalidPacketHeader),
297 InvalidPacketSize(InvalidPacketSize),
298 InvalidPropertyLength(InvalidPropertyLength),
299 InsufficientBufferSize(InsufficientBufferSize),
300 MandatoryPropertyMissing(MandatoryPropertyMissing),
301}
302
303impl fmt::Display for CodecError {
304 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
305 match self {
306 Self::ConversionError(err) => write!(f, "{}", err),
307 Self::PropertyError(err) => write!(f, " {}", err),
308 Self::UnexpectedProperty(err) => write!(
309 f,
310 "{{ \"type\": \"CodecError\", \"message\": \"{}\" }}",
311 err
312 ),
313 Self::InvalidPacketHeader(err) => write!(
314 f,
315 "{{ \"type\": \"CodecError\", \"message\": \"{}\" }}",
316 err
317 ),
318 Self::InvalidPacketSize(err) => write!(
319 f,
320 "{{ \"type\": \"CodecError\", \"message\": \"{}\" }}",
321 err
322 ),
323 Self::InvalidPropertyLength(err) => write!(
324 f,
325 "{{ \"type\": \"CodecError\", \"message\": \"{}\" }}",
326 err
327 ),
328 Self::InsufficientBufferSize(err) => write!(
329 f,
330 "{{ \"type\": \"CodecError\", \"message\": \"{}\" }}",
331 err
332 ),
333 Self::MandatoryPropertyMissing(err) => write!(
334 f,
335 "{{ \"type\": \"CodecError\", \"message\": \"{}\" }}",
336 err
337 ),
338 }
339 }
340}
341
342impl Error for CodecError {}
343
344impl From<ConversionError> for CodecError {
345 fn from(err: ConversionError) -> Self {
346 Self::PropertyError(err.into())
347 }
348}
349
350impl From<PropertyError> for CodecError {
351 fn from(err: PropertyError) -> Self {
352 Self::PropertyError(err)
353 }
354}
355
356impl From<UnexpectedProperty> for CodecError {
357 fn from(err: UnexpectedProperty) -> Self {
358 Self::UnexpectedProperty(err)
359 }
360}
361
362impl From<InvalidPacketHeader> for CodecError {
363 fn from(err: InvalidPacketHeader) -> Self {
364 Self::InvalidPacketHeader(err)
365 }
366}
367
368impl From<InvalidPacketSize> for CodecError {
369 fn from(err: InvalidPacketSize) -> Self {
370 Self::InvalidPacketSize(err)
371 }
372}
373
374impl From<InvalidPropertyLength> for CodecError {
375 fn from(err: InvalidPropertyLength) -> Self {
376 Self::InvalidPropertyLength(err)
377 }
378}
379
380impl From<InsufficientBufferSize> for CodecError {
381 fn from(err: InsufficientBufferSize) -> Self {
382 Self::InsufficientBufferSize(err)
383 }
384}
385
386impl From<MandatoryPropertyMissing> for CodecError {
387 fn from(err: MandatoryPropertyMissing) -> Self {
388 Self::MandatoryPropertyMissing(err)
389 }
390}
391
392impl From<UninitializedFieldError> for CodecError {
393 fn from(_: UninitializedFieldError) -> CodecError {
394 MandatoryPropertyMissing.into()
395 }
396}