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

config: streamline treshold validation

This commit is contained in:
NotAShelf 2025-05-15 21:46:57 +03:00
parent 759ba2a10a
commit b215afc9dd
No known key found for this signature in database
GPG key ID: 29D95B64378DB4BF
3 changed files with 37 additions and 32 deletions

View file

@ -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

View file

@ -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::*;

View file

@ -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<Self, String> {
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<Self, Self::Error> {
let (start, stop) = values;
Self::new(start, stop)
}
}
// Structs for configuration using serde::Deserialize
#[derive(Deserialize, Debug, Clone)]
pub struct ProfileConfig {