power: Simplify GPUPowerProfile definition with strum

This commit is contained in:
Vicki Pfau 2024-07-31 20:40:19 -07:00
parent cdc314fb47
commit 4efb3572ec

View file

@ -5,9 +5,8 @@
* SPDX-License-Identifier: MIT * SPDX-License-Identifier: MIT
*/ */
use anyhow::{anyhow, bail, ensure, Error, Result}; use anyhow::{anyhow, bail, ensure, Result};
use std::collections::HashMap; use std::collections::HashMap;
use std::fmt;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use std::str::FromStr; use std::str::FromStr;
use strum::{Display, EnumString}; use strum::{Display, EnumString};
@ -34,12 +33,14 @@ const CPU_SCALING_AVAILABLE_GOVERNORS_SUFFIX: &str = "scaling_available_governor
const TDP_LIMIT1: &str = "power1_cap"; const TDP_LIMIT1: &str = "power1_cap";
const TDP_LIMIT2: &str = "power2_cap"; const TDP_LIMIT2: &str = "power2_cap";
#[derive(PartialEq, Debug, Copy, Clone)] #[derive(Display, EnumString, PartialEq, Debug, Copy, Clone)]
#[strum(serialize_all = "snake_case")]
#[repr(u32)] #[repr(u32)]
pub enum GPUPowerProfile { pub enum GPUPowerProfile {
// Currently firmware exposes these values, though // Currently firmware exposes these values, though
// deck doesn't support them yet // deck doesn't support them yet
FullScreen = 1, // 3D_FULL_SCREEN #[strum(serialize = "3d_full_screen")]
FullScreen = 1,
Video = 3, Video = 3,
VR = 4, VR = 4,
Compute = 5, Compute = 5,
@ -66,36 +67,6 @@ impl TryFrom<u32> for GPUPowerProfile {
} }
} }
impl FromStr for GPUPowerProfile {
type Err = Error;
fn from_str(input: &str) -> Result<GPUPowerProfile, Self::Err> {
Ok(match input.to_lowercase().as_str() {
"3d_full_screen" => GPUPowerProfile::FullScreen,
"video" => GPUPowerProfile::Video,
"vr" => GPUPowerProfile::VR,
"compute" => GPUPowerProfile::Compute,
"custom" => GPUPowerProfile::Custom,
"capped" => GPUPowerProfile::Capped,
"uncapped" => GPUPowerProfile::Uncapped,
_ => bail!("No match for value {input}"),
})
}
}
impl fmt::Display for GPUPowerProfile {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
GPUPowerProfile::FullScreen => write!(f, "3d_full_screen"),
GPUPowerProfile::Video => write!(f, "video"),
GPUPowerProfile::VR => write!(f, "vr"),
GPUPowerProfile::Compute => write!(f, "compute"),
GPUPowerProfile::Custom => write!(f, "custom"),
GPUPowerProfile::Capped => write!(f, "capped"),
GPUPowerProfile::Uncapped => write!(f, "uncapped"),
}
}
}
#[derive(Display, EnumString, PartialEq, Debug, Copy, Clone)] #[derive(Display, EnumString, PartialEq, Debug, Copy, Clone)]
#[strum(serialize_all = "snake_case")] #[strum(serialize_all = "snake_case")]
#[repr(u32)] #[repr(u32)]