assorted_debian_utils/
architectures.rs

1// Copyright 2021-2024 Sebastian Ramacher
2// SPDX-License-Identifier: LGPL-3.0-or-later
3
4//! # Helpers to handle Debian architectures
5//!
6//! This module provides helpers for working with Debian architectures as they
7//! appear in various files related to Debian binary and source packages,
8//! archive indices, etc.
9
10use std::fmt::{Display, Formatter};
11use std::str::FromStr;
12
13use serde::{Deserialize, Serialize};
14
15pub use crate::ParseError;
16
17/// Debian architectures
18///
19/// This enum describes architectures that are release architectures or available on Debian ports.
20/// It also provides `All` as special case for binary independent packages.
21#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq, Hash, Eq)]
22#[serde(rename_all = "lowercase")]
23pub enum Architecture {
24    /// The `all` architecture for architecture independent packages
25    All,
26    /// The `alpha` architecture
27    Alpha,
28    /// The `amd64` architecture
29    Amd64,
30    /// The `arc` architecture
31    Arc,
32    /// The `arm64` architecture
33    Arm64,
34    /// The `armel` architecture
35    Armel,
36    /// The `armhf` architecture
37    Armhf,
38    /// The `hppa` architecture
39    Hppa,
40    /// The `hurd-amd64` architecture
41    #[serde(rename = "hurd-amd64")]
42    HurdAmd64,
43    /// The `hurd-i386` architecture
44    #[serde(rename = "hurd-i386")]
45    HurdI386,
46    /// The `i386` architecture
47    I386,
48    /// The `ia64` architecture
49    Ia64,
50    /// The `loong64` architecture
51    Loong64,
52    /// The `m68k` architecture
53    M68k,
54    /// The `mips64el` architecture
55    Mips64el,
56    /// The `mipsel` architecture
57    Mipsel,
58    /// The `powerpc` architecture
59    PowerPC,
60    /// The `ppc64` architecture
61    Ppc64,
62    /// The `ppc64el` architecture
63    Ppc64el,
64    /// The `riscv64` architecture
65    Riscv64,
66    /// The `s390x` architecture
67    S390x,
68    /// The `sh4` architecture
69    Sh4,
70    /// The `sparc64` architecture
71    Sparc64,
72    /// The `x32` architecture
73    X32,
74    /// The `source` architecture
75    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}