1use std::{fs, io, str::FromStr};
2
3#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq, Ord, PartialOrd)]
4pub enum Architecture {
5 Any,
6 All,
7 Amd64,
8 Armel,
9 Armhf,
10 Arm64,
11 I386,
12 Mips,
13 Mipsel,
14 Mips64,
15 Mips64El,
16 Ppc64El,
17 S390X,
18 LinuxAny,
19 X32,
20}
21
22impl FromStr for Architecture {
23 type Err = &'static str;
24
25 fn from_str(input: &str) -> Result<Self, Self::Err> {
26 let arch = match input {
27 "any" => Architecture::Any,
28 "all" => Architecture::All,
29 "amd64" => Architecture::Amd64,
30 "armel" => Architecture::Armel,
31 "armhf" => Architecture::Armhf,
32 "arm64" => Architecture::Arm64,
33 "i386" => Architecture::I386,
34 "mips" => Architecture::Mips,
35 "mipsel" => Architecture::Mipsel,
36 "mips64" => Architecture::Mips64,
37 "mips64el" => Architecture::Mips64El,
38 "ppc64el" => Architecture::Ppc64El,
39 "s390x" => Architecture::S390X,
40 "linux-any" => Architecture::LinuxAny,
41 "x32" => Architecture::X32,
42 _ => return Err("invalid architecture"),
43 };
44
45 Ok(arch)
46 }
47}
48
49impl From<Architecture> for &'static str {
50 fn from(arch: Architecture) -> Self {
51 match arch {
52 Architecture::Any => "any",
53 Architecture::All => "all",
54 Architecture::Amd64 => "amd64",
55 Architecture::Armel => "armel",
56 Architecture::Armhf => "armhf",
57 Architecture::Arm64 => "arm64",
58 Architecture::I386 => "i386",
59 Architecture::Mips => "mips",
60 Architecture::Mipsel => "mipsel",
61 Architecture::Mips64 => "mips64",
62 Architecture::Mips64El => "mips64el",
63 Architecture::Ppc64El => "ppc64el",
64 Architecture::S390X => "s390x",
65 Architecture::LinuxAny => "linux-any",
66 Architecture::X32 => "x32",
67 }
68 }
69}
70
71pub fn supported_architectures() -> io::Result<Vec<Architecture>> {
72 fs::read_to_string("/var/lib/dpkg/arch")?
73 .lines()
74 .map(|arch| {
75 arch.parse::<Architecture>().map_err(|why| {
76 io::Error::new(
77 io::ErrorKind::InvalidData,
78 format!("supported_architectures: {}: {}", why, arch),
79 )
80 })
81 })
82 .collect()
83}