use std::path::{Path, PathBuf};
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct UserDirs {
pub desktop: Option<PathBuf>,
pub download: Option<PathBuf>,
pub documents: Option<PathBuf>,
pub music: Option<PathBuf>,
pub pictures: Option<PathBuf>,
pub videos: Option<PathBuf>,
}
pub fn load(config_dir: &Path, home: &Path) -> UserDirs {
let path = config_dir.join("user-dirs.dirs");
match std::fs::read_to_string(&path) {
Ok(text) => parse(&text, home),
Err(_) => fallback(home),
}
}
pub fn fallback(home: &Path) -> UserDirs {
UserDirs {
desktop: Some(home.join("Desktop")),
download: Some(home.join("Downloads")),
documents: Some(home.join("Documents")),
music: Some(home.join("Music")),
pictures: Some(home.join("Pictures")),
videos: Some(home.join("Videos")),
}
}
pub fn parse(contents: &str, home: &Path) -> UserDirs {
let mut dirs = UserDirs::default();
for line in contents.lines() {
let line = line.trim();
if line.is_empty() || line.starts_with('#') {
continue;
}
let Some((key, value)) = line.split_once('=') else {
continue;
};
let key = key.trim();
let Some(value) = unquote(value.trim()) else {
continue;
};
let path = resolve(&value, home);
match key {
"XDG_DESKTOP_DIR" => dirs.desktop = Some(path),
"XDG_DOWNLOAD_DIR" => dirs.download = Some(path),
"XDG_DOCUMENTS_DIR" => dirs.documents = Some(path),
"XDG_MUSIC_DIR" => dirs.music = Some(path),
"XDG_PICTURES_DIR" => dirs.pictures = Some(path),
"XDG_VIDEOS_DIR" => dirs.videos = Some(path),
_ => {}
}
}
dirs
}
fn unquote(value: &str) -> Option<String> {
let value = value.strip_prefix('"')?;
let value = value.strip_suffix('"')?;
Some(value.to_string())
}
fn resolve(value: &str, home: &Path) -> PathBuf {
match value.strip_prefix("$HOME") {
Some(rest) => {
let rest = rest.trim_start_matches('/');
if rest.is_empty() {
home.to_path_buf()
} else {
home.join(rest)
}
}
None => PathBuf::from(value),
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_typical_file_resolves_home_relative_paths() {
let contents = r#"
# This file is written by xdg-user-dirs-update
XDG_DESKTOP_DIR="$HOME/Desktop"
XDG_DOCUMENTS_DIR="$HOME/Dokumente"
XDG_DOWNLOAD_DIR="$HOME/Downloads"
"#;
let home = Path::new("/home/alex");
let dirs = parse(contents, home);
assert_eq!(dirs.desktop, Some(PathBuf::from("/home/alex/Desktop")));
assert_eq!(dirs.documents, Some(PathBuf::from("/home/alex/Dokumente")));
assert_eq!(dirs.download, Some(PathBuf::from("/home/alex/Downloads")));
}
#[test]
fn a_key_the_file_never_mentions_stays_none_rather_than_guessed() {
let contents = r#"XDG_DOCUMENTS_DIR="$HOME/Documents""#;
let dirs = parse(contents, Path::new("/home/alex"));
assert_eq!(dirs.documents, Some(PathBuf::from("/home/alex/Documents")));
assert_eq!(dirs.music, None, "a directory the file doesn't mention is not a guess");
}
#[test]
fn an_unrecognised_line_is_skipped_not_fatal() {
let contents = "this is not a valid line at all\nXDG_MUSIC_DIR=\"$HOME/Music\"";
let dirs = parse(contents, Path::new("/home/alex"));
assert_eq!(dirs.music, Some(PathBuf::from("/home/alex/Music")));
}
#[test]
fn a_missing_file_falls_back_to_english_names_under_home() {
let dir = tempfile::tempdir().unwrap();
let home = Path::new("/home/alex");
let dirs = load(dir.path(), home);
assert_eq!(dirs, fallback(home));
assert_eq!(dirs.documents, Some(PathBuf::from("/home/alex/Documents")));
}
#[test]
fn a_present_file_is_used_instead_of_the_fallback() {
let dir = tempfile::tempdir().unwrap();
std::fs::write(
dir.path().join("user-dirs.dirs"),
r#"XDG_DOCUMENTS_DIR="$HOME/OnlyThis""#,
)
.unwrap();
let home = Path::new("/home/alex");
let dirs = load(dir.path(), home);
assert_eq!(dirs.documents, Some(PathBuf::from("/home/alex/OnlyThis")));
assert_eq!(dirs.download, None, "present file wins outright, no fallback merge");
}
}