Skip to content

Commit

Permalink
feat: Add comments and examples to the default written config file
Browse files Browse the repository at this point in the history
  • Loading branch information
bcyran committed Sep 15, 2024
1 parent 54a4617 commit c3e986e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 22 deletions.
39 changes: 21 additions & 18 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::fs::File;
use std::io::Write;
use std::{
env, fs,
path::{Path, PathBuf},
Expand All @@ -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<String>,
Expand Down Expand Up @@ -44,11 +56,10 @@ impl Config {

fn load_or_create<P: AsRef<Path>>(path: P) -> Result<Self> {
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<P: AsRef<Path>>(path: P) -> Result<Self> {
Expand All @@ -60,27 +71,19 @@ impl Config {
Ok(config)
}

fn create_default<P: AsRef<Path>>(path: P) -> Result<Self> {
fn create_default<P: AsRef<Path>>(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<P: AsRef<Path>>(&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(())
}

Expand Down
5 changes: 1 addition & 4 deletions tests/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,17 +78,14 @@ 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
.run(&["set", EXAMPLE_SUN.to_str().unwrap()])
.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]
Expand Down

0 comments on commit c3e986e

Please sign in to comment.