1
1
mirror of https://github.com/casey/just.git synced 2024-11-23 11:04:09 +03:00
just/src/scope.rs
2020-02-10 20:07:06 -08:00

58 lines
1.3 KiB
Rust

use crate::common::*;
#[derive(Debug)]
pub(crate) struct Scope<'src: 'run, 'run> {
parent: Option<&'run Scope<'src, 'run>>,
bindings: Table<'src, Binding<'src, String>>,
}
impl<'src, 'run> Scope<'src, 'run> {
pub(crate) fn child(parent: &'run Scope<'src, 'run>) -> Scope<'src, 'run> {
Scope {
parent: Some(parent),
bindings: Table::new(),
}
}
pub(crate) fn new() -> Scope<'src, 'run> {
Scope {
parent: None,
bindings: Table::new(),
}
}
pub(crate) fn bind(&mut self, export: bool, name: Name<'src>, value: String) {
self.bindings.insert(Binding {
name,
export,
value,
});
}
pub(crate) fn bound(&self, name: &str) -> bool {
self.bindings.contains_key(name)
}
pub(crate) fn value(&self, name: &str) -> Option<&str> {
if let Some(binding) = self.bindings.get(name) {
Some(binding.value.as_ref())
} else if let Some(parent) = self.parent {
parent.value(name)
} else {
None
}
}
pub(crate) fn bindings(&self) -> impl Iterator<Item = &Binding<String>> {
self.bindings.values()
}
pub(crate) fn names(&self) -> impl Iterator<Item = &str> {
self.bindings.keys().cloned()
}
pub(crate) fn parent(&self) -> Option<&'run Scope<'src, 'run>> {
self.parent
}
}