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
70
71
72
73
74
75
76
77
78
79
80
81
82
use std::fmt;

use super::HardwareStatus;

pub const DEVICE_STATUS_CHANGED: u32 = 6162;
pub const HARDWARE_ERROR: u32 = 6174;
pub const USER_ERROR: u32 = 6175;
pub const OFF_LINE: u32 = 6179;
pub const ON_LINE: u32 = 6180;

/// Represents CDR device status values.
#[repr(u32)]
#[derive(Clone, Copy, Debug, Default, PartialEq, serde::Deserialize, serde::Serialize)]
pub enum DeviceStatus {
    /// Indicates a change in the device status
    Changed = DEVICE_STATUS_CHANGED,
    /// Indicates a hardware error occured
    HardwareError = HARDWARE_ERROR,
    /// Indicates a user error occured
    UserError = USER_ERROR,
    /// Device is offline
    Offline = OFF_LINE,
    #[default]
    /// Device is online
    Online = ON_LINE,
}

impl DeviceStatus {
    /// Creates a new [DeviceStatus].
    pub const fn new() -> Self {
        Self::Online
    }

    /// Creates a new [DeviceStatus] from the provided parameter.
    pub const fn create(val: u32) -> Self {
        match val {
            DEVICE_STATUS_CHANGED => Self::Changed,
            HARDWARE_ERROR => Self::HardwareError,
            USER_ERROR => Self::UserError,
            OFF_LINE => Self::Offline,
            ON_LINE => Self::Online,
            _ => Self::HardwareError,
        }
    }
}

impl From<DeviceStatus> for HardwareStatus {
    fn from(val: DeviceStatus) -> Self {
        match val {
            DeviceStatus::Online => Self::Ok,
            DeviceStatus::Changed => Self::Notification,
            DeviceStatus::Offline => Self::Missing,
            DeviceStatus::HardwareError | DeviceStatus::UserError => Self::Error,
        }
    }
}

impl From<DeviceStatus> for &'static str {
    fn from(val: DeviceStatus) -> Self {
        match val {
            DeviceStatus::Changed => "changed",
            DeviceStatus::HardwareError => "hardware error",
            DeviceStatus::UserError => "user error",
            DeviceStatus::Offline => "offline",
            DeviceStatus::Online => "online",
        }
    }
}

impl From<&DeviceStatus> for &'static str {
    fn from(val: &DeviceStatus) -> Self {
        (*val).into()
    }
}

impl fmt::Display for DeviceStatus {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", <&str>::from(self))
    }
}

impl_xfs_enum!(DeviceStatus, "deviceStatus");