whoami/
arch.rs

1use std::{
2    fmt::{self, Display, Formatter},
3    io::{Error, ErrorKind},
4};
5
6use crate::Result;
7
8/// The address width of a CPU architecture
9#[derive(Debug, PartialEq, Eq, Copy, Clone)]
10#[non_exhaustive]
11pub enum Width {
12    /// 32 bits
13    Bits32,
14    /// 64 bits
15    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/// The architecture of a CPU
28#[non_exhaustive]
29#[derive(Debug, PartialEq, Eq, Clone)]
30pub enum Arch {
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    /// Unknown Architecture
76    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    /// Get the width of this architecture.
115    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}