From bdd6f4661f93fd003003f448ec5e76fda4b97983 Mon Sep 17 00:00:00 2001 From: Bazyli Cyran Date: Thu, 23 Jan 2025 17:47:30 +0100 Subject: [PATCH] refactor: Simplify Config default values handling --- src/actions.rs | 6 +++--- src/config.rs | 35 +++++------------------------------ 2 files changed, 8 insertions(+), 33 deletions(-) diff --git a/src/actions.rs b/src/actions.rs index 9864b6a..d1f59dd 100644 --- a/src/actions.rs +++ b/src/actions.rs @@ -73,7 +73,7 @@ pub fn set>( } } - let update_interval_seconds = config.update_interval_seconds(); + let update_interval_seconds = config.daemon.update_interval_seconds; debug!("sleeping for {update_interval_seconds} seconds"); if interruptible_sleep(Duration::from_secs(update_interval_seconds), termination_rx)? { unset_wallpaper()?; @@ -199,9 +199,9 @@ fn current_image_index( } fn try_get_location(config: &Config) -> Result { - let geoclue_timeout = Duration::from_millis(config.geoclue_timeout()); + let geoclue_timeout = Duration::from_millis(config.geoclue.timeout); - let maybe_location = match (config.geoclue_enabled(), config.geoclue_preferred()) { + let maybe_location = match (config.geoclue.enable, config.geoclue.prefer) { (true, true) => match geoclue::get_location(geoclue_timeout) { geoclue_ok @ Ok(_) => geoclue_ok, Err(e) => { diff --git a/src/config.rs b/src/config.rs index 3da1add..6800d76 100644 --- a/src/config.rs +++ b/src/config.rs @@ -104,25 +104,16 @@ impl Default for Geoclue { } } -#[derive(Deserialize, Serialize, Debug)] +#[derive(Deserialize, Serialize, Debug, Default)] pub struct Config { - pub daemon: Option, - pub geoclue: Option, + #[serde(default)] + pub daemon: Daemon, + #[serde(default)] + pub geoclue: Geoclue, pub location: Option, pub setter: Option, } -impl Default for Config { - fn default() -> Self { - Self { - daemon: Some(Daemon::default()), - geoclue: Some(Geoclue::default()), - location: None, - setter: None, - } - } -} - impl Config { pub fn find() -> Result { Self::load_or_create(Self::find_path()?) @@ -173,22 +164,6 @@ impl Config { Ok(()) } - pub fn update_interval_seconds(&self) -> u64 { - self.daemon.unwrap_or_default().update_interval_seconds - } - - pub fn geoclue_enabled(&self) -> bool { - self.geoclue.unwrap_or_default().enable - } - - pub fn geoclue_preferred(&self) -> bool { - self.geoclue.unwrap_or_default().prefer - } - - pub fn geoclue_timeout(&self) -> u64 { - self.geoclue.unwrap_or_default().timeout - } - pub fn try_get_location(&self) -> Result { self.location .ok_or_else(|| anyhow!("location not set in the configuration"))