fix(persist): use XDG paths on macOS

Config and state now live in ~/.config/soulseek-rs and
~/.local/share/soulseek-rs instead of Application Support.
This commit is contained in:
Michel de Graaf
2026-07-24 14:02:12 +02:00
parent 298ec5aad5
commit 37d146b907
+35 -2
View File
@@ -1,3 +1,4 @@
#[cfg(not(target_os = "macos"))]
use directories::ProjectDirs;
use std::path::PathBuf;
@@ -9,7 +10,7 @@ pub fn config_file() -> Option<PathBuf> {
if let Ok(dir) = std::env::var("SOULSEEK_CONFIG_DIR") {
return Some(PathBuf::from(dir).join("config.toml"));
}
project_dirs().map(|d| d.config_dir().join("config.toml"))
config_dir().map(|d| d.join("config.toml"))
}
#[must_use]
@@ -17,9 +18,41 @@ pub fn state_dir() -> Option<PathBuf> {
if let Ok(dir) = std::env::var("SOULSEEK_STATE_DIR") {
return Some(PathBuf::from(dir));
}
project_dirs().map(|d| d.data_dir().join("state"))
data_dir().map(|d| d.join("state"))
}
// macOS follows the XDG layout (~/.config, ~/.local/share) instead of
// Application Support, by request. Other platforms keep the crate defaults.
#[cfg(target_os = "macos")]
fn config_dir() -> Option<PathBuf> {
xdg_dir("XDG_CONFIG_HOME", ".config")
}
#[cfg(target_os = "macos")]
fn data_dir() -> Option<PathBuf> {
xdg_dir("XDG_DATA_HOME", ".local/share")
}
#[cfg(target_os = "macos")]
fn xdg_dir(env: &str, fallback: &str) -> Option<PathBuf> {
let base = match std::env::var_os(env) {
Some(dir) if !dir.is_empty() => PathBuf::from(dir),
_ => directories::UserDirs::new()?.home_dir().join(fallback),
};
Some(base.join("soulseek-rs"))
}
#[cfg(not(target_os = "macos"))]
fn config_dir() -> Option<PathBuf> {
project_dirs().map(|d| d.config_dir().to_path_buf())
}
#[cfg(not(target_os = "macos"))]
fn data_dir() -> Option<PathBuf> {
project_dirs().map(|d| d.data_dir().to_path_buf())
}
#[cfg(not(target_os = "macos"))]
fn project_dirs() -> Option<ProjectDirs> {
ProjectDirs::from("", "", "soulseek-rs")
}