1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
//! Contains [`ErrorRoccatVulcanApi`]

use std::error::Error;
use std::fmt::{Display, Formatter};
use std::time::Duration;

/// Error returned by the API
#[derive(Debug)]
#[non_exhaustive]
pub enum ErrorRoccatVulcanApi {
    /// No keyboard detected.
    KeyboardNotFound,
    /// LED device not found
    NoLedDevice,
    /// LED device error
    LedDeviceError(hidapi::HidError),
    /// Control device not found
    NoControlDevice,
    /// Control device error
    ControlDeviceError(hidapi::HidError),
    /// Read device not found
    NoReadDevice,
    /// Read device error
    ReadDeviceError(hidapi::HidError),
    /// Too much time elapsed while wating for the device to be ready
    WaitedToMuchTime(Duration),
    /// error while trying the get the hdiapi.
    HidApiError(hidapi::HidError),
    /// Invalide input
    InvalidInput,
}

impl Display for ErrorRoccatVulcanApi {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::KeyboardNotFound => write!(f, "keyboard not found"),
            Self::NoLedDevice => write!(f, "led device not found"),
            Self::LedDeviceError(error) => write!(f, "led device error : {}", error),
            Self::NoControlDevice => write!(f, "control device not found"),
            Self::ControlDeviceError(error) => write!(f, "control device error : {}", error),
            Self::NoReadDevice => write!(f, "read device not found"),
            Self::ReadDeviceError(error) => write!(f, "read device error : {}", error),
            Self::WaitedToMuchTime(duration) => write!(
                f,
                "waited for too long, waited for {} ms",
                duration.as_secs_f64() * 1_000_f64
            ),
            Self::HidApiError(error) => write!(f, "hid api error : {}", error),
            Self::InvalidInput => write!(f, "invalide input"),
        }
    }
}

impl Error for ErrorRoccatVulcanApi {
    fn source(&self) -> Option<&(dyn Error + 'static)> {
        match self {
            Self::KeyboardNotFound
            | Self::NoLedDevice
            | Self::NoControlDevice
            | Self::NoReadDevice
            | Self::InvalidInput
            | Self::WaitedToMuchTime(_) => None,
            Self::LedDeviceError(error)
            | Self::ControlDeviceError(error)
            | Self::ReadDeviceError(error)
            | Self::HidApiError(error) => Some(error),
        }
    }
}