Skip to main content

openlogi_core/
paths.rs

1//! Per-OS application directories, following the XDG Base Directory spec on
2//! **every** platform — including macOS, so configuration lives at the
3//! familiar `~/.config/openlogi/` rather than macOS's
4//! `~/Library/Application Support/`.
5//!
6//! | kind   | env override        | default                       |
7//! |--------|---------------------|-------------------------------|
8//! | config | `$XDG_CONFIG_HOME`  | `~/.config/openlogi`          |
9//! | data   | `$XDG_DATA_HOME`    | `~/.local/share/openlogi`     |
10//!
11//! On Windows `$HOME` falls back to `%USERPROFILE%`, so paths resolve to
12//! `%USERPROFILE%\.config\openlogi` etc.
13//!
14//! **Decision (#347):** the Windows location is final, not best-effort.
15//! XDG-on-every-platform is this module's deliberate design — macOS also
16//! skips its native `~/Library/Application Support` — and Windows follows
17//! the same rule rather than `%APPDATA%`. Recorded before the agent first
18//! shipped in Windows artifacts, because moving it afterwards would strand
19//! every existing user's `config.toml` and the agent's first-run state.
20
21use std::path::PathBuf;
22
23use etcetera::{BaseStrategy, base_strategy::Xdg};
24use thiserror::Error;
25
26/// Subdirectory created under each XDG base directory.
27const APP_DIR: &str = "openlogi";
28
29#[derive(Debug, Error)]
30pub enum PathsError {
31    #[error("could not resolve a home directory for the current user")]
32    HomeNotFound,
33}
34
35fn xdg() -> Result<Xdg, PathsError> {
36    Xdg::new().map_err(|_| PathsError::HomeNotFound)
37}
38
39/// The raw XDG config home directory (without the `openlogi` subdirectory).
40///
41/// Honours an absolute `$XDG_CONFIG_HOME`; falls back to `~/.config`.
42/// Useful when placing files that belong to other apps under the same base
43/// (e.g. systemd user units at `$XDG_CONFIG_HOME/systemd/user/`).
44pub fn xdg_config_home() -> Result<PathBuf, PathsError> {
45    Ok(xdg()?.config_dir())
46}
47
48/// Directory holding the user's `config.toml`.
49///
50/// `$XDG_CONFIG_HOME/openlogi`, default `~/.config/openlogi`.
51pub fn config_dir() -> Result<PathBuf, PathsError> {
52    Ok(xdg_config_home()?.join(APP_DIR))
53}
54
55/// Full path to the user config file.
56pub fn config_path() -> Result<PathBuf, PathsError> {
57    Ok(config_dir()?.join("config.toml"))
58}
59
60/// Directory for downloaded application data; the device-render asset cache
61/// lives under `data_dir()/assets`.
62///
63/// `$XDG_DATA_HOME/openlogi`, default `~/.local/share/openlogi`.
64pub fn data_dir() -> Result<PathBuf, PathsError> {
65    Ok(xdg()?.data_dir().join(APP_DIR))
66}
67
68/// Directory for runtime sockets — the background agent's IPC endpoint.
69pub fn runtime_dir() -> Result<PathBuf, PathsError> {
70    let xdg = xdg()?;
71    Ok(xdg
72        .runtime_dir()
73        .map_or_else(|| xdg.config_dir().join(APP_DIR), |dir| dir.join(APP_DIR)))
74}
75
76/// Path to the background agent's Unix-domain IPC socket: the GUI connects here
77/// to reach the agent that owns device I/O.
78pub fn agent_socket_path() -> Result<PathBuf, PathsError> {
79    Ok(runtime_dir()?.join("agent.sock"))
80}
81
82#[cfg(all(test, unix))]
83#[allow(clippy::expect_used, reason = "expect/unwrap are idiomatic in tests")]
84mod tests {
85    use super::*;
86
87    #[test]
88    fn config_dir_keeps_openlogi_under_xdg_config_home() {
89        assert!(config_dir().expect("config dir").ends_with("openlogi"));
90    }
91
92    #[test]
93    fn data_dir_keeps_openlogi_under_xdg_data_home() {
94        assert!(data_dir().expect("data dir").ends_with("openlogi"));
95    }
96
97    #[test]
98    fn runtime_dir_keeps_openlogi_suffix() {
99        assert!(runtime_dir().expect("runtime dir").ends_with("openlogi"));
100    }
101}