1use core::fmt::{self, Display, Formatter};
4#[cfg(feature = "std")]
5use std::error::Error;
6
7#[derive(Debug, Copy, Clone, PartialEq, Eq)]
9pub struct InsufficientCapacity {
10 pub required: usize,
12 pub actual: usize,
14}
15
16#[cfg(feature = "std")]
17impl Error for InsufficientCapacity {
18 fn description(&self) -> &str { "Insufficient Capacity" }
19}
20
21impl Display for InsufficientCapacity {
22 fn fmt(&self, f: &mut Formatter) -> fmt::Result {
23 write!(
24 f,
25 "Insufficient capacity. At least {} bytes were required but only found {}",
26 self.required, self.actual
27 )
28 }
29}
30
31#[derive(Debug, Copy, Clone, PartialEq, Eq)]
33pub enum DecodeError {
34 RequiresMoreData,
36 InvalidCRC,
38}
39
40impl DecodeError {
41 fn msg(&self) -> &str {
42 match *self {
43 DecodeError::RequiresMoreData => "More data required",
44 DecodeError::InvalidCRC => "Invalid CRC",
45 }
46 }
47}
48
49#[cfg(feature = "std")]
50impl Error for DecodeError {
51 fn description(&self) -> &str { self.msg() }
52}
53
54impl Display for DecodeError {
55 fn fmt(&self, f: &mut Formatter) -> fmt::Result { self.msg().fmt(f) }
56}
57
58#[derive(Debug, Copy, Clone, PartialEq, Eq)]
60pub enum EncodeError {
61 InsufficientCapacity(InsufficientCapacity),
63 EmptyPacket(EmptyPacket),
65}
66
67#[cfg(feature = "std")]
68impl Error for EncodeError {
69 fn description(&self) -> &str {
70 match *self {
71 EncodeError::InsufficientCapacity(ref i) => i.description(),
72 EncodeError::EmptyPacket(ref e) => e.description(),
73 }
74 }
75}
76
77impl Display for EncodeError {
78 fn fmt(&self, f: &mut Formatter) -> fmt::Result {
79 match *self {
80 EncodeError::InsufficientCapacity(i) => i.fmt(f),
81 EncodeError::EmptyPacket(e) => e.fmt(f),
82 }
83 }
84}
85
86impl From<InsufficientCapacity> for EncodeError {
87 fn from(other: InsufficientCapacity) -> EncodeError {
88 EncodeError::InsufficientCapacity(other)
89 }
90}
91
92impl From<EmptyPacket> for EncodeError {
93 fn from(other: EmptyPacket) -> EncodeError {
94 EncodeError::EmptyPacket(other)
95 }
96}
97
98#[derive(Debug, Copy, Clone, PartialEq, Eq)]
103pub struct EmptyPacket;
104
105impl EmptyPacket {
106 fn msg(&self) -> &str { "Cannot encode an empty packet" }
107}
108
109#[cfg(feature = "std")]
110impl Error for EmptyPacket {
111 fn description(&self) -> &str { self.msg() }
112}
113
114impl Display for EmptyPacket {
115 fn fmt(&self, f: &mut Formatter) -> fmt::Result { self.msg().fmt(f) }
116}