whoami/
arch.rs

1use alloc::string::String;
2use core::fmt::{self, Display, Formatter};
3
4use crate::{Error, Result};
5
6/// The address width of a CPU architecture
7#[derive(Debug, PartialEq, Eq, Copy, Clone)]
8#[non_exhaustive]
9pub enum Width {
10    /// 32 bits
11    Bits32,
12    /// 64 bits
13    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/// The architecture of a CPU
26#[derive(Debug, PartialEq, Eq, Clone)]
27#[non_exhaustive]
28pub enum CpuArchitecture {
29    /// Unknown Architecture
30    Unknown(String),
31    /// ARMv5
32    ArmV5,
33    /// ARMv6 (Sometimes just referred to as ARM)
34    ArmV6,
35    /// ARMv7 (May or may not support Neon/Thumb)
36    ArmV7,
37    /// ARM64 (aarch64)
38    Arm64,
39    /// i386 (x86)
40    I386,
41    /// i586 (x86)
42    I586,
43    /// i686 (x86)
44    I686,
45    /// X86_64 / Amd64
46    X64,
47    /// MIPS
48    Mips,
49    /// MIPS (LE)
50    MipsEl,
51    /// MIPS64
52    Mips64,
53    /// MIPS64 (LE)
54    Mips64El,
55    /// PowerPC
56    PowerPc,
57    /// PowerPC64
58    PowerPc64,
59    /// PowerPC64LE
60    PowerPc64Le,
61    /// 32-bit RISC-V
62    Riscv32,
63    /// 64-bit RISC-V
64    Riscv64,
65    /// S390x
66    S390x,
67    /// SPARC
68    Sparc,
69    /// SPARC64
70    Sparc64,
71    /// 32-bit Web Assembly
72    Wasm32,
73    /// 64-bit Web Assembly
74    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    /// Get the width of this architecture.
113    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}