ruuvi_sensor_protocol/
errors.rs

1use core::fmt::{self, Display};
2
3#[cfg(feature = "std")]
4use std::error::Error;
5
6/// Errors which can occur during parsing of the manufacturer specific data
7#[derive(Clone, Debug, Eq, PartialEq)]
8pub enum ParseError {
9    /// Manufacturer id does not match expected value
10    UnknownManufacturerId(u16),
11    /// Format of the data is not supported by this crate
12    UnsupportedFormatVersion(u8),
13    /// Length of the value does not match expected length of the format
14    InvalidValueLength(u8, usize, usize),
15    /// Format can not be determined from value due to it being empty
16    EmptyValue,
17}
18
19impl Display for ParseError {
20    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
21        match self {
22            ParseError::UnknownManufacturerId(id) => write!(
23                formatter,
24                "Unknown manufacturer id {:#04X}, only 0x0499 is supported",
25                id
26            ),
27            ParseError::UnsupportedFormatVersion(format_version) => write!(
28                formatter,
29                "Unsupported data format version {}, only version 3 is supported",
30                format_version
31            ),
32            ParseError::InvalidValueLength(version, length, expected) => write!(
33                formatter,
34                "Invalid data length of {} for format version {}, expected {}",
35                length, version, expected
36            ),
37            ParseError::EmptyValue => write!(formatter, "Empty value, expected at least one byte"),
38        }
39    }
40}
41
42#[cfg(feature = "std")]
43impl Error for ParseError {}
44
45#[cfg(test)]
46mod tests {
47    use super::*;
48
49    #[test]
50    fn parse_error_has_default_traits() {
51        crate::testing::type_has_default_traits::<ParseError>();
52    }
53}