From c50f5c88126d60935864d3d6b1223da5bf59dac1 Mon Sep 17 00:00:00 2001 From: RGBCube Date: Wed, 11 Jun 2025 23:54:19 +0300 Subject: [PATCH] config: integrate default config --- config.toml | 4 ++-- src/config.rs | 20 ++++++++++++++------ src/main.rs | 6 +++--- 3 files changed, 19 insertions(+), 11 deletions(-) diff --git a/config.toml b/config.toml index a89f807..ce2dd33 100644 --- a/config.toml +++ b/config.toml @@ -37,11 +37,11 @@ cpu.governor = "performance" cpu.energy-performance-preference = "performance" cpu.turbo = true -# Performance mode when charging. +# Performance mode when not discharging. [[rule]] priority = 70 if.all = [ - "?charging", + { not = "?discharging" }, { value = "%cpu-usage", is-more-than = 0.1 }, { value = "$cpu-temperature", is-less-than = 80.0 }, ] diff --git a/src/config.rs b/src/config.rs index b3d214e..ef2e14d 100644 --- a/src/config.rs +++ b/src/config.rs @@ -471,13 +471,21 @@ pub struct DaemonConfig { } impl DaemonConfig { - pub fn load_from(path: &Path) -> anyhow::Result { - let contents = fs::read_to_string(path).with_context(|| { - format!("failed to read config from '{path}'", path = path.display()) - })?; + pub fn load_from(path: Option<&Path>) -> anyhow::Result { + let contents = if let Some(path) = path { + &fs::read_to_string(path).with_context(|| { + format!("failed to read config from '{path}'", path = path.display()) + })? + } else { + include_str!("../config.toml") + }; - let mut config: Self = toml::from_str(&contents) - .with_context(|| format!("failed to parse file at '{path}'", path = path.display(),))?; + let mut config: Self = toml::from_str(contents).with_context(|| { + path.map_or( + "failed to parse builtin default config, this is a bug".to_owned(), + |p| format!("failed to parse file at '{path}'", path = p.display()), + ) + })?; { let mut priorities = Vec::with_capacity(config.rules.len()); diff --git a/src/main.rs b/src/main.rs index feed86a..bc25436 100644 --- a/src/main.rs +++ b/src/main.rs @@ -35,7 +35,7 @@ enum Command { /// The daemon config path. #[arg(long, env = "WATT_CONFIG")] - config: PathBuf, + config: Option, }, /// CPU metadata and modification utility. @@ -86,8 +86,8 @@ fn real_main() -> anyhow::Result<()> { match cli.command { Command::Watt { config, .. } => { - let config = - config::DaemonConfig::load_from(&config).context("failed to load daemon config")?; + let config = config::DaemonConfig::load_from(config.as_deref()) + .context("failed to load daemon config")?; daemon::run(config) }