1use std::env;
2use std::path::PathBuf;
3
4pub fn home_dir() -> Option<PathBuf> { dirs_sys_next::home_dir() }
5pub 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"))) }
6pub 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"))) }
7pub 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"))) }
8pub fn data_local_dir() -> Option<PathBuf> { data_dir() }
9pub fn runtime_dir() -> Option<PathBuf> { env::var_os(key:"XDG_RUNTIME_DIR").and_then(dirs_sys_next::is_absolute_path) }
10pub 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}
15pub fn audio_dir() -> Option<PathBuf> { dirs_sys_next::user_dir(user_dir_name:"MUSIC") }
16pub fn desktop_dir() -> Option<PathBuf> { dirs_sys_next::user_dir(user_dir_name:"DESKTOP") }
17pub fn document_dir() -> Option<PathBuf> { dirs_sys_next::user_dir(user_dir_name:"DOCUMENTS") }
18pub fn download_dir() -> Option<PathBuf> { dirs_sys_next::user_dir(user_dir_name:"DOWNLOAD") }
19pub fn font_dir() -> Option<PathBuf> { data_dir().map(|d: PathBuf| d.join(path:"fonts")) }
20pub fn picture_dir() -> Option<PathBuf> { dirs_sys_next::user_dir(user_dir_name:"PICTURES") }
21pub fn public_dir() -> Option<PathBuf> { dirs_sys_next::user_dir(user_dir_name:"PUBLICSHARE") }
22pub fn template_dir() -> Option<PathBuf> { dirs_sys_next::user_dir(user_dir_name:"TEMPLATES") }
23pub fn video_dir() -> Option<PathBuf> { dirs_sys_next::user_dir(user_dir_name:"VIDEOS") }
24
25#[cfg(test)]
26mod 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