1extern crate dirs_sys;
2
3use std::env;
4use std::path::PathBuf;
5
6pub fn home_dir() -> Option<PathBuf> { dirs_sys::home_dir() }
7
8pub fn cache_dir() -> Option<PathBuf> { env::var_os(key:"XDG_CACHE_HOME") .and_then(dirs_sys::is_absolute_path).or_else(|| home_dir().map(|h: PathBuf| h.join(path:".cache"))) }
9pub fn config_dir() -> Option<PathBuf> { env::var_os(key:"XDG_CONFIG_HOME").and_then(dirs_sys::is_absolute_path).or_else(|| home_dir().map(|h: PathBuf| h.join(path:".config"))) }
10pub fn data_dir() -> Option<PathBuf> { env::var_os(key:"XDG_DATA_HOME") .and_then(dirs_sys::is_absolute_path).or_else(|| home_dir().map(|h: PathBuf| h.join(path:".local/share"))) }
11pub fn data_local_dir() -> Option<PathBuf> { data_dir() }
12pub fn preference_dir() -> Option<PathBuf> { config_dir() }
13pub fn runtime_dir() -> Option<PathBuf> { env::var_os(key:"XDG_RUNTIME_DIR").and_then(dirs_sys::is_absolute_path) }
14pub fn state_dir() -> Option<PathBuf> { env::var_os(key:"XDG_STATE_HOME") .and_then(dirs_sys::is_absolute_path).or_else(|| home_dir().map(|h: PathBuf| h.join(path:".local/state"))) }
15pub fn executable_dir() -> Option<PathBuf> { env::var_os(key:"XDG_BIN_HOME") .and_then(dirs_sys::is_absolute_path).or_else(|| home_dir().map(|h: PathBuf| h.join(path:".local/bin"))) }
16
17pub fn audio_dir() -> Option<PathBuf> { dirs_sys::user_dir(user_dir_name:"MUSIC") }
18pub fn desktop_dir() -> Option<PathBuf> { dirs_sys::user_dir(user_dir_name:"DESKTOP") }
19pub fn document_dir() -> Option<PathBuf> { dirs_sys::user_dir(user_dir_name:"DOCUMENTS") }
20pub fn download_dir() -> Option<PathBuf> { dirs_sys::user_dir(user_dir_name:"DOWNLOAD") }
21pub fn font_dir() -> Option<PathBuf> { data_dir().map(|d: PathBuf| d.join(path:"fonts")) }
22pub fn picture_dir() -> Option<PathBuf> { dirs_sys::user_dir(user_dir_name:"PICTURES") }
23pub fn public_dir() -> Option<PathBuf> { dirs_sys::user_dir(user_dir_name:"PUBLICSHARE") }
24pub fn template_dir() -> Option<PathBuf> { dirs_sys::user_dir(user_dir_name:"TEMPLATES") }
25pub fn video_dir() -> Option<PathBuf> { dirs_sys::user_dir(user_dir_name:"VIDEOS") }
26
27#[cfg(test)]
28mod tests {
29 #[test]
30 fn test_file_user_dirs_exists() {
31 let user_dirs_file = ::config_dir().unwrap().join("user-dirs.dirs");
32 println!("{:?} exists: {:?}", user_dirs_file, user_dirs_file.exists());
33 }
34}
35