1use std::error::Error;
7use std::fmt::{Display, Formatter, Debug};
8
9#[derive(Debug)]
11pub enum Scd41Error<E> {
12 I2c(E),
14 CrcMismatch {
16 expected: u8,
18 actual: u8,
20 bytes: [u8; 2],
22 },
23 DataNotReady,
25 RecalibrationFailed,
27}
28
29impl<E: Display> Display for Scd41Error<E> {
30 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
31 match self {
32 Scd41Error::I2c(error) => write!(f, "I2C communication error: {}", error),
33 Scd41Error::CrcMismatch {
34 expected,
35 actual,
36 bytes,
37 } => write!(
38 f,
39 "CRC mismatch: expected: {}, actual: {}, bytes: {:?}",
40 expected, actual, bytes
41 ),
42 Scd41Error::DataNotReady => write!(f, "Data not ready"),
43 Scd41Error::RecalibrationFailed => write!(f, "Recalibration failed"),
44 }
45 }
46}
47
48impl<E: Debug + Display> Error for Scd41Error<E> {}
49
50#[cfg(test)]
51mod tests {
52 use super::*;
53
54 #[test]
55 fn test_display() {
56 let error: Scd41Error<linux_embedded_hal::I2CError> = Scd41Error::CrcMismatch {
57 expected: 0x00,
58 actual: 0x01,
59 bytes: [0x00, 0x01],
60 };
61 assert_eq!(format!("{}", error), "CRC mismatch: expected: 0, actual: 1, bytes: [0, 1]");
62
63 let error: Scd41Error<linux_embedded_hal::I2CError> = Scd41Error::DataNotReady;
64 assert_eq!(format!("{}", error), "Data not ready");
65 }
66}