From 2036663ed25b5885a87eb3a80caec3fa2e258d77 Mon Sep 17 00:00:00 2001 From: Laurenz Date: Wed, 27 Jan 2021 15:05:18 +0100 Subject: =?UTF-8?q?Capture=20variables=20in=20templates=20=F0=9F=94=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/eval/scope.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'src/eval/scope.rs') 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, /// 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()) } } -- cgit v1.2.3