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
use std::fmt;

use super::HardwareStatus;

pub const SHT_CLOSED: u32 = 6198;
pub const SHT_OPEN: u32 = 6199;
pub const SHT_NOT_SUPPORTED: u32 = 6201;
pub const SHT_UNKNOWN: u32 = 6202;

/// Represents shutter status values.
#[repr(u32)]
#[derive(Clone, Copy, Debug, Default, PartialEq, serde::Deserialize, serde::Serialize)]
pub enum ShutterStatus {
    /// Indicates the shutter is closed
    Closed = SHT_CLOSED,
    /// Indicates the shutter status is not supported
    NotSupported = SHT_NOT_SUPPORTED,
    /// Indicates the shutter is open
    Open = SHT_OPEN,
    /// Indicates the shutter status is unknown
    #[default]
    Unknown = SHT_UNKNOWN,
}

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

    /// Creates a new [ShutterStatus] from the provided parameter.
    pub const fn create(val: u32) -> Self {
        match val {
            SHT_CLOSED => Self::Closed,
            SHT_NOT_SUPPORTED => Self::NotSupported,
            SHT_OPEN => Self::Open,
            SHT_UNKNOWN => Self::Unknown,
            _ => Self::Unknown,
        }
    }
}

impl From<ShutterStatus> for HardwareStatus {
    fn from(val: ShutterStatus) -> Self {
        match val {
            ShutterStatus::Closed | ShutterStatus::Open => Self::Notification,
            ShutterStatus::NotSupported => Self::Missing,
            ShutterStatus::Unknown => Self::Warning,
        }
    }
}

impl From<ShutterStatus> for &'static str {
    fn from(val: ShutterStatus) -> Self {
        match val {
            ShutterStatus::Closed => "closed",
            ShutterStatus::Open => "open",
            ShutterStatus::NotSupported => "not supported",
            ShutterStatus::Unknown => "unknown",
        }
    }
}

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

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

impl_xfs_enum!(ShutterStatus, "shutterStatus");