Skip to content

Commit

Permalink
Add config_local_dir for non-roaming configs
Browse files Browse the repository at this point in the history
only tested on Linux.
  • Loading branch information
ReactorScram authored and soc committed Sep 12, 2022
1 parent d02685f commit 95ee381
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ pub struct ProjectDirs {
// base directories
cache_dir: PathBuf,
config_dir: PathBuf,
config_local_dir: PathBuf,
data_dir: PathBuf,
data_local_dir: PathBuf,
preference_dir: PathBuf,
Expand Down Expand Up @@ -441,6 +442,16 @@ impl ProjectDirs {
pub fn config_dir(&self) -> &Path {
self.config_dir.as_path()
}
/// Returns the path to the project's local config directory.
///
/// |Platform | Value | Example |
/// | ------- | ----------------------------------------------------------------------- | -------------------------------------------------------------- |
/// | Linux | `$XDG_CONFIG_HOME`/`_project_path_` or `$HOME`/.config/`_project_path_` | /home/alice/.config/barapp |
/// | macOS | `$HOME`/Library/Application Support/`_project_path_` | /Users/Alice/Library/Application Support/com.Foo-Corp.Bar-App |
/// | Windows | `{FOLDERID_LocalAppData}`\\`_project_path_`\\config | C:\Users\Alice\AppData\Local\Foo Corp\Bar App\config |
pub fn config_local_dir(&self) -> &Path {
self.config_local_dir.as_path()
}
/// Returns the path to the project's data directory.
///
/// |Platform | Value | Example |
Expand Down
2 changes: 2 additions & 0 deletions src/lin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ pub fn project_dirs_from_path(project_path: PathBuf) -> Option<ProjectDirs> {
if let Some(home_dir) = dirs_sys::home_dir() {
let cache_dir = env::var_os("XDG_CACHE_HOME") .and_then(dirs_sys::is_absolute_path).unwrap_or_else(|| home_dir.join(".cache")).join(&project_path);
let config_dir = env::var_os("XDG_CONFIG_HOME").and_then(dirs_sys::is_absolute_path).unwrap_or_else(|| home_dir.join(".config")).join(&project_path);
let config_local_dir = config_dir.clone();
let data_dir = env::var_os("XDG_DATA_HOME") .and_then(dirs_sys::is_absolute_path).unwrap_or_else(|| home_dir.join(".local/share")).join(&project_path);
let data_local_dir = data_dir.clone();
let preference_dir = config_dir.clone();
Expand All @@ -73,6 +74,7 @@ pub fn project_dirs_from_path(project_path: PathBuf) -> Option<ProjectDirs> {
project_path: project_path,
cache_dir: cache_dir,
config_dir: config_dir,
config_local_dir,
data_dir: data_dir,
data_local_dir: data_local_dir,
preference_dir: preference_dir,
Expand Down
2 changes: 2 additions & 0 deletions src/mac.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ pub fn project_dirs_from_path(project_path: PathBuf) -> Option<ProjectDirs> {
if let Some(home_dir) = dirs_sys::home_dir() {
let cache_dir = home_dir.join("Library/Caches").join(&project_path);
let config_dir = home_dir.join("Library/Application Support").join(&project_path);
let config_local_dir = config_dir.clone();
let data_dir = home_dir.join("Library/Application Support").join(&project_path);
let data_local_dir = data_dir.clone();
let preference_dir = home_dir.join("Library/Preferences").join(&project_path);
Expand All @@ -72,6 +73,7 @@ pub fn project_dirs_from_path(project_path: PathBuf) -> Option<ProjectDirs> {
project_path: project_path,
cache_dir: cache_dir,
config_dir: config_dir,
config_local_dir,
data_dir: data_dir,
data_local_dir: data_local_dir,
preference_dir: preference_dir,
Expand Down
2 changes: 2 additions & 0 deletions src/win.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,15 @@ pub fn project_dirs_from_path(project_path: PathBuf) -> Option<ProjectDirs> {
let cache_dir = app_data_local.join("cache");
let data_local_dir = app_data_local.join("data");
let config_dir = app_data_roaming.join("config");
let config_local_dir = app_data_local.join("config");
let data_dir = app_data_roaming.join("data");
let preference_dir = config_dir.clone();

let project_dirs = ProjectDirs {
project_path: project_path,
cache_dir: cache_dir,
config_dir: config_dir,
config_local_dir,
data_dir: data_dir,
data_local_dir: data_local_dir,
preference_dir: preference_dir,
Expand Down

0 comments on commit 95ee381

Please sign in to comment.