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

core: validate tresholds at the CLI level

This commit is contained in:
NotAShelf 2025-05-15 23:20:08 +03:00
parent 90fd077a67
commit 6fe322272e
No known key found for this signature in database
GPG key ID: 29D95B64378DB4BF
3 changed files with 30 additions and 10 deletions

View file

@ -154,7 +154,7 @@ impl Default for ProfileConfigToml {
}
}
#[derive(Deserialize, Debug, Clone)]
#[derive(Deserialize, Serialize, Debug, Clone)]
pub struct TurboAutoSettings {
#[serde(default = "default_load_threshold_high")]
pub load_threshold_high: f32,
@ -230,7 +230,7 @@ pub struct DaemonConfig {
pub stats_file_path: Option<String>,
}
#[derive(Deserialize, Debug, Clone, Copy, PartialEq, Eq)]
#[derive(Deserialize, Serialize, Debug, Clone, Copy, PartialEq, Eq)]
pub enum LogLevel {
Error,
Warning,
@ -261,7 +261,7 @@ const fn default_adaptive_interval() -> bool {
}
const fn default_min_poll_interval_sec() -> u64 {
1
1
}
const fn default_max_poll_interval_sec() -> u64 {

View file

@ -1,8 +1,8 @@
use clap::ValueEnum;
use serde::Deserialize;
use serde::{Deserialize, Serialize};
use std::fmt;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, ValueEnum)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, Serialize, ValueEnum)]
pub enum TurboSetting {
Always, // turbo is forced on (if possible)
Auto, // system or driver controls turbo

View file

@ -370,11 +370,31 @@ fn main() {
start_threshold,
stop_threshold,
}) => {
info!(
"Setting battery thresholds: start at {start_threshold}%, stop at {stop_threshold}%"
);
battery::set_battery_charge_thresholds(start_threshold, stop_threshold)
.map_err(|e| Box::new(e) as Box<dyn std::error::Error>)
// Basic validation to provide proper error messages at the CLI level
if start_threshold >= stop_threshold {
error!(
"Start threshold ({start_threshold}) must be less than stop threshold ({stop_threshold})"
);
Err(Box::new(ControlError::InvalidValueError(format!(
"Start threshold ({start_threshold}) must be less than stop threshold ({stop_threshold})"
))) as Box<dyn std::error::Error>)
} else if stop_threshold > 100 {
error!("Stop threshold ({stop_threshold}) cannot exceed 100%");
Err(Box::new(ControlError::InvalidValueError(format!(
"Stop threshold ({stop_threshold}) cannot exceed 100%"
))) as Box<dyn std::error::Error>)
} else if start_threshold == 0 || stop_threshold == 0 {
error!("Thresholds must be greater than 0%");
Err(Box::new(ControlError::InvalidValueError(
"Thresholds must be greater than 0%".to_string(),
)) as Box<dyn std::error::Error>)
} else {
info!(
"Setting battery thresholds: start at {start_threshold}%, stop at {stop_threshold}%"
);
battery::set_battery_charge_thresholds(start_threshold, stop_threshold)
.map_err(|e| Box::new(e) as Box<dyn std::error::Error>)
}
}
Some(Commands::Daemon { verbose }) => daemon::run_daemon(config, verbose),
Some(Commands::Debug) => cli::debug::run_debug(&config),