1use std::fmt::{Display, Formatter};
11use std::str::FromStr;
12
13use serde::{Deserialize, Serialize};
14
15pub use crate::ParseError;
16
17#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq, Hash, Eq)]
22#[serde(rename_all = "lowercase")]
23pub enum Architecture {
24 All,
26 Alpha,
28 Amd64,
30 Arc,
32 Arm64,
34 Armel,
36 Armhf,
38 Hppa,
40 #[serde(rename = "hurd-amd64")]
42 HurdAmd64,
43 #[serde(rename = "hurd-i386")]
45 HurdI386,
46 I386,
48 Ia64,
50 Loong64,
52 M68k,
54 Mips64el,
56 Mipsel,
58 PowerPC,
60 Ppc64,
62 Ppc64el,
64 Riscv64,
66 S390x,
68 Sh4,
70 Sparc64,
72 X32,
74 Source,
76}
77
78impl Display for Architecture {
79 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
80 write!(f, "{}", self.as_ref())
81 }
82}
83
84impl AsRef<str> for Architecture {
85 fn as_ref(&self) -> &str {
86 match self {
87 Self::All => "all",
88 Self::Alpha => "alpha",
89 Self::Amd64 => "amd64",
90 Self::Arc => "arc",
91 Self::Arm64 => "arm64",
92 Self::Armel => "armel",
93 Self::Armhf => "armhf",
94 Self::Hppa => "hppa",
95 Self::HurdAmd64 => "hurd-amd64",
96 Self::HurdI386 => "hurd-i386",
97 Self::I386 => "i386",
98 Self::Ia64 => "ia64",
99 Self::Loong64 => "loong64",
100 Self::M68k => "m68k",
101 Self::Mips64el => "mips64el",
102 Self::Mipsel => "mipsel",
103 Self::PowerPC => "powerpc",
104 Self::Ppc64 => "ppc64",
105 Self::Ppc64el => "ppc64el",
106 Self::Riscv64 => "riscv64",
107 Self::S390x => "s390x",
108 Self::Sh4 => "sh4",
109 Self::Sparc64 => "sparc64",
110 Self::X32 => "x32",
111 Self::Source => "source",
112 }
113 }
114}
115
116impl TryFrom<&str> for Architecture {
117 type Error = ParseError;
118
119 fn try_from(value: &str) -> Result<Self, Self::Error> {
120 match value {
121 "all" => Ok(Self::All),
122 "alpha" => Ok(Self::Alpha),
123 "amd64" => Ok(Self::Amd64),
124 "arc" => Ok(Self::Arc),
125 "arm64" => Ok(Self::Arm64),
126 "armel" => Ok(Self::Armel),
127 "armhf" => Ok(Self::Armhf),
128 "hppa" => Ok(Self::Hppa),
129 "hurd-amd64" => Ok(Self::HurdAmd64),
130 "hurd-i386" => Ok(Self::HurdI386),
131 "i386" => Ok(Self::I386),
132 "ia64" => Ok(Self::Ia64),
133 "loong64" => Ok(Self::Loong64),
134 "m68k" => Ok(Self::M68k),
135 "mips64el" => Ok(Self::Mips64el),
136 "mipsel" => Ok(Self::Mipsel),
137 "powerpc" => Ok(Self::PowerPC),
138 "ppc64" => Ok(Self::Ppc64),
139 "ppc64el" => Ok(Self::Ppc64el),
140 "riscv64" => Ok(Self::Riscv64),
141 "s390x" => Ok(Self::S390x),
142 "sh4" => Ok(Self::Sh4),
143 "sparc64" => Ok(Self::Sparc64),
144 "x32" => Ok(Self::X32),
145 "source" => Ok(Self::Source),
146 _ => Err(ParseError::InvalidArchitecture),
147 }
148 }
149}
150
151impl FromStr for Architecture {
152 type Err = ParseError;
153
154 fn from_str(s: &str) -> Result<Self, Self::Err> {
155 Self::try_from(s)
156 }
157}
158
159#[cfg(test)]
160mod test {
161 use super::Architecture;
162
163 #[test]
164 fn from_str() {
165 assert_eq!(
166 Architecture::try_from("amd64").unwrap(),
167 Architecture::Amd64
168 );
169 }
170}