mac_sys_info/structs/
mac_sysinfo.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//! The main struct that encapsulated all other info structs.
26
27use derive_more::Display as DeriveMoreDisplay;
28use serde::{Serialize};
29use std::collections::BTreeMap;
30use crate::structs::CpuInfo;
31use crate::structs::OsInfo;
32use crate::structs::MemInfo;
33use crate::structs::CpuFeaturesInfo;
34use crate::error::MacSysInfoError;
35
36/// Abstraction over MacOS system info.
37/// Allows raw access to all obtained system configuration as
38/// well as convenient abstractions over them.
39#[derive(Debug, Serialize, DeriveMoreDisplay)]
40#[display(fmt = "MacSysInfo (\n\
41\x20    all_keys: <Map>,\n\
42\x20    cpu_info: <CpuInfo>,\n\
43\x20    cpu_features: <CpuFeaturesInfo>,\n\
44\x20    os_info: <OsInfo>,\n\
45\x20    mem_info: <MemoryInfo>,\n\
46)")]
47pub struct MacSysInfo {
48    /// Raw presentation of all keys that `$ sysctl -a` outputs in alphabetically order.
49    /// You can use `.name()` on any enum variant of
50    /// [`crate::generated_sysctl_keys::SysctlKey`] to access
51    /// this. The key is a string and not `SysctlKey` in order to guarantee,
52    /// that even if `$ sysctl -a` outputs more keys than known
53    /// (e.g. on newer AppleSi Macbooks), all keys can be accessed.
54    all_keys: BTreeMap<String, String>,
55    /// Basic CPU info including Cache info.
56    cpu_info: CpuInfo,
57    /// Advanced CPU info.
58    cpu_features: CpuFeaturesInfo,
59    /// Software info.
60    os_info: OsInfo,
61    /// Memory info.
62    mem_info: MemInfo,
63}
64
65impl MacSysInfo {
66    /// Constructor.
67    pub(crate) fn new(all_keys: BTreeMap<String, String>) -> Result<Self, MacSysInfoError> {
68        let x = MacSysInfo {
69            cpu_info: CpuInfo::new(&all_keys)?,
70            cpu_features: CpuFeaturesInfo::new(&all_keys)?,
71            os_info: OsInfo::new(&all_keys)?,
72            mem_info: MemInfo::new(&all_keys)?,
73            all_keys,
74        };
75        Ok(x)
76    }
77
78    /// Getter for `all_keys`.
79    pub fn all_keys(&self) -> &BTreeMap<String, String> {
80        &self.all_keys
81    }
82
83    /// Getter for [`crate::structs::CpuFeaturesInfo`].
84    pub fn cpu_features(&self) -> &CpuFeaturesInfo {
85        &self.cpu_features
86    }
87
88    /// Getter for [`crate::structs::CpuInfo`].
89    pub fn cpu_info(&self) -> &CpuInfo {
90        &self.cpu_info
91    }
92
93    /// Getter for [`crate::structs::OsInfo`].
94    pub fn os_info(&self) -> &OsInfo {
95        &self.os_info
96    }
97
98    /// Getter for [`crate::structs::MemoryInfo`].
99    pub fn mem_info(&self) -> &MemInfo {
100        &self.mem_info
101    }
102
103}