w25q/
error.rs

1use core::fmt::{self, Debug, Display};
2use embedded_hal::blocking::spi::Transfer;
3use embedded_hal::digital::v2::OutputPin;
4
5mod private {
6    #[derive(Debug)]
7    pub enum Private {}
8}
9
10/// The error type used by this library.
11///
12/// This can encapsulate an SPI or GPIO error, and adds its own protocol errors
13/// on top of that.
14pub enum Error<SPI: Transfer<u8>, GPIO: OutputPin> {
15    /// An SPI transfer failed.
16    Spi(SPI::Error),
17
18    /// A GPIO could not be set.
19    Gpio(GPIO::Error),
20
21    /// Status register contained unexpected flags.
22    ///
23    /// This can happen when the chip is faulty, incorrectly connected, or the
24    /// driver wasn't constructed or destructed properly (eg. while there is
25    /// still a write in progress).
26    UnexpectedStatus,
27
28    #[doc(hidden)]
29    __NonExhaustive(private::Private),
30}
31
32impl<SPI: Transfer<u8>, GPIO: OutputPin> Debug for Error<SPI, GPIO>
33where
34    SPI::Error: Debug,
35    GPIO::Error: Debug,
36{
37    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
38        match self {
39            Error::Spi(spi) => write!(f, "Error::Spi({:?})", spi),
40            Error::Gpio(gpio) => write!(f, "Error::Gpio({:?})", gpio),
41            Error::UnexpectedStatus => f.write_str("Error::UnexpectedStatus"),
42            Error::__NonExhaustive(_) => unreachable!(),
43        }
44    }
45}
46
47impl<SPI: Transfer<u8>, GPIO: OutputPin> Display for Error<SPI, GPIO>
48where
49    SPI::Error: Display,
50    GPIO::Error: Display,
51{
52    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
53        match self {
54            Error::Spi(spi) => write!(f, "SPI error: {}", spi),
55            Error::Gpio(gpio) => write!(f, "GPIO error: {}", gpio),
56            Error::UnexpectedStatus => f.write_str("unexpected value in status register"),
57            Error::__NonExhaustive(_) => unreachable!(),
58        }
59    }
60}