use std::path::PathBuf;
pub const TOOL: &str = "pidge";
#[cfg(not(windows))]
pub fn config_dir() -> Option<PathBuf> {
xdg_dir(
std::env::var_os("XDG_CONFIG_HOME"),
dirs::home_dir(),
".config",
)
}
#[cfg(windows)]
pub fn config_dir() -> Option<PathBuf> {
dirs::config_dir().map(|d| d.join(TOOL))
}
#[cfg(not(windows))]
pub fn cache_dir() -> Option<PathBuf> {
xdg_dir(
std::env::var_os("XDG_CACHE_HOME"),
dirs::home_dir(),
".cache",
)
}
#[cfg(windows)]
pub fn cache_dir() -> Option<PathBuf> {
dirs::cache_dir().map(|d| d.join(TOOL))
}
#[cfg(not(windows))]
fn xdg_dir(
xdg: Option<std::ffi::OsString>,
home: Option<PathBuf>,
fallback: &str,
) -> Option<PathBuf> {
match xdg.map(PathBuf::from) {
Some(dir) if dir.is_absolute() => Some(dir.join(TOOL)),
_ => home.map(|h| h.join(fallback).join(TOOL)),
}
}
#[cfg(all(test, not(windows)))]
mod tests {
use super::*;
#[test]
fn an_absolute_xdg_variable_wins() {
let dir = xdg_dir(Some("/xdg".into()), Some("/home/k".into()), ".config");
assert_eq!(dir, Some(PathBuf::from("/xdg").join(TOOL)));
}
#[test]
fn otherwise_the_dot_folder_in_home() {
let config = xdg_dir(None, Some("/Users/k".into()), ".config");
assert_eq!(config, Some(PathBuf::from("/Users/k/.config").join(TOOL)));
let cache = xdg_dir(None, Some("/Users/k".into()), ".cache");
assert_eq!(cache, Some(PathBuf::from("/Users/k/.cache").join(TOOL)));
}
#[test]
fn empty_or_relative_xdg_variables_are_ignored() {
for xdg in ["", "relative/dir"] {
let dir = xdg_dir(Some(xdg.into()), Some("/home/k".into()), ".config");
assert_eq!(
dir,
Some(PathBuf::from("/home/k/.config").join(TOOL)),
"{xdg:?}"
);
}
}
#[test]
fn no_home_and_no_xdg_is_none() {
assert_eq!(xdg_dir(None, None, ".config"), None);
}
}