A cross-platform Rust library for resolving XDG and platform-specific directories with proper fallbacks.
at main 2.0 kB view raw
1use std::{env, path::PathBuf}; 2 3use crate::xdg; 4 5const APP_SUPPORT: &str = "Library/Application Support"; 6 7pub fn bin_home() -> Option<PathBuf> { 8 xdg::resolve_path_with_fallback(xdg::BIN_HOME, ".local/bin") 9} 10 11pub fn cache_home() -> Option<PathBuf> { 12 xdg::resolve_path_with_fallback(xdg::CACHE_HOME, "Library/Caches") 13} 14 15pub fn config_home() -> Option<PathBuf> { 16 xdg::resolve_path_with_fallback(xdg::CONFIG_HOME, APP_SUPPORT) 17} 18 19pub fn config_local() -> Option<PathBuf> { 20 config_home() 21} 22 23pub fn data_home() -> Option<PathBuf> { 24 xdg::resolve_path_with_fallback(xdg::DATA_HOME, APP_SUPPORT) 25} 26 27pub fn data_local() -> Option<PathBuf> { 28 data_home() 29} 30 31pub fn desktop() -> Option<PathBuf> { 32 xdg::resolve_path_with_fallback(xdg::DESKTOP_DIR, "Desktop") 33} 34 35pub fn documents() -> Option<PathBuf> { 36 xdg::resolve_path_with_fallback(xdg::DOCUMENTS_DIR, "Documents") 37} 38 39pub fn downloads() -> Option<PathBuf> { 40 xdg::resolve_path_with_fallback(xdg::DOWNLOAD_DIR, "Downloads") 41} 42 43pub fn fonts() -> Option<PathBuf> { 44 env::home_dir().map(|p| p.join("Library/Fonts")) 45} 46 47pub fn music() -> Option<PathBuf> { 48 xdg::resolve_path_with_fallback(xdg::MUSIC_DIR, "Music") 49} 50 51pub fn pictures() -> Option<PathBuf> { 52 xdg::resolve_path_with_fallback(xdg::PICTURES_DIR, "Pictures") 53} 54 55pub fn preferences() -> Option<PathBuf> { 56 env::home_dir().map(|p| p.join("Library/Preferences")) 57} 58 59pub fn publicshare() -> Option<PathBuf> { 60 xdg::resolve_path_with_fallback(xdg::PUBLICSHARE_DIR, "Public") 61} 62 63pub fn runtime() -> Option<PathBuf> { 64 xdg::resolve_path(xdg::RUNTIME_DIR) 65 .or_else(|| env::var("TMPDIR").ok().map(PathBuf::from).or_else(|| Some(PathBuf::from("/tmp")))) 66} 67 68pub fn state_home() -> Option<PathBuf> { 69 xdg::resolve_path_with_fallback(xdg::STATE_HOME, APP_SUPPORT) 70} 71 72pub fn templates() -> Option<PathBuf> { 73 xdg::resolve_path_with_fallback(xdg::TEMPLATES_DIR, "Templates") 74} 75 76pub fn videos() -> Option<PathBuf> { 77 xdg::resolve_path_with_fallback(xdg::VIDEOS_DIR, "Movies") 78}