Skip to content
This repository was archived by the owner on Feb 18, 2025. It is now read-only.

Commit 8f14dfc

Browse files
committed
Add support for XDG_STATE_HOME
1 parent 840acfc commit 8f14dfc

File tree

6 files changed

+37
-2
lines changed

6 files changed

+37
-2
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ use `ProjectDirs` of the [directories](https://github.com/dirs-dev/directories-r
114114
| `executable_dir` | `Some($XDG_BIN_HOME`/../bin`)` or `Some($XDG_DATA_HOME`/../bin`)` or `Some($HOME`/.local/bin`)` | `None` | `None` |
115115
| `preference_dir` | `Some($XDG_CONFIG_HOME)` or `Some($HOME`/.config`)` | `Some({FOLDERID_RoamingAppData})` | `Some($HOME`/Library/Preferences`)` |
116116
| `runtime_dir` | `Some($XDG_RUNTIME_DIR)` or `None` | `None` | `None` |
117+
| `state_dir` | `Some($XDG_STATE_HOME)` or `Some($HOME`/.local/state`)` | `None` | `None` |
117118
| `audio_dir` | `Some(XDG_MUSIC_DIR)` or `None` | `Some({FOLDERID_Music})` | `Some($HOME`/Music/`)` |
118119
| `desktop_dir` | `Some(XDG_DESKTOP_DIR)` or `None` | `Some({FOLDERID_Desktop})` | `Some($HOME`/Desktop/`)` |
119120
| `document_dir` | `Some(XDG_DOCUMENTS_DIR)` or `None` | `Some({FOLDERID_Documents})` | `Some($HOME`/Documents/`)` |
@@ -168,6 +169,9 @@ cargo build --target=x86_64-unknown-redox
168169

169170
## Changelog
170171

172+
### 4
173+
- Add support for `XDG_STATE_HOME`.
174+
171175
### 3
172176

173177
- **BREAKING CHANGE** The behavior of `config_dir` on macOS has been adjusted

src/lib.rs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! The _dirs_ crate is
22
//!
3-
//! - a tiny library with a minimal API (16 functions)
3+
//! - a tiny library with a minimal API (18 public functions)
44
//! - that provides the platform-specific, user-accessible locations
55
//! - for finding and storing configuration, cache and other data
66
//! - on Linux, Redox, Windows (≥ Vista) and macOS.
@@ -158,6 +158,22 @@ pub fn preference_dir() -> Option<PathBuf> {
158158
pub fn runtime_dir() -> Option<PathBuf> {
159159
sys::runtime_dir()
160160
}
161+
/// Returns the path to the user's state directory.
162+
///
163+
/// The state directory contains data that should be retained between sessions (unlike the runtime
164+
/// directory), but may not be important/portable enough to be synchronized across machines (unlike
165+
/// the config/preferences/data directories).
166+
///
167+
/// The returned value depends on the operating system and is either a `Some`, containing a value from the following table, or a `None`.
168+
///
169+
/// |Platform | Value | Example |
170+
/// | ------- | ----------------------------------------- | ------------------------ |
171+
/// | Linux | `$XDG_STATE_HOME` or `$HOME`/.local/state | /home/alice/.local/state |
172+
/// | macOS | – | – |
173+
/// | Windows | – | – |
174+
pub fn state_dir() -> Option<PathBuf> {
175+
sys::state_dir()
176+
}
161177

162178
/// Returns the path to the user's audio directory.
163179
///
@@ -274,15 +290,18 @@ mod tests {
274290
#[test]
275291
fn test_dirs() {
276292
println!("home_dir: {:?}", ::home_dir());
293+
println!();
277294
println!("cache_dir: {:?}", ::cache_dir());
278295
println!("config_dir: {:?}", ::config_dir());
279296
println!("data_dir: {:?}", ::data_dir());
280297
println!("data_local_dir: {:?}", ::data_local_dir());
281298
println!("executable_dir: {:?}", ::executable_dir());
282299
println!("preference_dir: {:?}", ::preference_dir());
283300
println!("runtime_dir: {:?}", ::runtime_dir());
301+
println!("state_dir: {:?}", ::state_dir());
302+
println!();
284303
println!("audio_dir: {:?}", ::audio_dir());
285-
println!("home_dir: {:?}", ::desktop_dir());
304+
println!("desktop_dir: {:?}", ::desktop_dir());
286305
println!("cache_dir: {:?}", ::document_dir());
287306
println!("config_dir: {:?}", ::download_dir());
288307
println!("font_dir: {:?}", ::font_dir());

src/lin.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,20 @@ use std::env;
44
use std::path::PathBuf;
55

66
pub fn home_dir() -> Option<PathBuf> { dirs_sys::home_dir() }
7+
78
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"))) }
89
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"))) }
910
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"))) }
1011
pub fn data_local_dir() -> Option<PathBuf> { data_dir() }
1112
pub fn preference_dir() -> Option<PathBuf> { config_dir() }
1213
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"))) }
1315
pub fn executable_dir() -> Option<PathBuf> {
1416
env::var_os("XDG_BIN_HOME").and_then(dirs_sys::is_absolute_path).or_else(|| {
1517
data_dir().map(|mut e| { e.pop(); e.push("bin"); e })
1618
})
1719
}
20+
1821
pub fn audio_dir() -> Option<PathBuf> { dirs_sys::user_dir("MUSIC") }
1922
pub fn desktop_dir() -> Option<PathBuf> { dirs_sys::user_dir("DESKTOP") }
2023
pub fn document_dir() -> Option<PathBuf> { dirs_sys::user_dir("DOCUMENTS") }

src/mac.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,16 @@ extern crate dirs_sys;
33
use std::path::PathBuf;
44

55
pub fn home_dir() -> Option<PathBuf> { dirs_sys::home_dir() }
6+
67
pub fn cache_dir() -> Option<PathBuf> { home_dir().map(|h| h.join("Library/Caches")) }
78
pub fn config_dir() -> Option<PathBuf> { home_dir().map(|h| h.join("Library/Application Support")) }
89
pub fn data_dir() -> Option<PathBuf> { home_dir().map(|h| h.join("Library/Application Support")) }
910
pub fn data_local_dir() -> Option<PathBuf> { data_dir() }
1011
pub fn preference_dir() -> Option<PathBuf> { home_dir().map(|h| h.join("Library/Preferences")) }
1112
pub fn executable_dir() -> Option<PathBuf> { None }
1213
pub fn runtime_dir() -> Option<PathBuf> { None }
14+
pub fn state_dir() -> Option<PathBuf> { None }
15+
1316
pub fn audio_dir() -> Option<PathBuf> { home_dir().map(|h| h.join("Music")) }
1417
pub fn desktop_dir() -> Option<PathBuf> { home_dir().map(|h| h.join("Desktop")) }
1518
pub fn document_dir() -> Option<PathBuf> { home_dir().map(|h| h.join("Documents")) }

src/wasm.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,16 @@
33
use std::path::PathBuf;
44

55
pub fn home_dir() -> Option<PathBuf> { None }
6+
67
pub fn cache_dir() -> Option<PathBuf> { None }
78
pub fn config_dir() -> Option<PathBuf> { None }
89
pub fn data_dir() -> Option<PathBuf> { None }
910
pub fn data_local_dir() -> Option<PathBuf> { None }
1011
pub fn preference_dir() -> Option<PathBuf> { None }
1112
pub fn runtime_dir() -> Option<PathBuf> { None }
1213
pub fn executable_dir() -> Option<PathBuf> { None }
14+
pub fn state_dir() -> Option<PathBuf> { None }
15+
1316
pub fn audio_dir() -> Option<PathBuf> { None }
1417
pub fn desktop_dir() -> Option<PathBuf> { None }
1518
pub fn document_dir() -> Option<PathBuf> { None }

src/win.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,16 @@ extern crate dirs_sys;
33
use std::path::PathBuf;
44

55
pub fn home_dir() -> Option<PathBuf> { dirs_sys::known_folder_profile() }
6+
67
pub fn data_dir() -> Option<PathBuf> { dirs_sys::known_folder_roaming_app_data() }
78
pub fn data_local_dir() -> Option<PathBuf> { dirs_sys::known_folder_local_app_data() }
89
pub fn cache_dir() -> Option<PathBuf> { data_local_dir() }
910
pub fn config_dir() -> Option<PathBuf> { data_dir() }
1011
pub fn executable_dir() -> Option<PathBuf> { None }
1112
pub fn preference_dir() -> Option<PathBuf> { data_dir() }
1213
pub fn runtime_dir() -> Option<PathBuf> { None }
14+
pub fn state_dir() -> Option<PathBuf> { None }
15+
1316
pub fn audio_dir() -> Option<PathBuf> { dirs_sys::known_folder_music() }
1417
pub fn desktop_dir() -> Option<PathBuf> { dirs_sys::known_folder_desktop() }
1518
pub fn document_dir() -> Option<PathBuf> { dirs_sys::known_folder_documents() }

0 commit comments

Comments
 (0)