use std::path::PathBuf;
pub fn config_home() -> PathBuf {
std::env::var_os("XDG_CONFIG_HOME")
.filter(|os_str| !os_str.is_empty())
.map(PathBuf::from)
.unwrap_or_else(|| {
PathBuf::from(
std::env::var_os("HOME").expect("Neither XDG_CONFIG_HOME nor HOME is set"),
)
.join(".config")
})
}
pub fn data_home() -> PathBuf {
std::env::var_os("XDG_DATA_HOME")
.filter(|os_str| !os_str.is_empty())
.map(PathBuf::from)
.unwrap_or_else(|| {
PathBuf::from(std::env::var_os("HOME").expect("Neither XDG_DATA_HOME nor HOME is set"))
.join(".local/share")
})
}
pub fn app_data_dir(app_name: &str) -> anyhow::Result<PathBuf> {
let app_dir = data_home().join(app_name);
if !app_dir.exists() {
std::fs::create_dir_all(&app_dir)?;
}
Ok(app_dir)
}
#[allow(dead_code)]
pub fn app_config_dir(app_name: &str) -> anyhow::Result<PathBuf> {
let app_dir = config_home().join(app_name);
if !app_dir.exists() {
std::fs::create_dir_all(&app_dir)?;
}
Ok(app_dir)
}