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
use crate::error::Error;
use core::{fmt, str::FromStr};
#[cfg(feature = "serde")]
use serde::{de, de::Error as DeError, ser, Deserialize, Serialize};
#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)]
#[non_exhaustive]
pub enum Endian {
Big,
Little,
}
impl Endian {
pub fn as_str(self) -> &'static str {
match self {
Endian::Big => "big",
Endian::Little => "little",
}
}
}
impl FromStr for Endian {
type Err = Error;
fn from_str(name: &str) -> Result<Self, Self::Err> {
let result = match name {
"big" => Endian::Big,
"little" => Endian::Little,
_ => return Err(Error),
};
Ok(result)
}
}
impl fmt::Display for Endian {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
#[cfg(feature = "serde")]
impl Serialize for Endian {
fn serialize<S: ser::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
serializer.serialize_str(self.as_str())
}
}
#[cfg(all(feature = "serde", feature = "std"))]
impl<'de> Deserialize<'de> for Endian {
fn deserialize<D: de::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
let string = std::string::String::deserialize(deserializer)?;
string.parse().map_err(|_| {
D::Error::custom(std::format!(
"Unrecognized value '{}' for target_endian",
string
))
})
}
}