lean_ctx/core/
data_dir.rs1use std::path::PathBuf;
2
3pub fn lean_ctx_data_dir() -> Result<PathBuf, String> {
10 if let Ok(dir) = std::env::var("LEAN_CTX_DATA_DIR") {
11 let trimmed = dir.trim();
12 if !trimmed.is_empty() {
13 return Ok(PathBuf::from(trimmed));
14 }
15 }
16
17 let home = dirs::home_dir().ok_or_else(|| "Cannot determine home directory".to_string())?;
18
19 let legacy = home.join(".lean-ctx");
20 if legacy.exists() {
21 return Ok(legacy);
22 }
23
24 let xdg_config = std::env::var("XDG_CONFIG_HOME")
25 .ok()
26 .filter(|s| !s.trim().is_empty())
27 .map(PathBuf::from)
28 .unwrap_or_else(|| home.join(".config"));
29
30 Ok(xdg_config.join("lean-ctx"))
31}
32
33pub fn test_env_lock() -> std::sync::MutexGuard<'static, ()> {
34 use std::sync::{Mutex, OnceLock};
35 static LOCK: OnceLock<Mutex<()>> = OnceLock::new();
36 let mutex = LOCK.get_or_init(|| Mutex::new(()));
37 mutex
38 .lock()
39 .unwrap_or_else(|poisoned| poisoned.into_inner())
40}