aranet_types/
error.rs

1//! Error types for data parsing in aranet-types.
2
3use thiserror::Error;
4
5/// Errors that can occur when parsing Aranet sensor data.
6///
7/// This error type is platform-agnostic and does not include
8/// BLE-specific errors (those belong in aranet-core).
9///
10/// This enum is marked `#[non_exhaustive]` to allow adding new error variants
11/// in future versions without breaking downstream code.
12#[derive(Debug, Clone, PartialEq, Eq, Error)]
13#[non_exhaustive]
14pub enum ParseError {
15    /// Failed to parse data due to insufficient bytes.
16    #[error("Insufficient bytes: expected {expected}, got {actual}")]
17    InsufficientBytes {
18        /// Expected number of bytes.
19        expected: usize,
20        /// Actual number of bytes received.
21        actual: usize,
22    },
23
24    /// Invalid or unrecognized value encountered during parsing.
25    ///
26    /// This variant is used for any value that doesn't meet validation
27    /// requirements (e.g., humidity > 100, temperature out of range).
28    #[error("Invalid value: {0}")]
29    InvalidValue(String),
30
31    /// Unknown device type byte value.
32    #[error("Unknown device type: 0x{0:02X}")]
33    UnknownDeviceType(u8),
34}
35
36impl ParseError {
37    /// Create an `InvalidValue` error with a descriptive message.
38    ///
39    /// This is a convenience constructor for the common case of invalid data.
40    #[must_use]
41    pub fn invalid_value(message: impl Into<String>) -> Self {
42        Self::InvalidValue(message.into())
43    }
44}
45
46/// Result type alias using aranet-types' [`ParseError`] type.
47pub type ParseResult<T> = core::result::Result<T, ParseError>;