1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
use std::{
    borrow::Cow,
    env::{self, VarError},
    fmt,
};

// adapted from target/arch.rs from platforms crate
/// `target_arch`: Target CPU architecture
#[derive(Clone, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)]
#[non_exhaustive]
pub enum Arch<'a> {
    /// `aarch64`: ARMv8 64-bit architecture
    AARCH64,

    /// `arm`: 32-bit ARM architecture
    ARM,

    /// `asm`: asm.js output
    ASMJS,

    /// `mips`: 32-bit MIPS CPU architecture
    MIPS,

    /// `mips64`: 32-bit MIPS CPU architecture
    MIPS64,

    /// `msp430`: 16-bit MSP430 microcontrollers
    MSP430,

    /// `powerpc`: 32-bit POWERPC platform
    POWERPC,

    /// `powerpc64`: 64-bit POWERPC platform
    POWERPC64,

    /// `riscv`: RISC-V CPU architecture
    RISCV,

    /// `s390x`: 64-bit IBM z/Architecture
    S390X,

    /// `sparc`: 32-bit SPARC CPU architecture
    SPARC,

    /// `sparc64`: 64-bit SPARC CPU architecture
    SPARC64,

    /// `thumbv6`: 16-bit ARM CPU architecture subset
    THUMBV6,

    /// `thumbv7`: 16-bit ARM CPU architecture subset
    THUMBV7,

    /// `wasm32`: Web Assembly (32-bit)
    WASM32,

    /// `x86`: Generic x86 CPU architecture
    X86,

    /// `x86_64`: "AMD64" CPU architecture
    X86_64,

    /// Unknown CPU architecture
    Other(Cow<'a, str>),
}

impl<'a> Arch<'a> {
    /// String representing this target architecture which matches `cfg(target_arch)`.
    pub fn as_str(&self) -> &str {
        match self {
            Arch::AARCH64 => "aarch64",
            Arch::ARM => "arm",
            Arch::ASMJS => "asmjs",
            Arch::MIPS => "mips",
            Arch::MIPS64 => "mips64",
            Arch::MSP430 => "msp430",
            Arch::POWERPC => "powerpc",
            Arch::POWERPC64 => "powerpc64",
            Arch::RISCV => "riscv",
            Arch::S390X => "s390x",
            Arch::SPARC => "sparc",
            Arch::SPARC64 => "sparc64",
            Arch::THUMBV6 => "thumbv6",
            Arch::THUMBV7 => "thumbv7",
            Arch::WASM32 => "wasm32",
            Arch::X86 => "x86",
            Arch::X86_64 => "x86_64",
            Arch::Other(s) => s,
        }
    }

    /// Create a new [`Arch`] from the given string.
    fn from_str(arch_name: impl Into<Cow<'a, str>>) -> Self {
        let arch_name = arch_name.into();
        match arch_name.as_ref() {
            "aarch64" => Arch::AARCH64,
            "arm" => Arch::ARM,
            "asmjs" => Arch::ASMJS,
            "mips" => Arch::MIPS,
            "mips64" => Arch::MIPS64,
            "msp430" => Arch::MSP430,
            "powerpc" => Arch::POWERPC,
            "powerpc64" => Arch::POWERPC64,
            "riscv" => Arch::RISCV,
            "s390x" => Arch::S390X,
            "sparc" => Arch::SPARC,
            "sparc64" => Arch::SPARC64,
            "thumbv6" => Arch::THUMBV6,
            "thumbv7" => Arch::THUMBV7,
            "wasm32" => Arch::WASM32,
            "x86" => Arch::X86,
            "x86_64" => Arch::X86_64,
            _ => Arch::Other(arch_name),
        }
    }

    pub fn target() -> Result<Self, VarError> {
        env::var("CARGO_CFG_TARGET_ARCH").map(Self::from_str)
    }
}

impl<'a> fmt::Display for Arch<'a> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(self.as_str())
    }
}