use std::cell::RefCell;
use std::path::{Path, PathBuf};
thread_local! {
static BASE_DIR: RefCell<Option<PathBuf>> = const { RefCell::new(None) };
}
pub(crate) fn with_base_dir<T>(dir: &Path, f: impl FnOnce() -> T) -> T {
let previous = BASE_DIR.with(|cell| cell.borrow_mut().replace(dir.to_path_buf()));
let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(f));
BASE_DIR.with(|cell| *cell.borrow_mut() = previous);
match result {
Ok(value) => value,
Err(payload) => std::panic::resume_unwind(payload),
}
}
pub(crate) fn base_dir_override() -> Option<PathBuf> {
BASE_DIR.with(|cell| cell.borrow().clone())
}