use std::env::home_dir;
use std::path::PathBuf;
pub fn cache(app_name: &str) -> Option<PathBuf>
{
if app_name.is_empty() {
return None;
}
cache_home().map(|mut dir| { dir.push(app_name); dir })
}
pub fn cache_home() -> Option<PathBuf>
{
#[cfg(unix)]
fn _cache_home() -> Option<PathBuf>
{
home_dir().map(|mut dir| { dir.push(".cache"); dir })
}
#[cfg(windows)]
fn _cache_home() -> Option<PathBuf>
{
home_dir().map(|mut dir| {
dir.push("Local Settings");
dir.push("Cache");
dir
})
}
_cache_home()
}
#[test]
#[cfg(test)]
fn tests()
{
#[cfg(unix)]
fn _tests()
{
match home_dir() {
Some(mut dir) => {
dir.push(".cache");
dir.push("blub");
let dir_str = format!("{}", dir.display());
let cache_str = format!("{}", cache("blub").unwrap().display());
assert_eq!(dir_str, cache_str);
}
None => assert!(false, "Couldn't get homedir!")
}
}
_tests()
}