1use std::{
2 fmt::{self, Display, Formatter},
3 io::{Error, ErrorKind},
4};
5
6use crate::Result;
7
8#[derive(Debug, PartialEq, Eq, Copy, Clone)]
10#[non_exhaustive]
11pub enum Width {
12 Bits32,
14 Bits64,
16}
17
18impl Display for Width {
19 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
20 f.write_str(match self {
21 Width::Bits32 => "32 bits",
22 Width::Bits64 => "64 bits",
23 })
24 }
25}
26
27#[non_exhaustive]
29#[derive(Debug, PartialEq, Eq, Clone)]
30pub enum Arch {
31 ArmV5,
33 ArmV6,
35 ArmV7,
37 Arm64,
39 I386,
41 I586,
43 I686,
45 X64,
47 Mips,
49 MipsEl,
51 Mips64,
53 Mips64El,
55 PowerPc,
57 PowerPc64,
59 PowerPc64Le,
61 Riscv32,
63 Riscv64,
65 S390x,
67 Sparc,
69 Sparc64,
71 Wasm32,
73 Wasm64,
75 Unknown(String),
77}
78
79impl Display for Arch {
80 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
81 if let Self::Unknown(_) = self {
82 f.write_str("Unknown: ")?;
83 }
84
85 f.write_str(match self {
86 Self::ArmV5 => "armv5",
87 Self::ArmV6 => "armv6",
88 Self::ArmV7 => "armv7",
89 Self::Arm64 => "arm64",
90 Self::I386 => "i386",
91 Self::I586 => "i586",
92 Self::I686 => "i686",
93 Self::Mips => "mips",
94 Self::MipsEl => "mipsel",
95 Self::Mips64 => "mips64",
96 Self::Mips64El => "mips64el",
97 Self::PowerPc => "powerpc",
98 Self::PowerPc64 => "powerpc64",
99 Self::PowerPc64Le => "powerpc64le",
100 Self::Riscv32 => "riscv32",
101 Self::Riscv64 => "riscv64",
102 Self::S390x => "s390x",
103 Self::Sparc => "sparc",
104 Self::Sparc64 => "sparc64",
105 Self::Wasm32 => "wasm32",
106 Self::Wasm64 => "wasm64",
107 Self::X64 => "x86_64",
108 Self::Unknown(arch) => arch,
109 })
110 }
111}
112
113impl Arch {
114 pub fn width(&self) -> Result<Width> {
116 match self {
117 Arch::ArmV5
118 | Arch::ArmV6
119 | Arch::ArmV7
120 | Arch::I386
121 | Arch::I586
122 | Arch::I686
123 | Arch::Mips
124 | Arch::MipsEl
125 | Arch::PowerPc
126 | Arch::Riscv32
127 | Arch::Sparc
128 | Arch::Wasm32 => Ok(Width::Bits32),
129 Arch::Arm64
130 | Arch::Mips64
131 | Arch::Mips64El
132 | Arch::PowerPc64
133 | Arch::PowerPc64Le
134 | Arch::Riscv64
135 | Arch::S390x
136 | Arch::Sparc64
137 | Arch::Wasm64
138 | Arch::X64 => Ok(Width::Bits64),
139 Arch::Unknown(unknown_arch) => Err(Error::new(
140 ErrorKind::InvalidData,
141 format!(
142 "Tried getting width of unknown arch ({})",
143 unknown_arch,
144 ),
145 )),
146 }
147 }
148}