use super::clone::string_map_to_owned;
use std::borrow::Cow;
use std::collections::HashMap;
pub type VariableMap<'t> = HashMap<Cow<'t, str>, Cow<'t, str>>;
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct VariableScopes {
scopes: Vec<VariableMap<'static>>,
}
impl VariableScopes {
#[inline]
pub fn new() -> Self {
VariableScopes::default()
}
pub fn get(&self, name: &str) -> Option<&str> {
for scope in self.scopes.iter().rev() {
if let Some(value) = scope.get(name) {
return Some(value);
}
}
None
}
pub fn push_scope(&mut self, scope: &VariableMap) {
self.scopes.push(string_map_to_owned(scope));
}
pub fn pop_scope(&mut self) {
self.scopes.pop().expect("Scope stack was empty");
}
}