assorted_debian_utils/architectures.rs
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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
// Copyright 2021-2024 Sebastian Ramacher
// SPDX-License-Identifier: LGPL-3.0-or-later
//! # Helpers to handle Debian architectures
//!
//! This module provides helpers for working with Debian architectures as they
//! appear in various files related to Debian binary and source packages,
//! archive indices, etc.
use std::fmt::{Display, Formatter};
use std::str::FromStr;
use serde::{Deserialize, Serialize};
pub use crate::ParseError;
/// Debian architectures
///
/// This enum describes architectures that are release architectures or available on Debian ports.
/// It also provides `All` as special case for binary independent packages.
#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq, Hash, Eq)]
#[serde(rename_all = "lowercase")]
pub enum Architecture {
/// The `all` architecture for architecture independent packages
All,
/// The `alpha` architecture
Alpha,
/// The `amd64` architecture
Amd64,
/// The `arc` architecture
Arc,
/// The `arm64` architecture
Arm64,
/// The `armel` architecture
Armel,
/// The `armhf` architecture
Armhf,
/// The `hppa` architecture
Hppa,
/// The `hurd-amd64` architecture
#[serde(rename = "hurd-amd64")]
HurdAmd64,
/// The `hurd-i386` architecture
#[serde(rename = "hurd-i386")]
HurdI386,
/// The `i386` architecture
I386,
/// The `ia64` architecture
Ia64,
/// The `loong64` architecture
Loong64,
/// The `m68k` architecture
M68k,
/// The `mips64el` architecture
Mips64el,
/// The `mipsel` architecture
Mipsel,
/// The `powerpc` architecture
PowerPC,
/// The `ppc64` architecture
Ppc64,
/// The `ppc64el` architecture
Ppc64el,
/// The `riscv64` architecture
Riscv64,
/// The `s390x` architecture
S390x,
/// The `sh4` architecture
Sh4,
/// The `sparc64` architecture
Sparc64,
/// The `x32` architecture
X32,
/// The `source` architecture
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
);
}
}