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
use core::fmt::{self, Debug, Display};
#[derive(Clone, Debug)]
pub struct Error<E: Debug> {
kind: ErrorKind,
cause: Option<E>,
}
impl<E> Error<E>
where
E: Debug,
{
pub fn new(kind: ErrorKind) -> Self {
Self { kind, cause: None }
}
pub fn new_with_cause(kind: ErrorKind, cause: E) -> Self {
Self {
kind,
cause: Some(cause),
}
}
pub fn kind(&self) -> ErrorKind {
self.kind
}
pub fn cause(&self) -> Option<&E> {
self.cause.as_ref()
}
pub fn into_cause(self) -> E {
self.cause
.expect("into_cause called on an error with no cause")
}
}
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum ErrorKind {
Bus,
Device,
Mode,
Param,
}
impl ErrorKind {
pub fn err<E: Debug>(self) -> Result<(), Error<E>> {
Err(Error::new(self))
}
}
impl ErrorKind {
pub fn description(self) -> &'static str {
match self {
ErrorKind::Device => "device error",
ErrorKind::Bus => "bus error",
ErrorKind::Mode => "invalid mode",
ErrorKind::Param => "invalid parameter",
}
}
}
impl Display for ErrorKind {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.description())
}
}
impl<E> From<E> for Error<E>
where
E: Debug,
{
fn from(cause: E) -> Error<E> {
Self::new_with_cause(ErrorKind::Bus, cause)
}
}