From b9cce7b634a520ac58587626998261ea05022bb1 Mon Sep 17 00:00:00 2001 From: NotAShelf Date: Thu, 15 May 2025 20:32:59 +0300 Subject: [PATCH] config: prefer a named struct over tuple for battery conf --- src/config/load.rs | 3 ++- src/config/types.rs | 19 +++++++++++++------ src/engine.rs | 7 ++++--- 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/src/config/load.rs b/src/config/load.rs index 470a99d..9679dc0 100644 --- a/src/config/load.rs +++ b/src/config/load.rs @@ -86,7 +86,8 @@ fn load_and_parse_config(path: &Path) -> Result { // 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; + charger_profile.battery_charge_thresholds = + toml_app_config.battery_charge_thresholds.clone(); } if battery_profile.battery_charge_thresholds.is_none() { diff --git a/src/config/types.rs b/src/config/types.rs index 52ef5c9..32c7c38 100644 --- a/src/config/types.rs +++ b/src/config/types.rs @@ -1,6 +1,12 @@ // Configuration types and structures for superfreq use crate::core::TurboSetting; -use serde::Deserialize; +use serde::{Deserialize, Serialize}; + +#[derive(Deserialize, Serialize, Debug, Clone)] +pub struct BatteryChargeThresholds { + pub start: u8, + pub stop: u8, +} // Structs for configuration using serde::Deserialize #[derive(Deserialize, Debug, Clone)] @@ -13,7 +19,8 @@ pub struct ProfileConfig { pub max_freq_mhz: Option, pub platform_profile: Option, pub turbo_auto_settings: Option, - pub battery_charge_thresholds: Option<(u8, u8)>, + #[serde(skip_serializing_if = "Option::is_none")] + pub battery_charge_thresholds: Option, } impl Default for ProfileConfig { @@ -87,7 +94,8 @@ pub struct ProfileConfigToml { pub min_freq_mhz: Option, pub max_freq_mhz: Option, pub platform_profile: Option, - pub battery_charge_thresholds: Option<(u8, u8)>, + #[serde(skip_serializing_if = "Option::is_none")] + pub battery_charge_thresholds: Option, } #[derive(Deserialize, Debug, Clone, Default)] @@ -96,10 +104,9 @@ pub struct AppConfigToml { pub charger: ProfileConfigToml, #[serde(default)] pub battery: ProfileConfigToml, - pub battery_charge_thresholds: Option<(u8, u8)>, + #[serde(skip_serializing_if = "Option::is_none")] + pub battery_charge_thresholds: Option, pub ignored_power_supplies: Option>, - #[serde(default = "default_poll_interval_sec")] - pub poll_interval_sec: u64, #[serde(default)] pub daemon: DaemonConfigToml, } diff --git a/src/engine.rs b/src/engine.rs index 67aa8b6..401d849 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -147,9 +147,10 @@ pub fn determine_and_apply_settings( } // Set battery charge thresholds if configured - if let Some((start_threshold, stop_threshold)) = - selected_profile_config.battery_charge_thresholds - { + if let Some(thresholds) = &selected_profile_config.battery_charge_thresholds { + let start_threshold = thresholds.start; + let stop_threshold = thresholds.stop; + if start_threshold < stop_threshold && stop_threshold <= 100 { info!("Setting battery charge thresholds: {start_threshold}-{stop_threshold}%"); match battery::set_battery_charge_thresholds(start_threshold, stop_threshold) {