1
Fork 0
mirror of https://github.com/RGBCube/superfreq synced 2025-07-27 17:07:44 +00:00

config: better validation of battery charge threshold; update error handling

This commit is contained in:
NotAShelf 2025-05-16 00:42:51 +03:00
parent eb97689bc7
commit 8252f0d74e
No known key found for this signature in database
GPG key ID: 29D95B64378DB4BF
2 changed files with 22 additions and 11 deletions

View file

@ -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() {

View file

@ -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<Self, String> {
pub fn new(start: u8, stop: u8) -> Result<Self, ConfigError> {
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<Self, Self::Error> {
let (start, stop) = values;
@ -85,6 +89,7 @@ pub enum ConfigError {
TomlError(toml::de::Error),
NoValidConfigFound,
HomeDirNotFound,
ValidationError(String),
}
impl From<std::io::Error> 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}"),
}
}
}