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 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" ))) } |
11 | pub fn data_local_dir() -> Option<PathBuf> { data_dir() } |
12 | pub fn preference_dir() -> Option<PathBuf> { config_dir() } |
13 | pub fn runtime_dir() -> Option<PathBuf> { env::var_os(key:"XDG_RUNTIME_DIR" ).and_then(dirs_sys::is_absolute_path) } |
14 | 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" ))) } |
15 | 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" ))) } |
16 | |
17 | pub fn audio_dir() -> Option<PathBuf> { dirs_sys::user_dir(user_dir_name:"MUSIC" ) } |
18 | pub fn desktop_dir() -> Option<PathBuf> { dirs_sys::user_dir(user_dir_name:"DESKTOP" ) } |
19 | pub fn document_dir() -> Option<PathBuf> { dirs_sys::user_dir(user_dir_name:"DOCUMENTS" ) } |
20 | pub fn download_dir() -> Option<PathBuf> { dirs_sys::user_dir(user_dir_name:"DOWNLOAD" ) } |
21 | pub fn font_dir() -> Option<PathBuf> { data_dir().map(|d: PathBuf| d.join(path:"fonts" )) } |
22 | pub fn picture_dir() -> Option<PathBuf> { dirs_sys::user_dir(user_dir_name:"PICTURES" ) } |
23 | pub fn public_dir() -> Option<PathBuf> { dirs_sys::user_dir(user_dir_name:"PUBLICSHARE" ) } |
24 | pub fn template_dir() -> Option<PathBuf> { dirs_sys::user_dir(user_dir_name:"TEMPLATES" ) } |
25 | pub fn video_dir() -> Option<PathBuf> { dirs_sys::user_dir(user_dir_name:"VIDEOS" ) } |
26 | |
27 | #[cfg (test)] |
28 | mod 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 | |