use std::env;
use std::path::PathBuf;
const APP_NAME: &str = env!("CARGO_PKG_NAME");
#[cfg(any(target_os = "macos", target_os = "linux"))]
const CONFIG_ENV_VAR: &str = "XDG_CONFIG_HOME";
#[cfg(target_os = "windows")]
const CONFIG_ENV_VAR: &str = "APPDATA";
#[cfg(target_os = "macos")]
const DEFAULT_CONFIG_PATH: &str = "~/Library/Application Support/";
#[cfg(target_os = "linux")]
const DEFAULT_CONFIG_PATH: &str = "~/.config/";
#[cfg(target_os = "windows")]
const DEFAULT_CONFIG_PATH: &str = "~\\AppData\\Roaming";
#[cfg(any(target_os = "macos", target_os = "linux"))]
const LOG_PATH_ENV_VAR: &str = "XDG_DATA_HOME";
#[cfg(target_os = "windows")]
const LOG_PATH_ENV_VAR: &str = "LOCALAPPDATA";
#[cfg(target_os = "macos")]
const DEFAULT_LOG_PATH: &str = "~/Library/Application Support/";
#[cfg(target_os = "linux")]
const DEFAULT_LOG_PATH: &str = "~/.local/share/";
#[cfg(target_os = "windows")]
const DEFAULT_LOG_PATH: &str = "~\\AppData\\Local";
pub struct AppDirectories {
pub config_dir: PathBuf,
pub log_dir: PathBuf,
}
impl AppDirectories {
pub fn load_directories() -> Self {
let config_dir = Self::get_config_directory();
let log_dir = Self::get_log_directory();
Self {
config_dir,
log_dir,
}
}
fn get_config_directory() -> PathBuf {
env::var_os(CONFIG_ENV_VAR)
.map_or_else(|| PathBuf::from(DEFAULT_CONFIG_PATH), |s| PathBuf::from(&s))
.join(APP_NAME)
}
fn get_log_directory() -> PathBuf {
env::var_os(LOG_PATH_ENV_VAR)
.map_or_else(|| PathBuf::from(DEFAULT_LOG_PATH), |s| PathBuf::from(&s))
.join(APP_NAME)
}
}