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