scd30_interface/
error.rs

1//! Errors emitted by this library.
2
3use embedded_hal::i2c;
4use thiserror::Error;
5
6/// Error variants emitted by this library.
7#[derive(Debug, Error, PartialEq)]
8pub enum Scd30Error<I2cErr: i2c::Error> {
9    /// Emitted when an error handling the data has occurred.
10    #[error(transparent)]
11    DataError(#[from] DataError),
12    /// Emitted when an error with the underlying I2C bus has occurred.
13    #[error(transparent)]
14    I2cError(#[from] I2cErr),
15    /// Emitted when the argument intended to be sent to the sensor is bigger than 16-bits. Should
16    /// only occur if modifications to this library where made that send such data.
17    #[error("Only 16-bits of data can be send")]
18    SentDataToBig,
19}
20
21#[cfg(feature = "defmt")]
22impl<I2cErr: i2c::Error> defmt::Format for Scd30Error<I2cErr> {
23    fn format(&self, f: defmt::Formatter) {
24        defmt::write!(f, "{}", self)
25    }
26}
27
28/// Error variants handling data errors.
29#[derive(Debug, Error, PartialEq)]
30pub enum DataError {
31    /// Emitted when a value is used to construct data send to the sensor, but the value is not in
32    /// the specified value's range. Adjust the argument to a value within the specified bounds.
33    #[error("{parameter} must be between {min} and {max} {unit}.")]
34    ValueOutOfRange {
35        /// Name of the parameter
36        parameter: &'static str,
37        /// Lower limit of the value
38        min: u16,
39        /// Upper limit of the value
40        max: u16,
41        /// Unit of the value
42        unit: &'static str,
43    },
44    /// Emitted when the ambient pressure compensation is set to 0. Use either no value or the
45    /// [DefaultPressure](crate::data::AmbientPressureCompensation::DefaultPressure) enum variant.
46    #[error("Instead of setting the ambient pressure compensation to 0, use AmbientPressureCompensation::DefaultPressure.")]
47    UseDefaultPressure,
48    /// Emitted when the CRC check for received data fails.
49    #[error("CRC check failed.")]
50    CrcFailed,
51    /// Emitted when data received does not match the expected data size.
52    #[error("Buffer size received to wrong size for expected data.")]
53    ReceivedBufferWrongSize,
54    /// Emitted when a enum value received is not within the expected value range. Could occur if
55    /// the firmware of the sensor has received updates.
56    #[error("Unexpected Value for {parameter}: expected {expected} got {actual}")]
57    UnexpectedValueReceived {
58        /// Name of the parameter
59        parameter: &'static str,
60        /// Description of the expected value range
61        expected: &'static str,
62        /// Actual value received
63        actual: u16,
64    },
65}
66
67#[cfg(feature = "defmt")]
68impl defmt::Format for DataError {
69    fn format(&self, f: defmt::Formatter) {
70        defmt::write!(f, "{}", self)
71    }
72}