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
use crate::{Error, Result};
use std::collections::HashMap;
use std::ops::Deref;
use std::str::FromStr;
#[derive(Debug)]
pub struct CpuInfo(HashMap<String, String>);
impl CpuInfo {
pub fn processor(&self) -> usize {
self.get("processor").unwrap().parse::<usize>().unwrap()
}
pub fn cpu_family(&self) -> &str {
self.get("cpu family").unwrap()
}
pub fn model_name(&self) -> &str {
self.get("model name").unwrap()
}
pub fn cpu_mhz(&self) -> f64 {
self.get("cpu MHz").unwrap().parse::<f64>().unwrap()
}
pub fn cache_size(&self) -> &str {
self.get("cache size").unwrap()
}
pub fn siblings(&self) -> usize {
self.get("siblings").unwrap().parse::<usize>().unwrap()
}
pub fn flags(&self) -> Vec<&str> {
self.get("flags")
.unwrap()
.split_ascii_whitespace()
.collect()
}
}
impl Deref for CpuInfo {
type Target = HashMap<String, String>;
fn deref(&self) -> &HashMap<String, String> {
&self.0
}
}
impl FromStr for CpuInfo {
type Err = Error;
fn from_str(value: &str) -> Result<Self> {
let mut ret = HashMap::new();
for line in value.trim().lines() {
let columns: Vec<&str> = line.split(':').collect();
if columns.len() != 2 {
return Err(Error::BadFormat);
}
ret.insert(columns[0].trim().to_string(), columns[1].trim().to_string());
}
Ok(CpuInfo(ret))
}
}
#[inline(always)]
fn to_cpuinfo(block: &str) -> Result<CpuInfo> {
CpuInfo::from_str(block)
}
default_list! {
cpuinfo, "/proc/cpuinfo", CpuInfo, to_cpuinfo, "\n\n"
}