| 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("XDG_CACHE_HOME") .and_then(dirs_sys::is_absolute_path).or_else(|| home_dir().map(|h| h.join(".cache"))) } |
| 9 |
pub fn config_dir() -> Option<PathBuf> { env::var_os("XDG_CONFIG_HOME").and_then(dirs_sys::is_absolute_path).or_else(|| home_dir().map(|h| h.join(".config"))) } |
| 10 |
pub fn data_dir() -> Option<PathBuf> { env::var_os("XDG_DATA_HOME") .and_then(dirs_sys::is_absolute_path).or_else(|| home_dir().map(|h| h.join(".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("XDG_RUNTIME_DIR").and_then(dirs_sys::is_absolute_path) } |
| 14 |
pub fn state_dir() -> Option<PathBuf> { env::var_os("XDG_STATE_HOME") .and_then(dirs_sys::is_absolute_path).or_else(|| home_dir().map(|h| h.join(".local/state"))) } |
| 15 |
pub fn executable_dir() -> Option<PathBuf> { env::var_os("XDG_BIN_HOME") .and_then(dirs_sys::is_absolute_path).or_else(|| home_dir().map(|h| h.join(".local/bin"))) } |
| 16 |
|
| 17 |
pub fn audio_dir() -> Option<PathBuf> { dirs_sys::user_dir("MUSIC") } |
| 18 |
pub fn desktop_dir() -> Option<PathBuf> { dirs_sys::user_dir("DESKTOP") } |
| 19 |
pub fn document_dir() -> Option<PathBuf> { dirs_sys::user_dir("DOCUMENTS") } |
| 20 |
pub fn download_dir() -> Option<PathBuf> { dirs_sys::user_dir("DOWNLOAD") } |
| 21 |
pub fn font_dir() -> Option<PathBuf> { data_dir().map(|d| d.join("fonts")) } |
| 22 |
pub fn picture_dir() -> Option<PathBuf> { dirs_sys::user_dir("PICTURES") } |
| 23 |
pub fn public_dir() -> Option<PathBuf> { dirs_sys::user_dir("PUBLICSHARE") } |
| 24 |
pub fn template_dir() -> Option<PathBuf> { dirs_sys::user_dir("TEMPLATES") } |
| 25 |
pub fn video_dir() -> Option<PathBuf> { dirs_sys::user_dir("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 |
} |