Skip to main content

confex_core/
paths.rs

1//! Cross-platform application directory lookup.
2//!
3//! Two modes:
4//!
5//! - **Default** (no features): confex derives XDG / `%APPDATA%` / `~/Library`
6//!   paths itself. Desktop only (Linux, macOS, Windows).
7//! - **`istmo` feature**: delegates to [`istmo_core::path`], which also covers
8//!   Android (`Context.filesDir`) and iOS (`NSFileManager`). The per-app
9//!   subdirectory is derived from the build-time `ISTMO_APP_BUNDLE_ID` env
10//!   var (baked by `istmo-build::emit_app`), so the `app_name` argument of
11//!   [`app_config_dir`] / [`app_data_dir`] is **ignored** in this mode —
12//!   istmo already resolved the app identity.
13
14use std::path::PathBuf;
15
16/// Returns the base directory for user configuration files.
17///
18/// - **With `istmo` feature**: [`istmo_core::path::config_dir`] (already
19///   includes the app's bundle id subdir).
20/// - Linux/BSD: `$XDG_CONFIG_HOME` or `~/.config`
21/// - macOS: `~/Library/Application Support`
22/// - Windows: `%APPDATA%`
23#[cfg(feature = "istmo")]
24#[must_use]
25pub fn config_base_dir() -> Option<PathBuf> {
26    Some(istmo_core::path::config_dir().to_path_buf())
27}
28
29#[cfg(all(not(feature = "istmo"), target_os = "windows"))]
30#[must_use]
31pub fn config_base_dir() -> Option<PathBuf> {
32    std::env::var_os("APPDATA").map(PathBuf::from)
33}
34
35#[cfg(all(not(feature = "istmo"), target_os = "macos"))]
36#[must_use]
37pub fn config_base_dir() -> Option<PathBuf> {
38    home_dir().map(|h| h.join("Library").join("Application Support"))
39}
40
41#[cfg(all(
42    not(feature = "istmo"),
43    not(any(target_os = "windows", target_os = "macos"))
44))]
45#[must_use]
46pub fn config_base_dir() -> Option<PathBuf> {
47    std::env::var_os("XDG_CONFIG_HOME")
48        .map(PathBuf::from)
49        .or_else(|| home_dir().map(|h| h.join(".config")))
50}
51
52/// Returns the base directory for application data files.
53///
54/// - **With `istmo` feature**: [`istmo_core::path::data_dir`] (already
55///   includes the app's bundle id subdir).
56/// - Linux/BSD: `$XDG_DATA_HOME` or `~/.local/share`
57/// - macOS: `~/Library/Application Support`
58/// - Windows: `%LOCALAPPDATA%`
59#[cfg(feature = "istmo")]
60#[must_use]
61pub fn data_base_dir() -> Option<PathBuf> {
62    Some(istmo_core::path::data_dir().to_path_buf())
63}
64
65#[cfg(all(not(feature = "istmo"), target_os = "windows"))]
66#[must_use]
67pub fn data_base_dir() -> Option<PathBuf> {
68    std::env::var_os("LOCALAPPDATA").map(PathBuf::from)
69}
70
71#[cfg(all(not(feature = "istmo"), target_os = "macos"))]
72#[must_use]
73pub fn data_base_dir() -> Option<PathBuf> {
74    home_dir().map(|h| h.join("Library").join("Application Support"))
75}
76
77#[cfg(all(
78    not(feature = "istmo"),
79    not(any(target_os = "windows", target_os = "macos"))
80))]
81#[must_use]
82pub fn data_base_dir() -> Option<PathBuf> {
83    std::env::var_os("XDG_DATA_HOME")
84        .map(PathBuf::from)
85        .or_else(|| home_dir().map(|h| h.join(".local").join("share")))
86}
87
88/// Returns the config directory for a specific application.
89///
90/// With the `istmo` feature on, `_app_name` is ignored — istmo resolved the
91/// app identity at build time via `ISTMO_APP_BUNDLE_ID`.
92#[cfg(feature = "istmo")]
93#[must_use]
94pub fn app_config_dir(_app_name: &str) -> Option<PathBuf> {
95    Some(istmo_core::path::config_dir().to_path_buf())
96}
97
98#[cfg(not(feature = "istmo"))]
99#[must_use]
100pub fn app_config_dir(app_name: &str) -> Option<PathBuf> {
101    config_base_dir().map(|d| d.join(app_name))
102}
103
104/// Returns the data directory for a specific application.
105///
106/// With the `istmo` feature on, `_app_name` is ignored — istmo resolved the
107/// app identity at build time via `ISTMO_APP_BUNDLE_ID`.
108#[cfg(feature = "istmo")]
109#[must_use]
110pub fn app_data_dir(_app_name: &str) -> Option<PathBuf> {
111    Some(istmo_core::path::data_dir().to_path_buf())
112}
113
114#[cfg(not(feature = "istmo"))]
115#[must_use]
116pub fn app_data_dir(app_name: &str) -> Option<PathBuf> {
117    data_base_dir().map(|d| d.join(app_name))
118}
119
120/// Returns the OS-managed cache directory for a specific application.
121///
122/// Only available with the `istmo` feature (desktop + mobile unified).
123#[cfg(feature = "istmo")]
124#[must_use]
125pub fn app_cache_dir(_app_name: &str) -> Option<PathBuf> {
126    Some(istmo_core::path::cache_dir().to_path_buf())
127}
128
129/// Returns the state directory for a specific application.
130///
131/// Only available with the `istmo` feature (desktop + mobile unified).
132#[cfg(feature = "istmo")]
133#[must_use]
134pub fn app_state_dir(_app_name: &str) -> Option<PathBuf> {
135    Some(istmo_core::path::state_dir().to_path_buf())
136}
137
138#[cfg(not(feature = "istmo"))]
139fn home_dir() -> Option<PathBuf> {
140    std::env::var_os("HOME").map(PathBuf::from)
141}