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
use core::fmt::{self, Display, Formatter};
#[cfg(feature = "std")]
use std::error::Error;
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct InsufficientCapacity {
pub required: usize,
pub actual: usize,
}
#[cfg(feature = "std")]
impl Error for InsufficientCapacity {
fn description(&self) -> &str { "Insufficient Capacity" }
}
impl Display for InsufficientCapacity {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(
f,
"Insufficient capacity. At least {} bytes were required but only found {}",
self.required, self.actual
)
}
}
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum DecodeError {
RequiresMoreData,
InvalidCRC,
}
impl DecodeError {
fn msg(&self) -> &str {
match *self {
DecodeError::RequiresMoreData => "More data required",
DecodeError::InvalidCRC => "Invalid CRC",
}
}
}
#[cfg(feature = "std")]
impl Error for DecodeError {
fn description(&self) -> &str { self.msg() }
}
impl Display for DecodeError {
fn fmt(&self, f: &mut Formatter) -> fmt::Result { self.msg().fmt(f) }
}
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum EncodeError {
InsufficientCapacity(InsufficientCapacity),
EmptyPacket(EmptyPacket),
}
#[cfg(feature = "std")]
impl Error for EncodeError {
fn description(&self) -> &str {
match *self {
EncodeError::InsufficientCapacity(ref i) => i.description(),
EncodeError::EmptyPacket(ref e) => e.description(),
}
}
}
impl Display for EncodeError {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
match *self {
EncodeError::InsufficientCapacity(i) => i.fmt(f),
EncodeError::EmptyPacket(e) => e.fmt(f),
}
}
}
impl From<InsufficientCapacity> for EncodeError {
fn from(other: InsufficientCapacity) -> EncodeError {
EncodeError::InsufficientCapacity(other)
}
}
impl From<EmptyPacket> for EncodeError {
fn from(other: EmptyPacket) -> EncodeError {
EncodeError::EmptyPacket(other)
}
}
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct EmptyPacket;
impl EmptyPacket {
fn msg(&self) -> &str { "Cannot encode an empty packet" }
}
#[cfg(feature = "std")]
impl Error for EmptyPacket {
fn description(&self) -> &str { self.msg() }
}
impl Display for EmptyPacket {
fn fmt(&self, f: &mut Formatter) -> fmt::Result { self.msg().fmt(f) }
}