max7219_display/
error.rs

1//! Error types for MAX7219 driver
2
3/// Errors that can occur when using the MAX7219 driver
4#[derive(Debug, PartialEq, Eq)]
5pub enum Error {
6    /// The specified device count is invalid (exceeds maximum allowed).
7    InvalidDeviceCount,
8    /// Invalid scan limit value (must be 0-7)
9    InvalidScanLimit,
10    /// The specified register address is not valid for the MAX7219.
11    InvalidRegister,
12    /// Invalid device index (exceeds configured number of devices)
13    InvalidDeviceIndex,
14    /// Invalid digit position (0-7 for MAX7219)
15    InvalidDigit,
16    /// Invalid intensity value (must be 0-15)
17    InvalidIntensity,
18    /// Unsupported Character
19    UnsupportedChar,
20    /// Buffer Error
21    BufferError,
22    /// SPI communication error
23    SpiError,
24}
25
26impl core::fmt::Display for Error {
27    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
28        match self {
29            Self::SpiError => write!(f, "SPI communication error"),
30            Self::InvalidDeviceIndex => write!(f, "Invalid device index"),
31            Self::InvalidDigit => write!(f, "Invalid digit"),
32            Self::InvalidIntensity => write!(f, "Invalid intensity value"),
33            Self::InvalidScanLimit => write!(f, "Invalid scan limit value"),
34            Self::InvalidDeviceCount => write!(f, "Invalid device count"),
35            Self::InvalidRegister => write!(f, "Invalid register address"),
36            Self::UnsupportedChar => write!(f, "Unsupported Character"),
37            Self::BufferError => write!(f, "LED Matrix buffer error"),
38        }
39    }
40}
41
42/// Convert any embedded-hal SPI error into a general `SpiError`.
43///
44/// This allows using the `?` operator with SPI operations, automatically
45/// mapping their error into the driver's unified [`Error`] type.
46impl<E> From<E> for Error
47where
48    E: embedded_hal::spi::Error,
49{
50    fn from(_value: E) -> Self {
51        Self::SpiError
52    }
53}
54
55#[cfg(test)]
56mod tests {
57    use super::*;
58
59    // Mock SPI error for testing
60    #[derive(Debug)]
61    struct MockSpiError;
62
63    impl core::fmt::Display for MockSpiError {
64        fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
65            write!(f, "Mock SPI error")
66        }
67    }
68
69    impl embedded_hal::spi::Error for MockSpiError {
70        fn kind(&self) -> embedded_hal::spi::ErrorKind {
71            embedded_hal::spi::ErrorKind::Other
72        }
73    }
74
75    #[test]
76    fn test_error_device() {
77        assert_eq!(
78            format!("{}", Error::InvalidDeviceCount),
79            "Invalid device count"
80        );
81        assert_eq!(
82            format!("{}", Error::InvalidScanLimit),
83            "Invalid scan limit value"
84        );
85        assert_eq!(
86            format!("{}", Error::InvalidRegister),
87            "Invalid register address"
88        );
89        assert_eq!(
90            format!("{}", Error::InvalidDeviceIndex),
91            "Invalid device index"
92        );
93        assert_eq!(format!("{}", Error::InvalidDigit), "Invalid digit");
94        assert_eq!(
95            format!("{}", Error::InvalidIntensity),
96            "Invalid intensity value"
97        );
98        assert_eq!(
99            format!("{}", Error::UnsupportedChar),
100            "Unsupported Character"
101        );
102        assert_eq!(format!("{}", Error::BufferError), "LED Matrix buffer error");
103        assert_eq!(format!("{}", Error::SpiError), "SPI communication error");
104    }
105
106    #[test]
107    fn test_error_debug() {
108        // Test that Debug trait is implemented and works
109        let error = Error::InvalidDigit;
110        let debug_output = format!("{error:?}",);
111        assert!(debug_output.contains("InvalidDigit"));
112    }
113
114    #[test]
115    fn test_from_spi_error() {
116        let spi_error = MockSpiError;
117        let error = Error::from(spi_error);
118        assert_eq!(error, Error::SpiError);
119    }
120
121    #[test]
122    fn test_error_partialeq() {
123        // Test that all variants implement PartialEq correctly
124        assert!(Error::InvalidDeviceCount.eq(&Error::InvalidDeviceCount));
125        assert!(!Error::InvalidDeviceCount.eq(&Error::InvalidScanLimit));
126    }
127}