Skip to content

Commit

Permalink
refactor: Simplify Config default values handling
Browse files Browse the repository at this point in the history
  • Loading branch information
bcyran committed Jan 23, 2025
1 parent b31a60d commit bdd6f46
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 33 deletions.
6 changes: 3 additions & 3 deletions src/actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ pub fn set<P: AsRef<Path>>(
}
}

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()?;
Expand Down Expand Up @@ -199,9 +199,9 @@ fn current_image_index(
}

fn try_get_location(config: &Config) -> Result<Coords> {
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) => {
Expand Down
35 changes: 5 additions & 30 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,25 +104,16 @@ impl Default for Geoclue {
}
}

#[derive(Deserialize, Serialize, Debug)]
#[derive(Deserialize, Serialize, Debug, Default)]
pub struct Config {
pub daemon: Option<Daemon>,
pub geoclue: Option<Geoclue>,
#[serde(default)]
pub daemon: Daemon,
#[serde(default)]
pub geoclue: Geoclue,
pub location: Option<Coords>,
pub setter: Option<Setter>,
}

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> {
Self::load_or_create(Self::find_path()?)
Expand Down Expand Up @@ -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<Coords> {
self.location
.ok_or_else(|| anyhow!("location not set in the configuration"))
Expand Down

0 comments on commit bdd6f46

Please sign in to comment.