use std::fmt::{Display, Formatter};
use std::str::FromStr;
use serde::{Deserialize, Serialize};
pub use crate::ParseError;
#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq, Hash, Eq)]
#[serde(rename_all = "lowercase")]
pub enum Architecture {
All,
Alpha,
Amd64,
Arc,
Arm64,
Armel,
Armhf,
Hppa,
#[serde(rename = "hurd-amd64")]
HurdAmd64,
#[serde(rename = "hurd-i386")]
HurdI386,
I386,
Ia64,
Loong64,
M68k,
Mips64el,
Mipsel,
PowerPC,
Ppc64,
Ppc64el,
Riscv64,
S390x,
Sh4,
Sparc64,
X32,
Source,
}
impl Display for Architecture {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.as_ref())
}
}
impl AsRef<str> for Architecture {
fn as_ref(&self) -> &str {
match self {
Self::All => "all",
Self::Alpha => "alpha",
Self::Amd64 => "amd64",
Self::Arc => "arc",
Self::Arm64 => "arm64",
Self::Armel => "armel",
Self::Armhf => "armhf",
Self::Hppa => "hppa",
Self::HurdAmd64 => "hurd-amd64",
Self::HurdI386 => "hurd-i386",
Self::I386 => "i386",
Self::Ia64 => "ia64",
Self::Loong64 => "loong64",
Self::M68k => "m68k",
Self::Mips64el => "mips64el",
Self::Mipsel => "mipsel",
Self::PowerPC => "powerpc",
Self::Ppc64 => "ppc64",
Self::Ppc64el => "ppc64el",
Self::Riscv64 => "riscv64",
Self::S390x => "s390x",
Self::Sh4 => "sh4",
Self::Sparc64 => "sparc64",
Self::X32 => "x32",
Self::Source => "source",
}
}
}
impl TryFrom<&str> for Architecture {
type Error = ParseError;
fn try_from(value: &str) -> Result<Self, Self::Error> {
match value {
"all" => Ok(Self::All),
"alpha" => Ok(Self::Alpha),
"amd64" => Ok(Self::Amd64),
"arc" => Ok(Self::Arc),
"arm64" => Ok(Self::Arm64),
"armel" => Ok(Self::Armel),
"armhf" => Ok(Self::Armhf),
"hppa" => Ok(Self::Hppa),
"hurd-amd64" => Ok(Self::HurdAmd64),
"hurd-i386" => Ok(Self::HurdI386),
"i386" => Ok(Self::I386),
"ia64" => Ok(Self::Ia64),
"loong64" => Ok(Self::Loong64),
"m68k" => Ok(Self::M68k),
"mips64el" => Ok(Self::Mips64el),
"mipsel" => Ok(Self::Mipsel),
"powerpc" => Ok(Self::PowerPC),
"ppc64" => Ok(Self::Ppc64),
"ppc64el" => Ok(Self::Ppc64el),
"riscv64" => Ok(Self::Riscv64),
"s390x" => Ok(Self::S390x),
"sh4" => Ok(Self::Sh4),
"sparc64" => Ok(Self::Sparc64),
"x32" => Ok(Self::X32),
"source" => Ok(Self::Source),
_ => Err(ParseError::InvalidArchitecture),
}
}
}
impl FromStr for Architecture {
type Err = ParseError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Self::try_from(s)
}
}
#[cfg(test)]
mod test {
use super::Architecture;
#[test]
fn from_str() {
assert_eq!(
Architecture::try_from("amd64").unwrap(),
Architecture::Amd64
);
}
}