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
/// Endianness of a [processor](crate::Cpu).
#[cfg_attr(
    feature = "serde",
    derive(serde::Deserialize, serde::Serialize),
    serde(rename_all = "kebab-case")
)]
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum Endian {
    /// Little endian.
    Little,
    /// Big endian.
    Big,
    /// Mixed endian.
    Selectable,
    /// Other
    Other,
}

impl Default for Endian {
    fn default() -> Self {
        Self::Little
    }
}

impl Endian {
    /// Parse a string into an [Endian] value, returning [`Option::None`] if the string is not valid.
    pub fn parse_str(s: &str) -> Option<Self> {
        match s {
            "little" => Some(Self::Little),
            "big" => Some(Self::Big),
            "selectable" => Some(Self::Selectable),
            "other" => Some(Self::Other),
            _ => None,
        }
    }

    /// Convert this [`Endian`] into a static string.
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::Little => "little",
            Self::Big => "big",
            Self::Selectable => "selectable",
            Self::Other => "other",
        }
    }
}