bnr_xfs/status/
device.rs

1use std::fmt;
2
3use super::HardwareStatus;
4
5pub const DEVICE_STATUS_CHANGED: u32 = 6162;
6pub const HARDWARE_ERROR: u32 = 6174;
7pub const USER_ERROR: u32 = 6175;
8pub const OFF_LINE: u32 = 6179;
9pub const ON_LINE: u32 = 6180;
10
11/// Represents CDR device status values.
12#[repr(u32)]
13#[derive(Clone, Copy, Debug, Default, PartialEq, serde::Deserialize, serde::Serialize)]
14pub enum DeviceStatus {
15    /// Indicates a change in the device status
16    Changed = DEVICE_STATUS_CHANGED,
17    /// Indicates a hardware error occured
18    HardwareError = HARDWARE_ERROR,
19    /// Indicates a user error occured
20    UserError = USER_ERROR,
21    /// Device is offline
22    Offline = OFF_LINE,
23    #[default]
24    /// Device is online
25    Online = ON_LINE,
26}
27
28impl DeviceStatus {
29    /// Creates a new [DeviceStatus].
30    pub const fn new() -> Self {
31        Self::Online
32    }
33
34    /// Creates a new [DeviceStatus] from the provided parameter.
35    pub const fn create(val: u32) -> Self {
36        match val {
37            DEVICE_STATUS_CHANGED => Self::Changed,
38            HARDWARE_ERROR => Self::HardwareError,
39            USER_ERROR => Self::UserError,
40            OFF_LINE => Self::Offline,
41            ON_LINE => Self::Online,
42            _ => Self::HardwareError,
43        }
44    }
45}
46
47impl From<DeviceStatus> for HardwareStatus {
48    fn from(val: DeviceStatus) -> Self {
49        match val {
50            DeviceStatus::Online => Self::Ok,
51            DeviceStatus::Changed => Self::Notification,
52            DeviceStatus::Offline => Self::Missing,
53            DeviceStatus::HardwareError | DeviceStatus::UserError => Self::Error,
54        }
55    }
56}
57
58impl From<DeviceStatus> for &'static str {
59    fn from(val: DeviceStatus) -> Self {
60        match val {
61            DeviceStatus::Changed => "changed",
62            DeviceStatus::HardwareError => "hardware error",
63            DeviceStatus::UserError => "user error",
64            DeviceStatus::Offline => "offline",
65            DeviceStatus::Online => "online",
66        }
67    }
68}
69
70impl From<&DeviceStatus> for &'static str {
71    fn from(val: &DeviceStatus) -> Self {
72        (*val).into()
73    }
74}
75
76impl fmt::Display for DeviceStatus {
77    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
78        write!(f, "{}", <&str>::from(self))
79    }
80}
81
82impl_xfs_enum!(DeviceStatus, "deviceStatus");