use std::error::Error;
use std::path::{Path, PathBuf};
use std::process::ExitCode;
use tablero::config::Config;
use tablero::run;
fn main() -> ExitCode {
env_logger::init();
let config = match load_config() {
Ok(config) => config,
Err(e) => {
eprintln!("tablero: {e}");
return ExitCode::FAILURE;
}
};
match run(config) {
Ok(()) => ExitCode::SUCCESS,
Err(e) => {
eprintln!("tablero: {e}");
ExitCode::FAILURE
}
}
}
fn load_config() -> Result<Config, Box<dyn Error>> {
match config_path() {
Some(path) => Ok(Config::load_from_path(path)?),
None => Ok(Config::default()),
}
}
fn config_path() -> Option<PathBuf> {
config_path_from(
std::env::var("XDG_CONFIG_HOME").ok().as_deref(),
std::env::var("HOME").ok().as_deref(),
)
}
fn config_path_from(xdg_config_home: Option<&str>, home: Option<&str>) -> Option<PathBuf> {
if let Some(xdg) = xdg_config_home.filter(|s| !s.is_empty()) {
return Some(Path::new(xdg).join("tablero").join("config.toml"));
}
let home = home.filter(|s| !s.is_empty())?;
Some(
Path::new(home)
.join(".config")
.join("tablero")
.join("config.toml"),
)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn xdg_config_home_is_used_when_set() {
assert_eq!(
config_path_from(Some("/cfg"), Some("/home/u")),
Some(PathBuf::from("/cfg/tablero/config.toml"))
);
}
#[test]
fn falls_back_to_home_config_when_xdg_unset() {
assert_eq!(
config_path_from(None, Some("/home/u")),
Some(PathBuf::from("/home/u/.config/tablero/config.toml"))
);
}
#[test]
fn empty_xdg_is_ignored_in_favor_of_home() {
assert_eq!(
config_path_from(Some(""), Some("/home/u")),
Some(PathBuf::from("/home/u/.config/tablero/config.toml"))
);
}
#[test]
fn no_home_and_no_xdg_resolves_to_no_path() {
assert_eq!(config_path_from(None, None), None);
}
}