mirror of
https://github.com/RGBCube/superfreq
synced 2025-07-28 09:27:44 +00:00
config: streamline treshold validation
This commit is contained in:
parent
759ba2a10a
commit
b215afc9dd
3 changed files with 37 additions and 32 deletions
|
@ -1,4 +1,4 @@
|
||||||
use crate::util::error::ControlError;
|
use crate::{config::types::BatteryChargeThresholds, util::error::ControlError};
|
||||||
use log::{debug, warn};
|
use log::{debug, warn};
|
||||||
use std::{
|
use std::{
|
||||||
fs, io,
|
fs, io,
|
||||||
|
@ -71,7 +71,9 @@ pub struct SupportedBattery {
|
||||||
/// - No batteries with threshold support are found
|
/// - No batteries with threshold support are found
|
||||||
/// - Failed to set thresholds on any battery
|
/// - Failed to set thresholds on any battery
|
||||||
pub fn set_battery_charge_thresholds(start_threshold: u8, stop_threshold: u8) -> Result<()> {
|
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");
|
let power_supply_path = Path::new("/sys/class/power_supply");
|
||||||
if !power_supply_path.exists() {
|
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)
|
apply_thresholds_to_batteries(&supported_batteries, thresholds.start, thresholds.stop)
|
||||||
}
|
|
||||||
|
|
||||||
/// 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(())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Finds all batteries in the system that support threshold control
|
/// Finds all batteries in the system that support threshold control
|
||||||
|
|
|
@ -1,9 +1,6 @@
|
||||||
|
pub mod load;
|
||||||
|
pub mod types;
|
||||||
pub mod watcher;
|
pub mod watcher;
|
||||||
|
|
||||||
// Re-export all configuration types and functions
|
pub use load::*;
|
||||||
pub use self::load::*;
|
pub use types::*;
|
||||||
pub use self::types::*;
|
|
||||||
|
|
||||||
// Internal organization of config submodules
|
|
||||||
mod load;
|
|
||||||
mod types;
|
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
// Configuration types and structures for superfreq
|
// Configuration types and structures for superfreq
|
||||||
use crate::core::TurboSetting;
|
use crate::core::TurboSetting;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
use std::convert::TryFrom;
|
||||||
|
|
||||||
#[derive(Deserialize, Serialize, Debug, Clone)]
|
#[derive(Deserialize, Serialize, Debug, Clone)]
|
||||||
pub struct BatteryChargeThresholds {
|
pub struct BatteryChargeThresholds {
|
||||||
|
@ -8,6 +9,33 @@ pub struct BatteryChargeThresholds {
|
||||||
pub stop: u8,
|
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
|
// Structs for configuration using serde::Deserialize
|
||||||
#[derive(Deserialize, Debug, Clone)]
|
#[derive(Deserialize, Debug, Clone)]
|
||||||
pub struct ProfileConfig {
|
pub struct ProfileConfig {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue