From 8252f0d74e83ef744ced822e4c6b333bbf0ea6c5 Mon Sep 17 00:00:00 2001 From: NotAShelf Date: Fri, 16 May 2025 00:42:51 +0300 Subject: [PATCH] config: better validation of battery charge threshold; update error handling --- src/battery.rs | 9 +++++++-- src/config/types.rs | 24 +++++++++++++++--------- 2 files changed, 22 insertions(+), 11 deletions(-) diff --git a/src/battery.rs b/src/battery.rs index 4d05bbc..296096f 100644 --- a/src/battery.rs +++ b/src/battery.rs @@ -72,8 +72,13 @@ pub struct SupportedBattery<'a> { /// - Failed to set thresholds on any battery pub fn set_battery_charge_thresholds(start_threshold: u8, stop_threshold: u8) -> Result<()> { // Validate thresholds using `BatteryChargeThresholds` - let thresholds = BatteryChargeThresholds::new(start_threshold, stop_threshold) - .map_err(ControlError::InvalidValueError)?; + let thresholds = + BatteryChargeThresholds::new(start_threshold, stop_threshold).map_err(|e| match e { + crate::config::types::ConfigError::ValidationError(msg) => { + ControlError::InvalidValueError(msg) + } + _ => ControlError::InvalidValueError(format!("Invalid battery threshold values: {e}")), + })?; let power_supply_path = Path::new("/sys/class/power_supply"); if !power_supply_path.exists() { diff --git a/src/config/types.rs b/src/config/types.rs index 500320f..1c606eb 100644 --- a/src/config/types.rs +++ b/src/config/types.rs @@ -3,24 +3,28 @@ use crate::core::TurboSetting; use serde::{Deserialize, Serialize}; use std::convert::TryFrom; -#[derive(Deserialize, Serialize, Debug, Clone)] +#[derive(Deserialize, Serialize, Debug, Clone, PartialEq, Eq)] pub struct BatteryChargeThresholds { pub start: u8, pub stop: u8, } impl BatteryChargeThresholds { - pub fn new(start: u8, stop: u8) -> Result { + 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})" + return Err(ConfigError::ValidationError( + "Thresholds must be greater than 0%".to_string(), )); } + if start >= stop { + return Err(ConfigError::ValidationError(format!( + "Start threshold ({start}) must be less than stop threshold ({stop})" + ))); + } if stop > 100 { - return Err(format!("Stop threshold ({stop}) cannot exceed 100%")); + return Err(ConfigError::ValidationError(format!( + "Stop threshold ({stop}) cannot exceed 100%" + ))); } Ok(Self { start, stop }) @@ -28,7 +32,7 @@ impl BatteryChargeThresholds { } impl TryFrom<(u8, u8)> for BatteryChargeThresholds { - type Error = String; + type Error = ConfigError; fn try_from(values: (u8, u8)) -> Result { let (start, stop) = values; @@ -85,6 +89,7 @@ pub enum ConfigError { TomlError(toml::de::Error), NoValidConfigFound, HomeDirNotFound, + ValidationError(String), } impl From for ConfigError { @@ -106,6 +111,7 @@ impl std::fmt::Display for ConfigError { Self::TomlError(e) => write!(f, "TOML parsing error: {e}"), Self::NoValidConfigFound => write!(f, "No valid configuration file found."), Self::HomeDirNotFound => write!(f, "Could not determine user home directory."), + Self::ValidationError(s) => write!(f, "Configuration validation error: {s}"), } } }