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
use super::trim_sysfs_line;
use crate::{error::Error, Result};
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;
#[derive(Debug)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct PowerProfileModesTable {
pub modes: BTreeMap<u16, String>,
pub active: u16,
}
impl PowerProfileModesTable {
pub fn parse(s: &str) -> Result<Self> {
let mut modes = BTreeMap::new();
let mut active = None;
for (line, row) in s.lines().map(trim_sysfs_line).enumerate() {
let mut parts = row.split_whitespace();
if let Some(num) = parts.next().and_then(|part| part.parse::<u16>().ok()) {
let mut name = parts
.next()
.ok_or_else(|| Error::unexpected_eol("No name after mode number", line))?
.trim_matches(':');
if let Some(stripped_name) = name.strip_suffix('*') {
name = stripped_name;
active = Some(num);
}
modes.insert(num, name.to_owned());
if active.is_none() {
if let Some(part) = parts.next() {
if part.starts_with('*') {
active = Some(num);
}
}
}
}
}
Ok(Self {
modes,
active: active.ok_or_else(|| Error::basic_parse_error("No active level found"))?,
})
}
}
#[cfg(test)]
mod tests {
use super::PowerProfileModesTable;
use insta::assert_yaml_snapshot;
const TABLE_VEGA56: &str = include_test_data!("vega56/pp_power_profile_mode");
const TABLE_RX580: &str = include_test_data!("rx580/pp_power_profile_mode");
const TABLE_4800H: &str = include_test_data!("internal-4800h/pp_power_profile_mode");
const TABLE_RX6900XT: &str = include_test_data!("rx6900xt/pp_power_profile_mode");
#[test]
fn parse_full_vega56() {
let table = PowerProfileModesTable::parse(TABLE_VEGA56).unwrap();
assert_yaml_snapshot!(table);
}
#[test]
fn parse_full_rx580() {
let table = PowerProfileModesTable::parse(TABLE_RX580).unwrap();
assert_yaml_snapshot!(table);
}
#[test]
fn parse_full_internal_4800h() {
let table = PowerProfileModesTable::parse(TABLE_4800H).unwrap();
assert_yaml_snapshot!(table);
}
#[test]
fn parse_full_rx6900xt() {
let table = PowerProfileModesTable::parse(TABLE_RX6900XT).unwrap();
assert_yaml_snapshot!(table);
}
}