1use std::path::{Path, PathBuf};
24
25pub(crate) mod files {
27 pub(crate) const CLUSTER: &str = "cluster.toml";
29 pub(crate) const NOTEBOOKS: &str = "notebooks.toml";
31 pub(crate) const AI: &str = "ai.toml";
33 pub(crate) const TEXT: &str = "text.toml";
35}
36
37pub(crate) fn candidate_dirs(explicit: Option<&Path>) -> Vec<PathBuf> {
39 if let Some(dir) = explicit {
40 return vec![dir.to_path_buf()];
41 }
42 user_config_dir_from(
46 platform(),
47 &std::env::var("XDG_CONFIG_HOME").ok(),
48 &std::env::var("HOME").ok(),
49 &std::env::var("APPDATA").ok(),
50 )
51 .into_iter()
52 .chain(system_config_dir())
53 .collect()
54}
55
56pub(crate) fn find(explicit: Option<&Path>, file_name: &str) -> Option<PathBuf> {
58 candidate_dirs(explicit)
59 .into_iter()
60 .map(|dir| dir.join(file_name))
61 .find(|path| path.is_file())
62}
63
64#[derive(Debug, Clone, Copy, PartialEq, Eq)]
65enum Platform {
66 Linux,
67 MacOs,
68 Windows,
69}
70
71fn platform() -> Platform {
72 if cfg!(target_os = "macos") {
73 Platform::MacOs
74 } else if cfg!(windows) {
75 Platform::Windows
76 } else {
77 Platform::Linux
78 }
79}
80
81fn system_config_dir() -> Option<PathBuf> {
82 system_config_dir_from(platform(), &std::env::var("PROGRAMDATA").ok())
83}
84
85fn user_config_dir_from(
87 platform: Platform,
88 xdg_config_home: &Option<String>,
89 home: &Option<String>,
90 appdata: &Option<String>,
91) -> Option<PathBuf> {
92 let dir = match platform {
93 Platform::Linux => xdg_config_home
94 .clone()
95 .map(PathBuf::from)
96 .or_else(|| home.clone().map(|home| Path::new(&home).join(".config")))?,
97 Platform::MacOs => PathBuf::from(home.clone()?).join("Library/Application Support"),
99 Platform::Windows => PathBuf::from(appdata.clone()?),
101 };
102 Some(dir.join("waratah"))
103}
104
105fn system_config_dir_from(platform: Platform, program_data: &Option<String>) -> Option<PathBuf> {
107 let dir = match platform {
108 Platform::Linux => PathBuf::from("/etc"),
111 Platform::MacOs => PathBuf::from("/Library/Application Support"),
112 Platform::Windows => PathBuf::from(
113 program_data
114 .clone()
115 .unwrap_or_else(|| "C:\\ProgramData".to_owned()),
116 ),
117 };
118 Some(dir.join("waratah"))
119}
120
121#[cfg(test)]
122mod tests {
123 use super::*;
124
125 fn some(value: &str) -> Option<String> {
126 Some(value.to_owned())
127 }
128
129 #[test]
130 fn explicit_config_dir_is_the_only_candidate() {
131 let dirs = candidate_dirs(Some(Path::new("/opt/waratah-conf")));
132 assert_eq!(dirs, vec![PathBuf::from("/opt/waratah-conf")]);
133 }
134
135 #[test]
136 fn linux_user_dir_honors_xdg_then_home() {
137 assert_eq!(
138 user_config_dir_from(Platform::Linux, &some("/xdg"), &some("/home/u"), &None),
139 Some(PathBuf::from("/xdg/waratah"))
140 );
141 assert_eq!(
142 user_config_dir_from(Platform::Linux, &None, &some("/home/u"), &None),
143 Some(PathBuf::from("/home/u/.config/waratah"))
144 );
145 assert_eq!(
147 user_config_dir_from(Platform::Linux, &None, &None, &None),
148 None
149 );
150 }
151
152 fn assert_dir(platform_dir: Option<PathBuf>, expected_prefix: &str) {
157 let rendered = platform_dir
158 .map(|dir| dir.to_string_lossy().replace('\\', "/"))
159 .unwrap_or_default();
160 assert_eq!(rendered, format!("{expected_prefix}/waratah"));
161 }
162
163 #[test]
164 fn macos_and_windows_user_dirs_follow_platform_conventions() {
165 assert_dir(
166 user_config_dir_from(Platform::MacOs, &None, &some("/Users/u"), &None),
167 "/Users/u/Library/Application Support",
168 );
169 assert_dir(
170 user_config_dir_from(
171 Platform::Windows,
172 &None,
173 &None,
174 &some("C:\\Users\\u\\AppData\\Roaming"),
175 ),
176 "C:/Users/u/AppData/Roaming",
177 );
178 }
179
180 #[test]
181 fn system_dirs_are_machine_wide() {
182 assert_eq!(
183 system_config_dir_from(Platform::Linux, &None),
184 Some(PathBuf::from("/etc/waratah"))
185 );
186 assert_dir(
187 system_config_dir_from(Platform::MacOs, &None),
188 "/Library/Application Support",
189 );
190 assert_dir(
192 system_config_dir_from(Platform::Windows, &None),
193 "C:/ProgramData",
194 );
195 assert_dir(
196 system_config_dir_from(Platform::Windows, &some("D:\\ProgramData")),
197 "D:/ProgramData",
198 );
199 }
200
201 #[test]
202 fn find_prefers_user_dir_over_system_dir() {
203 let user = tempfile::tempdir().unwrap();
204 let system = tempfile::tempdir().unwrap();
205 std::fs::write(user.path().join("notebooks.toml"), "enabled = true").unwrap();
206
207 let found = [user.path(), system.path()]
209 .into_iter()
210 .map(|dir| dir.join(files::NOTEBOOKS))
211 .find(|path| path.is_file())
212 .unwrap();
213 assert_eq!(found, user.path().join("notebooks.toml"));
214
215 std::fs::remove_file(user.path().join("notebooks.toml")).unwrap();
217 std::fs::write(system.path().join("notebooks.toml"), "enabled = true").unwrap();
218 let found = [user.path(), system.path()]
219 .into_iter()
220 .map(|dir| dir.join(files::NOTEBOOKS))
221 .find(|path| path.is_file())
222 .unwrap();
223 assert_eq!(found, system.path().join("notebooks.toml"));
224 }
225
226 #[test]
227 fn find_returns_none_when_no_candidate_exists() {
228 let nowhere = tempfile::tempdir().unwrap();
229 assert_eq!(find(Some(nowhere.path()), files::CLUSTER), None);
230 }
231}