use std::cell::RefCell;
use std::collections::HashMap;
use std::env::VarError;
thread_local! {
static OVERLAY: RefCell<HashMap<String, Option<String>>> = RefCell::new(HashMap::new());
}
pub fn var(key: &str) -> Result<String, VarError> {
if let Some(over) = OVERLAY.with(|m| m.borrow().get(key).cloned()) {
return over.ok_or(VarError::NotPresent);
}
std::env::var(key)
}
pub fn override_var(key: &str, value: Option<&str>) {
OVERLAY.with(|m| {
m.borrow_mut()
.insert(key.to_string(), value.map(str::to_string));
});
}
pub fn clear_override(key: &str) {
OVERLAY.with(|m| {
m.borrow_mut().remove(key);
});
}