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

battery: simplify file permission check and improve threshold inheritance

This commit is contained in:
NotAShelf 2025-05-16 00:18:46 +03:00
parent d8f609fdef
commit eb97689bc7
No known key found for this signature in database
GPG key ID: 29D95B64378DB4BF
4 changed files with 19 additions and 21 deletions

View file

@ -168,10 +168,7 @@ fn path_exists_and_writable(path: &Path) -> bool {
}
// Try to open the file with write access to verify write permission
match fs::OpenOptions::new().write(true).open(path) {
Ok(_) => true,
Err(_) => false,
}
fs::OpenOptions::new().write(true).open(path).is_ok()
}
/// Identifies if a battery supports threshold control and which pattern it uses

View file

@ -84,14 +84,17 @@ fn load_and_parse_config(path: &Path) -> Result<AppConfig, ConfigError> {
let mut charger_profile = toml_app_config.charger.clone();
let mut battery_profile = toml_app_config.battery.clone();
// If profile-specific battery thresholds are not set, inherit from global config
if charger_profile.battery_charge_thresholds.is_none() {
charger_profile.battery_charge_thresholds =
toml_app_config.battery_charge_thresholds.clone();
}
// Clone global battery_charge_thresholds once if it exists
if let Some(global_thresholds) = toml_app_config.battery_charge_thresholds {
// Apply to charger profile if not already set
if charger_profile.battery_charge_thresholds.is_none() {
charger_profile.battery_charge_thresholds = Some(global_thresholds.clone());
}
if battery_profile.battery_charge_thresholds.is_none() {
battery_profile.battery_charge_thresholds = toml_app_config.battery_charge_thresholds;
// Apply to battery profile if not already set
if battery_profile.battery_charge_thresholds.is_none() {
battery_profile.battery_charge_thresholds = Some(global_thresholds);
}
}
// Convert AppConfigToml to AppConfig

View file

@ -257,12 +257,10 @@ fn validate_epb_value(epb: &str) -> Result<()> {
if let Ok(value) = epb.parse::<u8>() {
if value <= 15 {
return Ok(());
} else {
return Err(ControlError::InvalidValueError(format!(
"EPB numeric value must be between 0 and 15, got {}",
value
)));
}
return Err(ControlError::InvalidValueError(format!(
"EPB numeric value must be between 0 and 15, got {value}"
)));
}
// If not a number, check if it's a recognized string value

View file

@ -396,7 +396,11 @@ fn main() {
// Get available platform profiles and validate early if possible
match cpu::get_platform_profiles() {
Ok(available_profiles) => {
if !available_profiles.contains(&profile) {
if available_profiles.contains(&profile) {
info!("Setting platform profile to '{profile}'");
cpu::set_platform_profile(&profile)
.map_err(|e| Box::new(e) as Box<dyn std::error::Error>)
} else {
error!(
"Invalid platform profile: '{}'. Available profiles: {}",
profile,
@ -407,10 +411,6 @@ fn main() {
profile,
available_profiles.join(", ")
))) as Box<dyn std::error::Error>)
} else {
info!("Setting platform profile to '{}'", profile);
cpu::set_platform_profile(&profile)
.map_err(|e| Box::new(e) as Box<dyn std::error::Error>)
}
}
Err(_) => {