2022-06-18 21:56:31 -07:00
|
|
|
use super::*;
|
2019-12-07 03:09:21 -08:00
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub(crate) struct Scope<'src: 'run, 'run> {
|
2024-05-14 20:07:41 -07:00
|
|
|
parent: Option<&'run Self>,
|
2019-12-07 03:09:21 -08:00
|
|
|
bindings: Table<'src, Binding<'src, String>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'src, 'run> Scope<'src, 'run> {
|
2024-05-14 20:07:41 -07:00
|
|
|
pub(crate) fn child(&'run self) -> Self {
|
2023-12-27 20:27:15 -08:00
|
|
|
Self {
|
2021-09-16 06:44:40 -07:00
|
|
|
parent: Some(self),
|
2019-12-07 03:09:21 -08:00
|
|
|
bindings: Table::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-14 20:07:41 -07:00
|
|
|
pub(crate) fn new() -> Self {
|
2023-12-27 20:27:15 -08:00
|
|
|
Self {
|
2021-09-16 06:44:40 -07:00
|
|
|
parent: None,
|
2019-12-07 03:09:21 -08:00
|
|
|
bindings: Table::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn bind(&mut self, export: bool, name: Name<'src>, value: String) {
|
|
|
|
self.bindings.insert(Binding {
|
2024-05-14 18:39:42 -07:00
|
|
|
depth: 0,
|
2019-12-07 03:09:21 -08:00
|
|
|
export,
|
2021-05-07 00:14:38 -07:00
|
|
|
name,
|
2019-12-07 03:09:21 -08:00
|
|
|
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 {
|
2021-09-16 07:51:45 -07:00
|
|
|
self.parent?.value(name)
|
2019-12-07 03:09:21 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn bindings(&self) -> impl Iterator<Item = &Binding<String>> {
|
|
|
|
self.bindings.values()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn names(&self) -> impl Iterator<Item = &str> {
|
2022-01-30 12:16:10 -08:00
|
|
|
self.bindings.keys().copied()
|
2019-12-07 03:09:21 -08:00
|
|
|
}
|
|
|
|
|
2024-05-14 20:07:41 -07:00
|
|
|
pub(crate) fn parent(&self) -> Option<&'run Self> {
|
2019-12-07 03:09:21 -08:00
|
|
|
self.parent
|
|
|
|
}
|
|
|
|
}
|