-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherror.rs
67 lines (63 loc) · 2.04 KB
/
error.rs
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
use crate::hal_type;
use core::fmt::{self, Display, Formatter};
/// Errors that can occur when using the FH101RF
pub enum Error<SPI>
where
SPI: hal_type::spi::ErrorType,
{
/// SPI error occured during a communication transaction
Communication(SPI::Error),
/// Wrong configuration error
Config(&'static str),
/// No data available
NoData,
/// GPIO error
Gpio,
/// Value does not exist
Value(&'static str),
/// Generic FH101RF error
Generic,
}
impl<SPI> Display for Error<SPI>
where
SPI: hal_type::spi::ErrorType,
SPI::Error: core::fmt::Debug,
{
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
write!(f, "{:?}", self)
}
}
// We can't derive this implementation, as the compiler will complain that the
// associated error type doesn't implement `Debug`.
impl<SPI> fmt::Debug for Error<SPI>
where
SPI: hal_type::spi::ErrorType,
SPI::Error: fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Error::Communication(error) => write!(f, "Communication error ({:?})", error),
Error::Config(s) => write!(f, "Configuration error ({:?})", s),
Error::NoData => write!(f, "No Data available error"),
Error::Gpio => write!(f, "Gpio error"),
Error::Value(s) => write!(f, "Value error ({:?})", s),
Error::Generic => write!(f, "Generic error"),
}
}
}
#[cfg(feature = "defmt")]
impl<SPI> defmt::Format for Error<SPI>
where
SPI: hal_type::spi::SpiDevice<u8>,
{
fn format(&self, f: defmt::Formatter) {
match self {
Error::Communication(_) => defmt::write!(f, "Communication error"),
Error::Config(s) => defmt::write!(f, "Configuration error ({:?})", s),
Error::NoData => defmt::write!(f, "No Data available error"),
Error::Gpio => defmt::write!(f, "Gpio error"),
Error::Value(s) => defmt::write!(f, "Value error ({:?})", s),
Error::Generic => defmt::write!(f, "Generic error"),
}
}
}