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