use std::cell::RefCell;
use std::path::PathBuf;
thread_local! {
static PIPELINE_SOURCE_DIR: RefCell<Option<PathBuf>> = const { RefCell::new(None) };
}
pub struct SourceDirGuard {
prev: Option<PathBuf>,
}
impl SourceDirGuard {
pub(crate) fn new(dir: Option<PathBuf>) -> Self {
let prev = get();
set(dir);
Self { prev }
}
}
impl Drop for SourceDirGuard {
fn drop(&mut self) {
if let Some(prev) = self.prev.take() {
set(Some(prev));
} else {
set(None);
}
}
}
pub fn set(dir: Option<PathBuf>) {
PIPELINE_SOURCE_DIR.with(|d| *d.borrow_mut() = dir);
}
pub fn get() -> Option<PathBuf> {
PIPELINE_SOURCE_DIR.with(|d| d.borrow().clone())
}