Skip to content

Commit

Permalink
Add config_local_dir for non-roaming configs
Browse files Browse the repository at this point in the history
  • Loading branch information
soc committed Mar 19, 2023
1 parent c89b761 commit a07a619
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 92 deletions.
41 changes: 21 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,26 +102,27 @@ dirs::executable_dir();
**If you want to compute the location of cache, config or data directories for your own application or project,
use `ProjectDirs` of the [directories](https://github.com/dirs-dev/directories-rs) project instead.**

| Function name | Value on Linux/Redox | Value on Windows | Value on macOS |
| ---------------- | ---------------------------------------------------------------------- | --------------------------------- | ------------------------------------------- |
| `home_dir` | `Some($HOME)` | `Some({FOLDERID_Profile})` | `Some($HOME)` |
| `cache_dir` | `Some($XDG_CACHE_HOME)` or `Some($HOME`/.cache`)` | `Some({FOLDERID_LocalAppData})` | `Some($HOME`/Library/Caches`)` |
| `config_dir` | `Some($XDG_CONFIG_HOME)` or `Some($HOME`/.config`)` | `Some({FOLDERID_RoamingAppData})` | `Some($HOME`/Library/Application Support`)` |
| `data_dir` | `Some($XDG_DATA_HOME)` or `Some($HOME`/.local/share`)` | `Some({FOLDERID_RoamingAppData})` | `Some($HOME`/Library/Application Support`)` |
| `data_local_dir` | `Some($XDG_DATA_HOME)` or `Some($HOME`/.local/share`)` | `Some({FOLDERID_LocalAppData})` | `Some($HOME`/Library/Application Support`)` |
| `executable_dir` | `Some($XDG_BIN_HOME)` or `Some($HOME`/.local/bin`)` | `None` | `None` |
| `preference_dir` | `Some($XDG_CONFIG_HOME)` or `Some($HOME`/.config`)` | `Some({FOLDERID_RoamingAppData})` | `Some($HOME`/Library/Preferences`)` |
| `runtime_dir` | `Some($XDG_RUNTIME_DIR)` or `None` | `None` | `None` |
| `state_dir` | `Some($XDG_STATE_HOME)` or `Some($HOME`/.local/state`)` | `None` | `None` |
| `audio_dir` | `Some(XDG_MUSIC_DIR)` or `None` | `Some({FOLDERID_Music})` | `Some($HOME`/Music/`)` |
| `desktop_dir` | `Some(XDG_DESKTOP_DIR)` or `None` | `Some({FOLDERID_Desktop})` | `Some($HOME`/Desktop/`)` |
| `document_dir` | `Some(XDG_DOCUMENTS_DIR)` or `None` | `Some({FOLDERID_Documents})` | `Some($HOME`/Documents/`)` |
| `download_dir` | `Some(XDG_DOWNLOAD_DIR)` or `None` | `Some({FOLDERID_Downloads})` | `Some($HOME`/Downloads/`)` |
| `font_dir` | `Some($XDG_DATA_HOME`/fonts/`)` or `Some($HOME`/.local/share/fonts/`)` | `None` | `Some($HOME`/Library/Fonts/`)` |
| `picture_dir` | `Some(XDG_PICTURES_DIR)` or `None` | `Some({FOLDERID_Pictures})` | `Some($HOME`/Pictures/`)` |
| `public_dir` | `Some(XDG_PUBLICSHARE_DIR)` or `None` | `Some({FOLDERID_Public})` | `Some($HOME`/Public/`)` |
| `template_dir` | `Some(XDG_TEMPLATES_DIR)` or `None` | `Some({FOLDERID_Templates})` | `None` |
| `video_dir` | `Some(XDG_VIDEOS_DIR)` or `None` | `Some({FOLDERID_Videos})` | `Some($HOME`/Movies/`)` |
| Function name | Value on Linux/Redox | Value on Windows | Value on macOS |
|--------------------| ---------------------------------------------------------------------- |-----------------------------------| ------------------------------------------- |
| `home_dir` | `Some($HOME)` | `Some({FOLDERID_Profile})` | `Some($HOME)` |
| `cache_dir` | `Some($XDG_CACHE_HOME)` or `Some($HOME`/.cache`)` | `Some({FOLDERID_LocalAppData})` | `Some($HOME`/Library/Caches`)` |
| `config_dir` | `Some($XDG_CONFIG_HOME)` or `Some($HOME`/.config`)` | `Some({FOLDERID_RoamingAppData})` | `Some($HOME`/Library/Application Support`)` |
| `config_local_dir` | `Some($XDG_CONFIG_HOME)` or `Some($HOME`/.config`)` | `Some({FOLDERID_LocalAppData})` | `Some($HOME`/Library/Application Support`)` |
| `data_dir` | `Some($XDG_DATA_HOME)` or `Some($HOME`/.local/share`)` | `Some({FOLDERID_RoamingAppData})` | `Some($HOME`/Library/Application Support`)` |
| `data_local_dir` | `Some($XDG_DATA_HOME)` or `Some($HOME`/.local/share`)` | `Some({FOLDERID_LocalAppData})` | `Some($HOME`/Library/Application Support`)` |
| `executable_dir` | `Some($XDG_BIN_HOME)` or `Some($HOME`/.local/bin`)` | `None` | `None` |
| `preference_dir` | `Some($XDG_CONFIG_HOME)` or `Some($HOME`/.config`)` | `Some({FOLDERID_RoamingAppData})` | `Some($HOME`/Library/Preferences`)` |
| `runtime_dir` | `Some($XDG_RUNTIME_DIR)` or `None` | `None` | `None` |
| `state_dir` | `Some($XDG_STATE_HOME)` or `Some($HOME`/.local/state`)` | `None` | `None` |
| `audio_dir` | `Some(XDG_MUSIC_DIR)` or `None` | `Some({FOLDERID_Music})` | `Some($HOME`/Music/`)` |
| `desktop_dir` | `Some(XDG_DESKTOP_DIR)` or `None` | `Some({FOLDERID_Desktop})` | `Some($HOME`/Desktop/`)` |
| `document_dir` | `Some(XDG_DOCUMENTS_DIR)` or `None` | `Some({FOLDERID_Documents})` | `Some($HOME`/Documents/`)` |
| `download_dir` | `Some(XDG_DOWNLOAD_DIR)` or `None` | `Some({FOLDERID_Downloads})` | `Some($HOME`/Downloads/`)` |
| `font_dir` | `Some($XDG_DATA_HOME`/fonts/`)` or `Some($HOME`/.local/share/fonts/`)` | `None` | `Some($HOME`/Library/Fonts/`)` |
| `picture_dir` | `Some(XDG_PICTURES_DIR)` or `None` | `Some({FOLDERID_Pictures})` | `Some($HOME`/Pictures/`)` |
| `public_dir` | `Some(XDG_PUBLICSHARE_DIR)` or `None` | `Some({FOLDERID_Public})` | `Some($HOME`/Public/`)` |
| `template_dir` | `Some(XDG_TEMPLATES_DIR)` or `None` | `Some({FOLDERID_Templates})` | `None` |
| `video_dir` | `Some(XDG_VIDEOS_DIR)` or `None` | `Some({FOLDERID_Videos})` | `Some($HOME`/Movies/`)` |

## Comparison

Expand Down
12 changes: 12 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,18 @@ pub fn cache_dir() -> Option<PathBuf> {
pub fn config_dir() -> Option<PathBuf> {
sys::config_dir()
}
/// Returns the path to the user's local config directory.
///
/// The returned value depends on the operating system and is either a `Some`, containing a value from the following table, or a `None`.
///
/// |Platform | Value | Example |
/// | ------- | ------------------------------------- | ---------------------------------------- |
/// | Linux | `$XDG_CONFIG_HOME` or `$HOME`/.config | /home/alice/.config |
/// | macOS | `$HOME`/Library/Application Support | /Users/Alice/Library/Application Support |
/// | Windows | `{FOLDERID_LocalAppData}` | C:\Users\Alice\AppData\Local |
pub fn config_local_dir() -> Option<PathBuf> {
sys::config_local_dir()
}
/// Returns the path to the user's data directory.
///
/// The returned value depends on the operating system and is either a `Some`, containing a value from the following table, or a `None`.
Expand Down
37 changes: 19 additions & 18 deletions src/lin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,27 @@ extern crate dirs_sys;
use std::env;
use std::path::PathBuf;

pub fn home_dir() -> Option<PathBuf> { dirs_sys::home_dir() }
pub fn home_dir() -> Option<PathBuf> { dirs_sys::home_dir() }

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"))) }
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"))) }
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"))) }
pub fn data_local_dir() -> Option<PathBuf> { data_dir() }
pub fn preference_dir() -> Option<PathBuf> { config_dir() }
pub fn runtime_dir() -> Option<PathBuf> { env::var_os("XDG_RUNTIME_DIR").and_then(dirs_sys::is_absolute_path) }
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"))) }
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"))) }
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"))) }
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"))) }
pub fn config_local_dir() -> Option<PathBuf> { config_dir() }
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"))) }
pub fn data_local_dir() -> Option<PathBuf> { data_dir() }
pub fn preference_dir() -> Option<PathBuf> { config_dir() }
pub fn runtime_dir() -> Option<PathBuf> { env::var_os("XDG_RUNTIME_DIR").and_then(dirs_sys::is_absolute_path) }
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"))) }
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"))) }

pub fn audio_dir() -> Option<PathBuf> { dirs_sys::user_dir("MUSIC") }
pub fn desktop_dir() -> Option<PathBuf> { dirs_sys::user_dir("DESKTOP") }
pub fn document_dir() -> Option<PathBuf> { dirs_sys::user_dir("DOCUMENTS") }
pub fn download_dir() -> Option<PathBuf> { dirs_sys::user_dir("DOWNLOAD") }
pub fn font_dir() -> Option<PathBuf> { data_dir().map(|d| d.join("fonts")) }
pub fn picture_dir() -> Option<PathBuf> { dirs_sys::user_dir("PICTURES") }
pub fn public_dir() -> Option<PathBuf> { dirs_sys::user_dir("PUBLICSHARE") }
pub fn template_dir() -> Option<PathBuf> { dirs_sys::user_dir("TEMPLATES") }
pub fn video_dir() -> Option<PathBuf> { dirs_sys::user_dir("VIDEOS") }
pub fn audio_dir() -> Option<PathBuf> { dirs_sys::user_dir("MUSIC") }
pub fn desktop_dir() -> Option<PathBuf> { dirs_sys::user_dir("DESKTOP") }
pub fn document_dir() -> Option<PathBuf> { dirs_sys::user_dir("DOCUMENTS") }
pub fn download_dir() -> Option<PathBuf> { dirs_sys::user_dir("DOWNLOAD") }
pub fn font_dir() -> Option<PathBuf> { data_dir().map(|d| d.join("fonts")) }
pub fn picture_dir() -> Option<PathBuf> { dirs_sys::user_dir("PICTURES") }
pub fn public_dir() -> Option<PathBuf> { dirs_sys::user_dir("PUBLICSHARE") }
pub fn template_dir() -> Option<PathBuf> { dirs_sys::user_dir("TEMPLATES") }
pub fn video_dir() -> Option<PathBuf> { dirs_sys::user_dir("VIDEOS") }

#[cfg(test)]
mod tests {
Expand Down
39 changes: 21 additions & 18 deletions src/mac.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,26 @@ extern crate dirs_sys;

use std::path::PathBuf;

pub fn home_dir() -> Option<PathBuf> { dirs_sys::home_dir() }
pub fn home_dir() -> Option<PathBuf> { dirs_sys::home_dir() }

pub fn cache_dir() -> Option<PathBuf> { home_dir().map(|h| h.join("Library/Caches")) }
pub fn config_dir() -> Option<PathBuf> { home_dir().map(|h| h.join("Library/Application Support")) }
pub fn data_dir() -> Option<PathBuf> { home_dir().map(|h| h.join("Library/Application Support")) }
pub fn data_local_dir() -> Option<PathBuf> { data_dir() }
pub fn preference_dir() -> Option<PathBuf> { home_dir().map(|h| h.join("Library/Preferences")) }
pub fn executable_dir() -> Option<PathBuf> { None }
pub fn runtime_dir() -> Option<PathBuf> { None }
pub fn state_dir() -> Option<PathBuf> { None }
fn app_support_dir() -> Option<PathBuf> { home_dir().map(|h| h.join("Library/Application Support")) }

pub fn audio_dir() -> Option<PathBuf> { home_dir().map(|h| h.join("Music")) }
pub fn desktop_dir() -> Option<PathBuf> { home_dir().map(|h| h.join("Desktop")) }
pub fn document_dir() -> Option<PathBuf> { home_dir().map(|h| h.join("Documents")) }
pub fn download_dir() -> Option<PathBuf> { home_dir().map(|h| h.join("Downloads")) }
pub fn font_dir() -> Option<PathBuf> { home_dir().map(|h| h.join("Library/Fonts")) }
pub fn picture_dir() -> Option<PathBuf> { home_dir().map(|h| h.join("Pictures")) }
pub fn public_dir() -> Option<PathBuf> { home_dir().map(|h| h.join("Public")) }
pub fn template_dir() -> Option<PathBuf> { None }
pub fn video_dir() -> Option<PathBuf> { home_dir().map(|h| h.join("Movies")) }
pub fn cache_dir() -> Option<PathBuf> { home_dir().map(|h| h.join("Library/Caches")) }
pub fn config_dir() -> Option<PathBuf> { app_support_dir() }
pub fn config_local_dir() -> Option<PathBuf> { app_support_dir() }
pub fn data_dir() -> Option<PathBuf> { app_support_dir() }
pub fn data_local_dir() -> Option<PathBuf> { app_support_dir() }
pub fn preference_dir() -> Option<PathBuf> { home_dir().map(|h| h.join("Library/Preferences")) }
pub fn executable_dir() -> Option<PathBuf> { None }
pub fn runtime_dir() -> Option<PathBuf> { None }
pub fn state_dir() -> Option<PathBuf> { None }

pub fn audio_dir() -> Option<PathBuf> { home_dir().map(|h| h.join("Music")) }
pub fn desktop_dir() -> Option<PathBuf> { home_dir().map(|h| h.join("Desktop")) }
pub fn document_dir() -> Option<PathBuf> { home_dir().map(|h| h.join("Documents")) }
pub fn download_dir() -> Option<PathBuf> { home_dir().map(|h| h.join("Downloads")) }
pub fn font_dir() -> Option<PathBuf> { home_dir().map(|h| h.join("Library/Fonts")) }
pub fn picture_dir() -> Option<PathBuf> { home_dir().map(|h| h.join("Pictures")) }
pub fn public_dir() -> Option<PathBuf> { home_dir().map(|h| h.join("Public")) }
pub fn template_dir() -> Option<PathBuf> { None }
pub fn video_dir() -> Option<PathBuf> { home_dir().map(|h| h.join("Movies")) }
37 changes: 19 additions & 18 deletions src/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,24 @@

use std::path::PathBuf;

pub fn home_dir() -> Option<PathBuf> { None }
pub fn home_dir() -> Option<PathBuf> { None }

pub fn cache_dir() -> Option<PathBuf> { None }
pub fn config_dir() -> Option<PathBuf> { None }
pub fn data_dir() -> Option<PathBuf> { None }
pub fn data_local_dir() -> Option<PathBuf> { None }
pub fn preference_dir() -> Option<PathBuf> { None }
pub fn runtime_dir() -> Option<PathBuf> { None }
pub fn executable_dir() -> Option<PathBuf> { None }
pub fn state_dir() -> Option<PathBuf> { None }
pub fn cache_dir() -> Option<PathBuf> { None }
pub fn config_dir() -> Option<PathBuf> { None }
pub fn config_local_dir() -> Option<PathBuf> { None }
pub fn data_dir() -> Option<PathBuf> { None }
pub fn data_local_dir() -> Option<PathBuf> { None }
pub fn preference_dir() -> Option<PathBuf> { None }
pub fn runtime_dir() -> Option<PathBuf> { None }
pub fn executable_dir() -> Option<PathBuf> { None }
pub fn state_dir() -> Option<PathBuf> { None }

pub fn audio_dir() -> Option<PathBuf> { None }
pub fn desktop_dir() -> Option<PathBuf> { None }
pub fn document_dir() -> Option<PathBuf> { None }
pub fn download_dir() -> Option<PathBuf> { None }
pub fn font_dir() -> Option<PathBuf> { None }
pub fn picture_dir() -> Option<PathBuf> { None }
pub fn public_dir() -> Option<PathBuf> { None }
pub fn template_dir() -> Option<PathBuf> { None }
pub fn video_dir() -> Option<PathBuf> { None }
pub fn audio_dir() -> Option<PathBuf> { None }
pub fn desktop_dir() -> Option<PathBuf> { None }
pub fn document_dir() -> Option<PathBuf> { None }
pub fn download_dir() -> Option<PathBuf> { None }
pub fn font_dir() -> Option<PathBuf> { None }
pub fn picture_dir() -> Option<PathBuf> { None }
pub fn public_dir() -> Option<PathBuf> { None }
pub fn template_dir() -> Option<PathBuf> { None }
pub fn video_dir() -> Option<PathBuf> { None }
Loading

0 comments on commit a07a619

Please sign in to comment.