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