logiops_core/
error.rs

1//! Error types for the logiops-core crate.
2
3use hidpp_transport::TransportError;
4
5/// Errors that can occur during HID++ protocol operations.
6#[derive(Debug, thiserror::Error)]
7pub enum ProtocolError {
8    /// Transport-level error (HID communication failed).
9    #[error("transport error: {0}")]
10    Transport(#[from] TransportError),
11
12    /// Device returned an error response.
13    #[error("HID++ error: {0}")]
14    HidppError(HidppErrorCode),
15
16    /// Invalid response from device.
17    #[error("invalid response: {0}")]
18    InvalidResponse(String),
19
20    /// Feature not supported by device.
21    #[error("feature not supported: {0}")]
22    FeatureNotSupported(String),
23
24    /// Device is not a HID++ device.
25    #[error("not a HID++ device")]
26    NotHidppDevice,
27
28    /// Operation timed out.
29    #[error("operation timed out")]
30    Timeout,
31}
32
33/// HID++ error codes returned by devices.
34#[derive(Debug, Clone, Copy, PartialEq, Eq)]
35pub enum HidppErrorCode {
36    /// No error.
37    NoError,
38    /// Unknown command/feature.
39    Unknown,
40    /// Invalid argument.
41    InvalidArgument,
42    /// Out of range.
43    OutOfRange,
44    /// Hardware error.
45    HardwareError,
46    /// Logitech internal.
47    LogitechInternal,
48    /// Invalid feature index.
49    InvalidFeatureIndex,
50    /// Invalid function ID.
51    InvalidFunctionId,
52    /// Busy.
53    Busy,
54    /// Unsupported.
55    Unsupported,
56    /// Unknown error code.
57    UnknownCode(u8),
58}
59
60impl HidppErrorCode {
61    /// Parses error code from byte.
62    #[must_use]
63    pub fn from_byte(code: u8) -> Self {
64        match code {
65            0x00 => Self::NoError,
66            0x01 => Self::Unknown,
67            0x02 => Self::InvalidArgument,
68            0x03 => Self::OutOfRange,
69            0x04 => Self::HardwareError,
70            0x05 => Self::LogitechInternal,
71            0x06 => Self::InvalidFeatureIndex,
72            0x07 => Self::InvalidFunctionId,
73            0x08 => Self::Busy,
74            0x09 => Self::Unsupported,
75            code => Self::UnknownCode(code),
76        }
77    }
78}
79
80impl std::fmt::Display for HidppErrorCode {
81    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
82        match self {
83            Self::NoError => write!(f, "no error"),
84            Self::Unknown => write!(f, "unknown command"),
85            Self::InvalidArgument => write!(f, "invalid argument"),
86            Self::OutOfRange => write!(f, "out of range"),
87            Self::HardwareError => write!(f, "hardware error"),
88            Self::LogitechInternal => write!(f, "logitech internal"),
89            Self::InvalidFeatureIndex => write!(f, "invalid feature index"),
90            Self::InvalidFunctionId => write!(f, "invalid function ID"),
91            Self::Busy => write!(f, "busy"),
92            Self::Unsupported => write!(f, "unsupported"),
93            Self::UnknownCode(code) => write!(f, "unknown error code: 0x{code:02X}"),
94        }
95    }
96}
97
98/// Result type alias for protocol operations.
99pub type Result<T> = std::result::Result<T, ProtocolError>;