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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
use std::{
error::Error as StdError,
fmt::{
Display,
Formatter,
Result as FmtResult,
},
io::{
Error as IoError,
ErrorKind as IoErrorKind,
},
};
use rppal::{
gpio::Error as GpioError,
uart::Error as UartError,
};
#[derive(Debug)]
pub enum Error {
Uninitialized,
InvalidValue(u16),
FaultyRead {
#[allow(missing_docs)]
actual_count: usize,
},
FaultyWrite {
#[allow(missing_docs)]
actual_count: usize,
#[allow(missing_docs)]
expected_count: usize,
},
Io(IoError),
}
#[doc(hidden)]
impl Error {
pub(crate) fn new_io_error<E>(err_kind: IoErrorKind, err_msg: E) -> Self
where
E: Into<Box<dyn StdError + Send + Sync>>,
{
Error::Io(IoError::new(err_kind, err_msg))
}
}
impl StdError for Error {}
impl Display for Error {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
match self {
Error::Uninitialized => write!(f, "maestro struct is uninitialized; please consider calling .start() on the instance first"),
Error::InvalidValue(value) => write!(f, "target must be between 3968 quarter-us (992us) and 8000 quarter-us (2000us) but {} quarter-us was used", value),
Error::FaultyRead { actual_count, } => write!(f, "2 bytes were expected to be read, but only {} byte(s) were actually read", actual_count),
Error::FaultyWrite { actual_count, expected_count, } => write!(f, "{} bytes were expected to be written, but only {} byte(s) were actually written", expected_count, actual_count),
Error::Io(io_error) => io_error.fmt(f),
}
}
}
impl From<IoError> for Error {
fn from(io_error: IoError) -> Self { Self::Io(io_error) }
}
impl From<UartError> for Error {
fn from(uart_error: UartError) -> Self {
match uart_error {
UartError::Io(std_err) => Error::from(std_err),
UartError::Gpio(gpio_err) => match gpio_err {
GpioError::UnknownModel => Error::new_io_error(IoErrorKind::Other, "unknown model"),
GpioError::PinNotAvailable(pin) => Error::new_io_error(
IoErrorKind::AddrNotAvailable,
format!("pin number {} is not available", pin),
),
GpioError::PermissionDenied(err_string) => Error::new_io_error(
IoErrorKind::PermissionDenied,
format!("permission denied: {} ", err_string),
),
GpioError::Io(error) => Error::from(error),
GpioError::ThreadPanic => Error::new_io_error(IoErrorKind::Other, "thread panic"),
},
UartError::InvalidValue => Error::new_io_error(IoErrorKind::Other, "invalid value"),
}
}
}