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:
parent
d8f609fdef
commit
eb97689bc7
4 changed files with 19 additions and 21 deletions
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
10
src/main.rs
10
src/main.rs
|
@ -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(_) => {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue