Skip to main content

wasm_pack/install/
arch.rs

1use anyhow::{bail, Result};
2use std::fmt;
3
4use crate::target;
5
6/// An enum representing supported architectures
7#[derive(Clone, PartialEq, Eq)]
8pub enum Arch {
9    /// x86 64-bit
10    X86_64,
11    /// x86 32-bit
12    X86,
13    /// ARM 64-bit
14    AArch64,
15    /// RISC-V 64-bit
16    Riscv64,
17}
18
19impl Arch {
20    /// Gets the current architecture
21    pub fn get() -> Result<Self> {
22        if target::x86_64 {
23            Ok(Arch::X86_64)
24        } else if target::x86 {
25            Ok(Arch::X86)
26        } else if target::aarch64 {
27            Ok(Arch::AArch64)
28        } else if target::riscv64 {
29            Ok(Arch::Riscv64)
30        } else {
31            bail!("Unrecognized target!")
32        }
33    }
34}
35
36impl fmt::Display for Arch {
37    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
38        let s = match self {
39            Arch::X86_64 => "x86-64",
40            Arch::X86 => "x86",
41            Arch::AArch64 => "aarch64",
42            Arch::Riscv64 => "riscv64",
43        };
44        write!(f, "{}", s)
45    }
46}