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

pub const CDR_POS_TOP: u32 = 1;
pub const CDR_POS_BOTTOM: u32 = 2;

/// Represents a CDR position
#[repr(u32)]
#[derive(Clone, Copy, Debug, Default, PartialEq, serde::Deserialize, serde::Serialize)]
pub enum CdrPosition {
    /// Inlet position
    Top = CDR_POS_TOP,
    /// Outlet position
    #[default]
    Bottom = CDR_POS_BOTTOM,
}

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

    /// Creates a new [CdrPosition] from the provided parameter.
    pub const fn create(val: u32) -> Self {
        match val {
            CDR_POS_BOTTOM => Self::Bottom,
            CDR_POS_TOP => Self::Top,
            _ => Self::Bottom,
        }
    }
}

impl From<&CdrPosition> for &'static str {
    fn from(val: &CdrPosition) -> Self {
        match val {
            CdrPosition::Bottom => "bottom",
            CdrPosition::Top => "top",
        }
    }
}

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

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

impl_xfs_enum!(CdrPosition, "position");