use ledgeline_core::hledger_conf::UserConfDirs;
use std::path::{Path, PathBuf};
pub const CONFIG_DIR_ENV: &str = "LEDGELINE_CONFIG_DIR";
pub const HLEDGER_ENV: &str = "LEDGELINE_HLEDGER";
pub const IGNORE_USER_CONF_ENV: &str = "LEDGELINE_IGNORE_USER_HLEDGER_CONF";
const APP_DIR: &str = "ledgeline";
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AppDirs {
pub config_dir: Option<PathBuf>,
pub cache_dir: Option<PathBuf>,
pub temp_dir: PathBuf,
pub hledger_bin: Option<PathBuf>,
pub hledger_conf: UserConfDirs,
}
impl AppDirs {
#[must_use]
pub fn from_env() -> Self {
Self {
config_dir: non_empty_env(CONFIG_DIR_ENV)
.or_else(|| dirs::config_dir().map(|base| base.join(APP_DIR))),
cache_dir: dirs::cache_dir().map(|base| base.join(APP_DIR)),
temp_dir: std::env::temp_dir(),
hledger_bin: non_empty_env(HLEDGER_ENV),
hledger_conf: if non_empty_env(IGNORE_USER_CONF_ENV).is_some() {
UserConfDirs::default()
} else {
UserConfDirs {
home: dirs::home_dir(),
xdg_config_home: non_empty_env("XDG_CONFIG_HOME")
.filter(|path| path.is_absolute()),
xdg_default: if cfg!(windows) {
dirs::config_dir()
} else {
None
},
}
},
}
}
#[must_use]
pub fn under(root: impl AsRef<Path>) -> Self {
let root = root.as_ref();
Self {
config_dir: Some(root.join("config")),
cache_dir: Some(root.join("cache")),
temp_dir: root.join("tmp"),
hledger_bin: None,
hledger_conf: UserConfDirs::default(),
}
}
pub fn create_all(&self) -> std::io::Result<()> {
self.config_dir
.iter()
.chain(self.cache_dir.iter())
.chain(std::iter::once(&self.temp_dir))
.try_for_each(std::fs::create_dir_all)
}
#[must_use]
pub fn config_file(&self, name: &str) -> Option<PathBuf> {
self.config_dir.as_ref().map(|dir| dir.join(name))
}
}
fn non_empty_env(var: &str) -> Option<PathBuf> {
std::env::var_os(var)
.filter(|value| !value.is_empty())
.map(PathBuf::from)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn under_places_everything_beneath_the_root_and_names_no_hledger() {
let dirs = AppDirs::under("/container/Library");
assert_eq!(
dirs.config_dir.as_deref(),
Some(Path::new("/container/Library/config"))
);
assert_eq!(
dirs.cache_dir.as_deref(),
Some(Path::new("/container/Library/cache"))
);
assert_eq!(dirs.temp_dir, Path::new("/container/Library/tmp"));
assert_eq!(dirs.hledger_bin, None);
assert_eq!(dirs.hledger_conf, UserConfDirs::default());
assert_eq!(
dirs.config_file("prefs.toml").as_deref(),
Some(Path::new("/container/Library/config/prefs.toml"))
);
}
#[test]
fn create_all_makes_every_named_directory() {
let root = tempfile::TempDir::new().expect("temp dir");
let dirs = AppDirs::under(root.path().join("nested"));
dirs.create_all().expect("create");
assert!(dirs.temp_dir.is_dir());
assert!(dirs.config_dir.as_deref().is_some_and(Path::is_dir));
assert!(dirs.cache_dir.as_deref().is_some_and(Path::is_dir));
}
#[test]
fn no_config_dir_means_no_config_file() {
let dirs = AppDirs {
config_dir: None,
..AppDirs::under("/x")
};
assert_eq!(dirs.config_file("recent.json"), None);
}
#[test]
fn from_env_keeps_the_os_temp_dir() {
assert_eq!(AppDirs::from_env().temp_dir, std::env::temp_dir());
}
}