mac_sys_info/structs/
cpu_architecture_info.rs

1/*
2MIT License
3
4Copyright (c) 2020 Philipp Schuster
5
6Permission is hereby granted, free of charge, to any person obtaining a copy
7of this software and associated documentation files (the "Software"), to deal
8in the Software without restriction, including without limitation the rights
9to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10copies of the Software, and to permit persons to whom the Software is
11furnished to do so, subject to the following conditions:
12
13The above copyright notice and this permission notice shall be included in all
14copies or substantial portions of the Software.
15
16THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22SOFTWARE.
23*/
24
25//! CPU-Architecture info.
26
27use derive_more::Display as DeriveMoreDisplay;
28use serde::{Serialize};
29use std::collections::BTreeMap;
30use crate::error::MacSysInfoError;
31use crate::generated_sysctl_keys::SysctlKey;
32
33/// Information about the CPU architecture. Because this library
34/// is Mac-specific, there is only X86_64 (since the beginning)
35/// and Apple Silicon (AppleSi) since late 2020.
36#[derive(Debug, Serialize, DeriveMoreDisplay)]
37#[allow(non_camel_case_types)]
38#[allow(dead_code)]
39pub enum CpuArchitectureInfo {
40    /// x86_64
41    x86_64,
42    /// Apple Silicon
43    AppleSi,
44}
45
46
47#[allow(dead_code)]
48impl CpuArchitectureInfo {
49
50    /// Constructor
51    pub(crate) fn new(sysinfo: &BTreeMap<String, String>) -> Result<Self, MacSysInfoError> {
52        CpuArchitectureInfo::determine_architecture(sysinfo)
53    }
54
55    /// Reads the CPU architecture from the "sysctl -a" output.
56    pub(crate) fn determine_architecture(sysinfo: &BTreeMap<String, String>) -> Result<CpuArchitectureInfo, MacSysInfoError> {
57        let is_x86 = sysinfo.get(SysctlKey::KernVersion.name())
58            .ok_or(MacSysInfoError::KeyNotFound(SysctlKey::KernVersion))?
59            .to_lowercase()
60            .contains("x86_64");
61        if is_x86 {
62            Ok(CpuArchitectureInfo::x86_64)
63        } else {
64            Ok(CpuArchitectureInfo::AppleSi)
65        }
66    }
67
68    /// Convenient getter to check if the current system is a x86_64 system.
69    pub fn is_x86_64(&self) -> bool {
70        match self {
71            CpuArchitectureInfo::x86_64 => { true }
72            CpuArchitectureInfo::AppleSi => { false }
73        }
74    }
75
76    /// Convenient getter to check if the current system is a Apple Silicon system.
77    pub fn is_apple_si(&self) -> bool {
78        !self.is_x86_64()
79    }
80}
81
82