mac_sys_info/structs/os_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//! Info about MacOS/software.
26
27use derive_more::Display as DeriveMoreDisplay;
28use serde::{Serialize};
29use std::collections::BTreeMap;
30use crate::error::MacSysInfoError;
31use crate::parse::{parse_sysctl_value, ParseAsType};
32use crate::generated_sysctl_keys::SysctlKey;
33
34/// Info about MacOS/software.
35#[derive(Debug, Serialize, DeriveMoreDisplay)]
36#[display(fmt = "OsInfo (\n\
37\x20 kern_version: {},\n\
38\x20 os_version: {},\n\
39)", kern_version, os_version)]
40pub struct OsInfo {
41 /// Kern version, e.g. "Darwin Kernel Version 19.6.0: Thu Oct 29 22:56:45 PDT 2020; root:xnu-6153.141.2.2~1/RELEASE_X86_64"
42 kern_version: String,
43 /// MacOS version, e.g. "10.15.7"
44 os_version: String,
45}
46
47impl OsInfo {
48 /// Constructor.
49 pub(crate) fn new(sysinfo: &BTreeMap<String, String>) -> Result<Self, MacSysInfoError> {
50 let x = Self {
51 kern_version: parse_sysctl_value(
52 "kern_version",
53 SysctlKey::KernVersion,
54 sysinfo,
55 ParseAsType::String)?
56 .get_string(),
57 os_version: parse_sysctl_value(
58 "os_version",
59 SysctlKey::KernOsproductversion,
60 sysinfo,
61 ParseAsType::String)?
62 .get_string(),
63 };
64 Ok(x)
65 }
66
67 /// Getter for the field `kern_version`.
68 pub fn kern_version(&self) -> &str {
69 &self.kern_version
70 }
71
72 /// Getter for the field `os_version`.
73 pub fn os_version(&self) -> &str {
74 &self.os_version
75 }
76}