1use super::HidDeviceInfo;
8use libc::wchar_t;
9use std::error::Error;
10use std::fmt::{Display, Formatter, Result};
11
12#[derive(Debug)]
13pub enum HidError {
14 HidApiError {
15 message: String,
16 },
17 #[deprecated]
18 HidApiErrorEmptyWithCause {
19 cause: Box<dyn Error + Send + Sync>,
20 },
21 HidApiErrorEmpty,
22 FromWideCharError {
23 wide_char: wchar_t,
24 },
25 InitializationError,
26 #[deprecated]
27 OpenHidDeviceError,
28 InvalidZeroSizeData,
29 IncompleteSendError {
30 sent: usize,
31 all: usize,
32 },
33 SetBlockingModeError {
34 mode: &'static str,
35 },
36 OpenHidDeviceWithDeviceInfoError {
37 device_info: Box<HidDeviceInfo>,
38 },
39}
40
41impl Display for HidError {
42 fn fmt(&self, f: &mut Formatter<'_>) -> Result {
43 match self {
44 HidError::HidApiError { message } => write!(f, "hidapi error: {}", message),
45 HidError::HidApiErrorEmptyWithCause { cause } => write!(
46 f,
47 "hidapi error: (could not get error message), caused by: {}",
48 cause
49 ),
50 HidError::HidApiErrorEmpty => write!(f, "hidapi error: (could not get error message)"),
51 HidError::FromWideCharError { wide_char } => {
52 write!(f, "failed converting {:#X} to rust char", wide_char)
53 }
54 HidError::InitializationError => {
55 write!(f, "Failed to initialize hidapi (maybe initialized before?)")
56 }
57 HidError::OpenHidDeviceError => write!(f, "Failed opening hid device"),
58 HidError::InvalidZeroSizeData => write!(f, "Invalid data: size can not be 0"),
59 HidError::IncompleteSendError { sent, all } => write!(
60 f,
61 "Failed to send all data: only sent {} out of {} bytes",
62 sent, all
63 ),
64 HidError::SetBlockingModeError { mode } => {
65 write!(f, "Can not set blocking mode to '{}'", mode)
66 }
67 HidError::OpenHidDeviceWithDeviceInfoError { device_info } => {
68 write!(f, "Can not open hid device with: {:?}", *device_info)
69 }
70 }
71 }
72}
73
74impl Error for HidError {
75 fn source(&self) -> Option<&(dyn Error + 'static)> {
76 match self {
77 HidError::HidApiErrorEmptyWithCause { cause } => Some(cause.as_ref()),
78 _ => None,
79 }
80 }
81}