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