use crate::constants::env::system;
use std::path::PathBuf;
pub fn get_config_dir() -> Option<PathBuf> {
#[cfg(target_os = "windows")]
{
std::env::var(system::APPDATA).ok().map(PathBuf::from)
}
#[cfg(target_os = "macos")]
{
std::env::var(system::HOME)
.ok()
.map(|h| PathBuf::from(h).join("Library/Application Support"))
}
#[cfg(target_os = "linux")]
{
std::env::var(system::XDG_CONFIG_HOME)
.ok()
.map(PathBuf::from)
.or_else(|| {
std::env::var(system::HOME)
.ok()
.map(|h| PathBuf::from(h).join(".config"))
})
}
#[cfg(not(any(target_os = "windows", target_os = "macos", target_os = "linux")))]
{
None
}
}
pub fn get_data_dir() -> Option<PathBuf> {
#[cfg(target_os = "windows")]
{
std::env::var(system::LOCALAPPDATA).ok().map(PathBuf::from)
}
#[cfg(target_os = "macos")]
{
std::env::var(system::HOME)
.ok()
.map(|h| PathBuf::from(h).join("Library/Application Support"))
}
#[cfg(target_os = "linux")]
{
std::env::var(system::XDG_DATA_HOME)
.ok()
.map(PathBuf::from)
.or_else(|| {
std::env::var(system::HOME)
.ok()
.map(|h| PathBuf::from(h).join(".local/share"))
})
}
#[cfg(not(any(target_os = "windows", target_os = "macos", target_os = "linux")))]
{
None
}
}
pub fn get_cache_dir() -> Option<PathBuf> {
#[cfg(target_os = "windows")]
{
std::env::var(system::LOCALAPPDATA)
.ok()
.map(|p| PathBuf::from(p).join("Cache"))
}
#[cfg(target_os = "macos")]
{
std::env::var(system::HOME)
.ok()
.map(|h| PathBuf::from(h).join("Library/Caches"))
}
#[cfg(target_os = "linux")]
{
std::env::var(system::XDG_CACHE_HOME)
.ok()
.map(PathBuf::from)
.or_else(|| {
std::env::var(system::HOME)
.ok()
.map(|h| PathBuf::from(h).join(".cache"))
})
}
#[cfg(not(any(target_os = "windows", target_os = "macos", target_os = "linux")))]
{
None
}
}
pub fn get_temp_dir() -> PathBuf {
std::env::temp_dir()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_get_temp_dir() {
let temp = get_temp_dir();
assert!(temp.to_string_lossy().contains("tmp") || temp.to_string_lossy().contains("temp"));
}
}