From 93878bbe8ec22ed8bd95e79b9463c9eca1acdf2f Mon Sep 17 00:00:00 2001 From: Vicki Pfau Date: Mon, 7 Oct 2024 19:20:07 -0700 Subject: [PATCH] hardware: Switch another enum to strum --- src/hardware.rs | 26 +++++--------------------- 1 file changed, 5 insertions(+), 21 deletions(-) diff --git a/src/hardware.rs b/src/hardware.rs index 2c83f96..75ead2f 100644 --- a/src/hardware.rs +++ b/src/hardware.rs @@ -9,6 +9,7 @@ use anyhow::{bail, ensure, Error, Result}; use num_enum::TryFromPrimitive; use std::fmt; use std::str::FromStr; +use strum::{Display, EnumString}; use tokio::fs; use zbus::Connection; @@ -36,10 +37,13 @@ pub(crate) enum HardwareCurrentlySupported { Supported = 2, } -#[derive(PartialEq, Debug, Copy, Clone, TryFromPrimitive)] +#[derive(Display, EnumString, PartialEq, Debug, Copy, Clone, TryFromPrimitive)] +#[strum(ascii_case_insensitive)] #[repr(u32)] pub enum FanControlState { + #[strum(to_string = "BIOS")] Bios = 0, + #[strum(to_string = "OS")] Os = 1, } @@ -64,26 +68,6 @@ impl fmt::Display for HardwareCurrentlySupported { } } -impl FromStr for FanControlState { - type Err = Error; - fn from_str(input: &str) -> Result { - Ok(match input.to_lowercase().as_str() { - "bios" => FanControlState::Bios, - "os" => FanControlState::Os, - v => bail!("No enum match for value {v}"), - }) - } -} - -impl fmt::Display for FanControlState { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - match self { - FanControlState::Bios => write!(f, "BIOS"), - FanControlState::Os => write!(f, "OS"), - } - } -} - pub(crate) async fn variant() -> Result { let board_vendor = fs::read_to_string(path(BOARD_VENDOR_PATH)).await?; if board_vendor.trim_end() != "Valve" {