scd4x_rs/
error.rs

1// Copyright Claudio Mattera 2024.
2//
3// Distributed under the MIT License or the Apache 2.0 License at your option.
4// See the accompanying files License-MIT.txt and License-Apache-2.0.txt, or
5// online at
6// https://opensource.org/licenses/MIT
7// https://opensource.org/licenses/Apache-2.0
8
9//! Data types and functions for error handling
10
11use embedded_hal::i2c::Error as I2cError;
12use embedded_hal::i2c::ErrorKind as I2cErrorKind;
13
14/// An error
15#[derive(Debug, PartialEq)]
16pub enum Error {
17    /// A checksum was different than expected
18    ChecksumMismatch {
19        /// Actual checksum
20        actual: u8,
21
22        /// Expected checksum
23        expected: u8,
24    },
25
26    /// An error in the  underlying I²C system
27    I2c(I2cErrorKind),
28}
29
30impl<E> From<E> for Error
31where
32    E: I2cError,
33{
34    fn from(error: E) -> Self {
35        Self::I2c(error.kind())
36    }
37}
38
39#[cfg(feature = "std")]
40impl std::error::Error for Error {}
41
42#[cfg(feature = "std")]
43impl core::fmt::Display for Error {
44    fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
45        write!(f, "{self:?}")
46    }
47}