use std::convert;
use std::env;
use std::path::{ Path, PathBuf };
use error::{ Result, DirsError };
pub struct Directories {
bin_home: PathBuf,
cache_home: PathBuf,
config_home: PathBuf,
data_home: PathBuf,
}
impl Directories {
pub fn with_prefix<P>(prefix: P, _: P) -> Result<Directories>
where P: AsRef<Path>
{
let make_path_fallback = |fallback_path, prefix| {
let home = env::home_dir().ok_or(DirsError::HomeMissing)?;
let mut path = home;
if path.as_os_str() == "" {
return Err(DirsError::HomeMissing);
}
path.push(fallback_path);
path.push(prefix);
Ok(path)
};
let make_path = |var, fallback_path, prefix|
if let Some(xdg) = env::var_os(var) {
let mut path: PathBuf = convert::From::from(xdg);
if path.as_os_str() == "" {
return Err(DirsError::HomeMissing);
}
path.push(prefix);
Ok(path)
} else {
make_path_fallback(fallback_path, prefix)
};
let bin = make_path_fallback(".local/bin", prefix.as_ref());
let cache = make_path("XDG_CACHE_HOME", ".cache", prefix.as_ref());
let config = make_path("XDG_CONFIG_HOME", ".config", prefix.as_ref());
let data = make_path("XDG_DATA_HOME", ".local/share", prefix.as_ref());
Ok(Directories {
bin_home: bin?,
cache_home: cache?,
config_home: config?,
data_home: data?,
})
}
pub fn bin_home(&self) -> &Path {
&self.bin_home
}
pub fn cache_home(&self) -> &Path {
&self.cache_home
}
pub fn config_home(&self) -> &Path {
&self.config_home
}
pub fn data_home(&self) -> &Path {
&self.data_home
}
}