use std::error::Error;
use std::process::ExitCode;
use tablero::config::{Config, config_file_path};
use tablero::run;
fn main() -> ExitCode {
env_logger::init();
let path = config_file_path();
let config = match load_config(path.as_deref()) {
Ok(config) => config,
Err(e) => {
eprintln!("tablero: {e}");
return ExitCode::FAILURE;
}
};
match run(config, path) {
Ok(()) => ExitCode::SUCCESS,
Err(e) => {
eprintln!("tablero: {e}");
ExitCode::FAILURE
}
}
}
fn load_config(path: Option<&std::path::Path>) -> Result<Config, Box<dyn Error>> {
match path {
Some(path) => Ok(Config::load_from_path(path)?),
None => Ok(Config::default()),
}
}
#[cfg(test)]
mod tests {
use std::path::PathBuf;
use tablero::config::config_file_path_from;
#[test]
fn xdg_config_home_is_used_when_set() {
assert_eq!(
config_file_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_file_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_file_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_file_path_from(None, None), None);
}
}