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
//! An enum to represent what endianness to read as

#[cfg(not(feature = "std"))]
use alloc::string::String;

/// An enum to represent what endianness to read as
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum Endian {
    Big,
    Little,
    Native,
}

pub use Endian::{
    Big as BE,
    Little as LE,
    Native as NE
};

impl From<&Endian> for String {
    fn from(endian: &Endian) -> String {
        String::from(
            match endian {
                Endian::Big => "Big",
                Endian::Little => "Little",
                Endian::Native => "Native",
            }
        )
    }
}

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

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn endian_to_string() {
        for &(ref endian, string) in [
            (Endian::Big, "Big"),
            (Endian::Little, "Little"),
            (Endian::Native, "Native"),
        ].iter() {
            let converted: String = endian.into();
            assert_eq!(converted, string)
        }
    }
}