1use hidpp_transport::TransportError;
4
5#[derive(Debug, thiserror::Error)]
7pub enum ProtocolError {
8 #[error("transport error: {0}")]
10 Transport(#[from] TransportError),
11
12 #[error("HID++ error: {0}")]
14 HidppError(HidppErrorCode),
15
16 #[error("invalid response: {0}")]
18 InvalidResponse(String),
19
20 #[error("feature not supported: {0}")]
22 FeatureNotSupported(String),
23
24 #[error("not a HID++ device")]
26 NotHidppDevice,
27
28 #[error("operation timed out")]
30 Timeout,
31}
32
33#[derive(Debug, Clone, Copy, PartialEq, Eq)]
35pub enum HidppErrorCode {
36 NoError,
38 Unknown,
40 InvalidArgument,
42 OutOfRange,
44 HardwareError,
46 LogitechInternal,
48 InvalidFeatureIndex,
50 InvalidFunctionId,
52 Busy,
54 Unsupported,
56 UnknownCode(u8),
58}
59
60impl HidppErrorCode {
61 #[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
98pub type Result<T> = std::result::Result<T, ProtocolError>;