-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror.rs
More file actions
274 lines (250 loc) · 8.74 KB
/
Copy patherror.rs
File metadata and controls
274 lines (250 loc) · 8.74 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
use core::fmt;
/// Defines the category of an error.
#[derive(Clone, Copy, PartialEq, Eq)]
pub enum ErrorKind {
/// Errors related to the UART peripheral.
Uart(UartError),
/// Errors related to the I2C peripheral.
I2c(I2cError),
/// Errors related to the SPI peripheral.
Spi(SpiError),
/// Errors related to the GPIO peripheral.
Gpio(GpioError),
/// Errors related to the ADC peripheral.
Adc(AdcError),
/// Hardware-level faults such as power, short circuits, etc.
Hardware(HardwareError),
/// Errors related to buffers or data structures.
Buffer(BufferError),
/// An invalid configuration or an unsupported setup.
InvalidConfig,
/// An unknown error that cannot be categorized.
Unknown,
/// Other external or custom errors.
Other,
}
#[derive(Clone, Copy, PartialEq, Eq)]
pub enum UartError {
/// A framing error occurred.
Framing,
/// A parity error occurred.
Parity,
/// An overrun error occurred.
Overrun,
/// An underrun error occurred.
Underrun,
/// A timeout occurred during an operation.
Timeout,
}
impl fmt::Display for UartError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
UartError::Framing => f.write_str("Framing"),
UartError::Parity => f.write_str("Parity"),
UartError::Overrun => f.write_str("Overrun"),
UartError::Underrun => f.write_str("Underrun"),
UartError::Timeout => f.write_str("Timeout"),
}
}
}
#[derive(Clone, Copy, PartialEq, Eq)]
pub enum I2cError {
/// A NACK (No Acknowledgment) was received from a device.
Nack,
/// Arbitration was lost during an I2C transaction.
ArbitrationLost,
/// A general bus error occurred.
Bus,
}
impl fmt::Display for I2cError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
I2cError::Nack => f.write_str("Nack"),
I2cError::ArbitrationLost => f.write_str("ArbitrationLost"),
I2cError::Bus => f.write_str("Bus"),
}
}
}
#[derive(Clone, Copy, PartialEq, Eq)]
pub enum SpiError {
/// A mode fault occurred on the SPI bus.
ModeFault,
}
impl fmt::Display for SpiError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
SpiError::ModeFault => f.write_str("ModeFault"),
}
}
}
#[derive(Clone, Copy, PartialEq, Eq)]
pub enum GpioError {
/// An invalid state was detected for a GPIO pin.
InvalidState,
}
impl fmt::Display for GpioError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
GpioError::InvalidState => f.write_str("InvalidState"),
}
}
}
#[derive(Clone, Copy, PartialEq, Eq)]
pub enum AdcError {
/// The ADC reading is out of its valid range.
OutOfRange,
}
impl fmt::Display for AdcError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
AdcError::OutOfRange => f.write_str("OutOfRange"),
}
}
}
#[derive(Clone, Copy, PartialEq, Eq)]
pub enum HardwareError {
/// A power fault was detected.
Power,
/// A peripheral-related fault occurred.
Peripheral,
/// A short circuit was detected.
ShortCircuit,
/// An open circuit was detected.
OpenCircuit,
}
impl fmt::Display for HardwareError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
HardwareError::Power => f.write_str("PowerFault"),
HardwareError::Peripheral => f.write_str("PeripheralFault"),
HardwareError::ShortCircuit => f.write_str("ShortCircuit"),
HardwareError::OpenCircuit => f.write_str("OpenCircuit"),
}
}
}
#[derive(Clone, Copy, PartialEq, Eq)]
pub enum BufferError {
/// A buffer overflow occurred.
Overflow,
/// A buffer underflow occurred.
Underflow,
}
impl fmt::Display for BufferError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
BufferError::Overflow => f.write_str("Overflow"),
BufferError::Underflow => f.write_str("Underflow"),
}
}
}
impl fmt::Display for ErrorKind {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
ErrorKind::Uart(e) => write!(f, "Uart: {e}"),
ErrorKind::I2c(e) => write!(f, "I2c: {e}"),
ErrorKind::Spi(e) => write!(f, "Spi: {e}"),
ErrorKind::Gpio(e) => write!(f, "Gpio: {e}"),
ErrorKind::Adc(e) => write!(f, "Adc: {e}"),
ErrorKind::Hardware(e) => write!(f, "Hardware: {e}"),
ErrorKind::Buffer(e) => write!(f, "Buffer: {e}"),
ErrorKind::InvalidConfig => f.write_str("InvalidConfig"),
ErrorKind::Unknown => f.write_str("Unknown"),
ErrorKind::Other => f.write_str("Other"),
}
}
}
/// Errors that can occur within the BitFlags utility.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BitFlagsError {
/// An index is out of bounds for the bit flags.
IndexOutOfBounds { idx: usize, max: usize },
}
impl fmt::Display for BitFlagsError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
BitFlagsError::IndexOutOfBounds { idx, max } => {
write!(
f,
"Index out of bounds: index {idx} is out of range 0..{max}"
)
}
}
}
}
/// Errors that can occur during the exploration of command sequences.
#[derive(PartialEq, Eq)]
pub enum ExplorerError {
/// The provided sequence contained more commands than supported by the capacity.
TooManyCommands,
/// The command dependency graph contains a cycle.
DependencyCycle,
/// No valid I2C addresses were found for any command sequence.
NoValidAddressesFound,
/// An I2C command execution failed.
ExecutionFailed(ErrorKind),
/// An internal buffer overflowed during the exploration process.
BufferOverflow,
/// A dependency index is out of bounds.
InvalidDependencyIndex,
/// An I2C device was not found during a scan operation.
DeviceNotFound(ErrorKind),
/// An error occurred in the BitFlags utility.
BitFlags(BitFlagsError),
}
impl fmt::Display for ExplorerError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
ExplorerError::TooManyCommands => f.write_str("TooManyCommands"),
ExplorerError::DependencyCycle => f.write_str("DependencyCycle"),
ExplorerError::NoValidAddressesFound => f.write_str("NoValidAddressesFound"),
ExplorerError::ExecutionFailed(kind) => write!(f, "ExecutionFailed: {kind}"),
ExplorerError::BufferOverflow => f.write_str("BufferOverflow"),
ExplorerError::InvalidDependencyIndex => f.write_str("InvalidDependencyIndex"),
ExplorerError::DeviceNotFound(kind) => write!(f, "DeviceNotFound: {kind}"),
ExplorerError::BitFlags(e) => write!(f, "BitFlagsError: {e}"),
}
}
}
/// Errors that can occur during command execution.
#[derive(PartialEq, Eq)]
pub enum ExecutorError {
/// A command failed to execute due to an I2C error.
I2cError(ErrorKind),
/// The command failed to execute (e.g., NACK, I/O error).
ExecFailed,
/// An internal buffer overflowed during command preparation.
BufferOverflow,
/// An error occurred in the BitFlags utility.
BitFlags(BitFlagsError),
/// An error occurred in the explorer module.
Explorer(ExplorerError),
}
/// Converts an `ExecutorError` into an `ExplorerError`.
impl From<ExecutorError> for ExplorerError {
fn from(error: ExecutorError) -> Self {
match error {
ExecutorError::I2cError(kind) => ExplorerError::ExecutionFailed(kind),
ExecutorError::ExecFailed => ExplorerError::ExecutionFailed(ErrorKind::Unknown),
ExecutorError::BufferOverflow => ExplorerError::BufferOverflow,
ExecutorError::BitFlags(e) => ExplorerError::BitFlags(e),
ExecutorError::Explorer(e) => e,
}
}
}
/// Converts an `ErrorKind` into an `ExplorerError`.
impl From<ErrorKind> for ExplorerError {
fn from(error: ErrorKind) -> Self {
ExplorerError::DeviceNotFound(error)
}
}
impl fmt::Display for ExecutorError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
ExecutorError::I2cError(kind) => write!(f, "I2cError: {kind}"),
ExecutorError::ExecFailed => f.write_str("ExecFailed"),
ExecutorError::BufferOverflow => f.write_str("BufferOverflow"),
ExecutorError::BitFlags(e) => write!(f, "BitFlagsError: {e}"),
ExecutorError::Explorer(e) => write!(f, "ExplorerError: {e}"),
}
}
}