1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
//! Cross-platform configuration path discovery following XDG and platform conventions.
//!
//! This crate provides a platform-agnostic way to discover configuration
//! directories following the conventions of each operating system:
//!
//! - **Unix/Linux**: XDG Base Directory Specification
//! - **macOS CLI**: XDG (same as Unix)
//! - **macOS GUI**: Application Support directories
//! - **Windows**: Known Folder IDs (`AppData`, `ProgramData`)
//!
//! # Quick Start
//!
//! ```
//! use cfgmatic_paths::PathsBuilder;
//!
//! // Create a path finder for your application
//! let finder = PathsBuilder::new("myapp").build();
//!
//! // Get user config directories
//! let user_dirs = finder.user_dirs();
//! println!("User config dirs: {:?}", user_dirs);
//!
//! // Ensure the primary user config directory exists
//! if let Ok(config_dir) = finder.ensure_user_config_dir() {
//! println!("Config dir: {}", config_dir.display());
//! }
//! ```
//!
//! # Configuration Tiers
//!
//! Configuration directories are organized into three tiers:
//!
//! 1. **User** (`ConfigTier::User`): User-specific configs in home directory.
//! Highest priority, typically writable.
//!
//! 2. **Local** (`ConfigTier::Local`): Machine-specific configs.
//! Medium priority, may be writable.
//!
//! 3. **System** (`ConfigTier::System`): System-wide configs.
//! Lowest priority, typically read-only.
//!
//! # Platform Details
//!
//! ## Unix/Linux (XDG)
//!
//! Uses the [XDG Base Directory Specification](https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html):
//!
//! - User: `$XDG_CONFIG_HOME/<app>/` (default: `~/.config/<app>/`)
//! - Legacy: `~/.<app>rc` (optional, enabled by default)
//! - System: `$XDG_CONFIG_DIRS/<app>/` (default: `/etc/xdg/<app>/`)
//!
//! ## macOS
//!
//! ### CLI Applications
//! Uses XDG (same as Unix/Linux).
//!
//! ### GUI Applications (with `macos-gui` feature)
//! Uses macOS conventions:
//!
//! - User: `~/Library/Application Support/<bundle-id>/`
//! - System: `/Library/Application Support/<bundle-id>/`
//!
//! ## Windows
//!
//! Uses Windows Known Folder IDs:
//!
//! - User Roaming: `%APPDATA%\<Company>\<App>\`
//! - User Local: `%LOCALAPPDATA%\<Company>\<App>\`
//! - System: `%PROGRAMDATA%\<Company>\<App>\`
//!
//! # Testing
//!
//! The crate provides abstractions for testing:
//!
//! - [`Env`]: Mock environment variables
//! - [`Fs`]: Mock filesystem operations
//!
//! These allow testing without modifying the actual environment or filesystem.
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
/// Platform-specific directory finder implementations.
/// Find the first existing configuration directory.
///
/// Searches in order: User → Local → System.
/// Returns the first directory that exists, or `None`.
///
/// # Example
///
/// ```
/// use cfgmatic_paths::find_existing_dir;
///
/// if let Some(dir) = find_existing_dir("myapp") {
/// println!("Found config dir: {}", dir.display());
/// }
/// ```
/// Find or create the user configuration directory.
///
/// Returns the primary user config directory, creating it if it doesn't exist.
///
/// # Errors
///
/// Returns an error if the directory cannot be created.
///
/// # Example
///
/// ```
/// use cfgmatic_paths::ensure_config_dir;
///
/// match ensure_config_dir("myapp") {
/// Ok(dir) => println!("Config dir: {}", dir.display()),
/// Err(e) => eprintln!("Failed to create config dir: {}", e),
/// }
/// ```
/// Get all configuration directories with their metadata.
///
/// Returns a list of all known configuration directories with their
/// tier and existence status.
///
/// # Example
///
/// ```
/// use cfgmatic_paths::get_all_dirs;
///
/// for info in get_all_dirs("myapp") {
/// println!("{:?}: {} (exists: {})",
/// info.tier, info.path.display(), info.exists);
/// }
/// ```
/// Discover configuration with full diagnostics.
///
/// This is a convenience function that creates a finder and returns
/// comprehensive information about configuration locations.
///
/// # Example
///
/// ```
/// use cfgmatic_paths::discover_config;
///
/// let discovery = discover_config("myapp");
/// println!("Preferred: {}", discovery.preferred_path.display());
///
/// for candidate in discovery.candidates {
/// println!(" - {:?}: {} ({:?})",
/// candidate.tier,
/// candidate.path.display(),
/// candidate.status
/// );
/// }
/// ```
/// Get the preferred configuration path (without checking existence).
///
/// Returns the first user config directory path, regardless of whether
/// it exists. This is useful for determining where to create a new
/// configuration file.
///
/// # Example
///
/// ```
/// use cfgmatic_paths::config_path;
///
/// let path = config_path("myapp");
/// println!("Config would be at: {}", path.display());
/// ```
/// Get the preferred configuration file path.
///
/// Returns the full path to a config file in the preferred location,
/// without checking existence.
///
/// # Example
///
/// ```
/// use cfgmatic_paths::config_file_path;
///
/// let path = config_file_path("myapp", "config.toml");
/// println!("Config file would be at: {}", path.display());
/// ```