summaryrefslogtreecommitdiff
path: root/src/eval/scope.rs
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2021-01-27 15:05:18 +0100
committerLaurenz <laurmaedje@gmail.com>2021-01-27 15:05:18 +0100
commit2036663ed25b5885a87eb3a80caec3fa2e258d77 (patch)
tree110ca98e4d76dc887b41c91685bb202c49730236 /src/eval/scope.rs
parent2641c2d20ef5ddaf8e1dc91f4a69abfe2c170e4d (diff)
Capture variables in templates 🔍
Diffstat (limited to 'src/eval/scope.rs')
-rw-r--r--src/eval/scope.rs10
1 files changed, 5 insertions, 5 deletions
diff --git a/src/eval/scope.rs b/src/eval/scope.rs
index 1ed34f86..9c966a24 100644
--- a/src/eval/scope.rs
+++ b/src/eval/scope.rs
@@ -5,19 +5,19 @@ use std::iter;
use super::Value;
/// A stack of scopes.
-#[derive(Debug, Clone, PartialEq)]
+#[derive(Debug, Default, Clone, PartialEq)]
pub struct Scopes<'a> {
/// The active scope.
top: Scope,
/// The stack of lower scopes.
scopes: Vec<Scope>,
/// The base scope.
- base: &'a Scope,
+ base: Option<&'a Scope>,
}
impl<'a> Scopes<'a> {
/// Create a new hierarchy of scopes.
- pub fn new(base: &'a Scope) -> Self {
+ pub fn new(base: Option<&'a Scope>) -> Self {
Self { top: Scope::new(), scopes: vec![], base }
}
@@ -43,7 +43,7 @@ impl<'a> Scopes<'a> {
pub fn get(&self, var: &str) -> Option<&Value> {
iter::once(&self.top)
.chain(self.scopes.iter().rev())
- .chain(iter::once(self.base))
+ .chain(self.base.into_iter())
.find_map(|scope| scope.get(var))
}
@@ -58,7 +58,7 @@ impl<'a> Scopes<'a> {
///
/// Defaults to `false` if the variable does not exist.
pub fn is_const(&self, var: &str) -> bool {
- self.base.get(var).is_some()
+ self.base.map_or(false, |base| base.get(var).is_some())
}
}