1#[derive(Debug, PartialEq, Eq)]
5pub enum Error {
6 InvalidDeviceCount,
8 InvalidScanLimit,
10 InvalidRegister,
12 InvalidDeviceIndex,
14 InvalidDigit,
16 InvalidIntensity,
18 UnsupportedChar,
20 BufferError,
22 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
42impl<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 #[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 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 assert!(Error::InvalidDeviceCount.eq(&Error::InvalidDeviceCount));
125 assert!(!Error::InvalidDeviceCount.eq(&Error::InvalidScanLimit));
126 }
127}