bnr_xfs/status/
transport.rs

1use std::fmt;
2
3use super::HardwareStatus;
4
5pub const TRANSPORT_CHANGED: u32 = 6167;
6pub const TRANSPORT_OK: u32 = 6203;
7pub const TRANSPORT_INOP: u32 = 6204;
8pub const TRANSPORT_UNKNOWN: u32 = 6206;
9
10/// Represents transport status values.
11#[repr(u32)]
12#[derive(Clone, Copy, Debug, Default, PartialEq, serde::Deserialize, serde::Serialize)]
13pub enum TransportStatus {
14    /// Indicates the transport status changed
15    Changed = TRANSPORT_CHANGED,
16    /// Indicates the transport is OK
17    Ok = TRANSPORT_OK,
18    /// Indicates the transport is inoperable
19    Inoperable = TRANSPORT_INOP,
20    /// Indicates the transport status is unknown
21    #[default]
22    Unknown = TRANSPORT_UNKNOWN,
23}
24
25impl TransportStatus {
26    /// Creates a new [TransportStatus].
27    pub const fn new() -> Self {
28        Self::Unknown
29    }
30
31    /// Creates a new [TransportStatus] from the provided parameter.
32    pub const fn create(val: u32) -> Self {
33        match val {
34            TRANSPORT_CHANGED => Self::Changed,
35            TRANSPORT_OK => Self::Ok,
36            TRANSPORT_INOP => Self::Inoperable,
37            TRANSPORT_UNKNOWN => Self::Unknown,
38            _ => Self::Unknown,
39        }
40    }
41}
42
43impl From<TransportStatus> for HardwareStatus {
44    fn from(val: TransportStatus) -> Self {
45        match val {
46            TransportStatus::Changed => Self::Notification,
47            TransportStatus::Ok => Self::Ok,
48            TransportStatus::Inoperable => Self::Error,
49            TransportStatus::Unknown => Self::Warning,
50        }
51    }
52}
53
54impl From<TransportStatus> for &'static str {
55    fn from(val: TransportStatus) -> Self {
56        match val {
57            TransportStatus::Changed => "changed",
58            TransportStatus::Ok => "OK",
59            TransportStatus::Inoperable => "inoperable",
60            TransportStatus::Unknown => "unknown",
61        }
62    }
63}
64
65impl From<&TransportStatus> for &'static str {
66    fn from(val: &TransportStatus) -> Self {
67        (*val).into()
68    }
69}
70
71impl fmt::Display for TransportStatus {
72    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
73        write!(f, r#""{}""#, <&str>::from(self))
74    }
75}
76
77impl_xfs_enum!(TransportStatus, "transportStatus");