diff --git a/src/config.rs b/src/config.rs index e5d40d3..8a8a771 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,5 +1,3 @@ -use std::fs::File; -use std::io::Write; use std::{ env, fs, path::{Path, PathBuf}, @@ -14,6 +12,20 @@ use crate::geo::Coords; const CONFIG_FILE_NAME: &str = "config.toml"; +const DEFAULT_CONFIG_FILE_CONTENT: &str = "\ +# Configuration file for timewall + +# Set your geographical location coordinates here +[location] +lat = 51.11 +lon = 17.02 + +# Uncomment and adjust the following section to use a custom wallpaper setter command. +# The example uses `swww`: https://github.com/LGFae/swww. +# [setter] +# command = ['swww', 'img', '%f'] +"; + #[derive(Deserialize, Serialize, Debug)] pub struct Setter { pub command: Vec, @@ -44,11 +56,10 @@ impl Config { fn load_or_create>(path: P) -> Result { let path = path.as_ref(); - if path.exists() { - Self::load(path) - } else { - Self::create_default(path) + if !path.exists() { + Self::create_default(path)?; } + Self::load(path) } fn load>(path: P) -> Result { @@ -60,27 +71,19 @@ impl Config { Ok(config) } - fn create_default>(path: P) -> Result { + fn create_default>(path: P) -> Result<()> { let path = path.as_ref(); let config_dir = path.parent().unwrap(); if !config_dir.exists() { fs::create_dir_all(config_dir).context("couldn't create config directory")?; } - let config = Self::default(); - config - .write(path) - .with_context(|| "couldn't write the configuration file")?; + fs::write(path, DEFAULT_CONFIG_FILE_CONTENT).with_context(|| { + format!("couldn't write default configuration to {}", path.display()) + })?; eprintln!("Default config written to {}.", path.display()); eprintln!("You should probably adjust it to your needs!"); - Ok(config) - } - - fn write>(&self, path: P) -> Result<()> { - let path = path.as_ref(); - let mut config_file = File::create(path)?; - config_file.write_all(toml::to_string_pretty(self)?.as_bytes())?; Ok(()) } diff --git a/tests/set.rs b/tests/set.rs index e3a1d7d..aec28d6 100644 --- a/tests/set.rs +++ b/tests/set.rs @@ -78,7 +78,6 @@ fn test_runs_command(testenv: TestEnv) { #[rstest] fn test_creates_config(testenv: TestEnv) { let config_path = testenv.config_dir.child("config.toml"); - let expected_config = "[location]\nlat = 51.11\nlon = 17.02\n"; let expected_stderr = format!("Default config written to {}", config_path.display()); testenv @@ -86,9 +85,7 @@ fn test_creates_config(testenv: TestEnv) { .success() .stderr(predicate::str::contains(expected_stderr)); - config_path - .assert(predicate::path::is_file()) - .assert(predicate::eq(expected_config)); + config_path.assert(predicate::path::is_file()); } #[rstest]