From b215afc9ddc8ac91355fb68589024728507181b7 Mon Sep 17 00:00:00 2001 From: NotAShelf Date: Thu, 15 May 2025 21:46:57 +0300 Subject: [PATCH] config: streamline treshold validation --- src/battery.rs | 30 +++++------------------------- src/config/mod.rs | 11 ++++------- src/config/types.rs | 28 ++++++++++++++++++++++++++++ 3 files changed, 37 insertions(+), 32 deletions(-) diff --git a/src/battery.rs b/src/battery.rs index 5fb41d4..4eb9ebb 100644 --- a/src/battery.rs +++ b/src/battery.rs @@ -1,4 +1,4 @@ -use crate::util::error::ControlError; +use crate::{config::types::BatteryChargeThresholds, util::error::ControlError}; use log::{debug, warn}; use std::{ fs, io, @@ -71,7 +71,9 @@ pub struct SupportedBattery { /// - No batteries with threshold support are found /// - Failed to set thresholds on any battery pub fn set_battery_charge_thresholds(start_threshold: u8, stop_threshold: u8) -> Result<()> { - validate_thresholds(start_threshold, stop_threshold)?; + // Validate thresholds using `BatteryChargeThresholds` + let thresholds = BatteryChargeThresholds::new(start_threshold, stop_threshold) + .map_err(|e| ControlError::InvalidValueError(e))?; let power_supply_path = Path::new("/sys/class/power_supply"); if !power_supply_path.exists() { @@ -87,29 +89,7 @@ pub fn set_battery_charge_thresholds(start_threshold: u8, stop_threshold: u8) -> )); } - apply_thresholds_to_batteries(&supported_batteries, start_threshold, stop_threshold) -} - -/// Validates that the threshold values are in acceptable ranges -fn validate_thresholds(start_threshold: u8, stop_threshold: u8) -> Result<()> { - if start_threshold == 0 || stop_threshold == 0 { - return Err(ControlError::InvalidValueError( - "Thresholds must be greater than 0%".to_string(), - )); - } - if start_threshold >= stop_threshold { - return Err(ControlError::InvalidValueError(format!( - "Start threshold ({start_threshold}) must be less than stop threshold ({stop_threshold})" - ))); - } - - if stop_threshold > 100 { - return Err(ControlError::InvalidValueError(format!( - "Stop threshold ({stop_threshold}) cannot exceed 100%" - ))); - } - - Ok(()) + apply_thresholds_to_batteries(&supported_batteries, thresholds.start, thresholds.stop) } /// Finds all batteries in the system that support threshold control diff --git a/src/config/mod.rs b/src/config/mod.rs index b386b52..0a20a83 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -1,9 +1,6 @@ +pub mod load; +pub mod types; pub mod watcher; -// Re-export all configuration types and functions -pub use self::load::*; -pub use self::types::*; - -// Internal organization of config submodules -mod load; -mod types; +pub use load::*; +pub use types::*; diff --git a/src/config/types.rs b/src/config/types.rs index 32c7c38..742ff78 100644 --- a/src/config/types.rs +++ b/src/config/types.rs @@ -1,6 +1,7 @@ // Configuration types and structures for superfreq use crate::core::TurboSetting; use serde::{Deserialize, Serialize}; +use std::convert::TryFrom; #[derive(Deserialize, Serialize, Debug, Clone)] pub struct BatteryChargeThresholds { @@ -8,6 +9,33 @@ pub struct BatteryChargeThresholds { pub stop: u8, } +impl BatteryChargeThresholds { + pub fn new(start: u8, stop: u8) -> Result { + if start == 0 || stop == 0 { + return Err("Thresholds must be greater than 0%".to_string()); + } + if start >= stop { + return Err(format!( + "Start threshold ({start}) must be less than stop threshold ({stop})" + )); + } + if stop > 100 { + return Err(format!("Stop threshold ({stop}) cannot exceed 100%")); + } + + Ok(Self { start, stop }) + } +} + +impl TryFrom<(u8, u8)> for BatteryChargeThresholds { + type Error = String; + + fn try_from(values: (u8, u8)) -> Result { + let (start, stop) = values; + Self::new(start, stop) + } +} + // Structs for configuration using serde::Deserialize #[derive(Deserialize, Debug, Clone)] pub struct ProfileConfig {