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