diff --git a/liquid-interpreter/src/context.rs b/liquid-interpreter/src/context.rs index 8d95a6208..2293fdbcb 100644 --- a/liquid-interpreter/src/context.rs +++ b/liquid-interpreter/src/context.rs @@ -7,7 +7,7 @@ use itertools; use value::{Object, PathRef, Scalar, Value}; use super::Expression; -use super::Globals; +use super::ValueStore; use super::PluginRegistry; use super::{BoxedValueFilter, FilterValue}; @@ -128,7 +128,7 @@ impl IfChangedState { /// Stack of variables. #[derive(Debug, Clone)] pub struct Stack<'g> { - globals: Option<&'g Globals>, + globals: Option<&'g ValueStore>, stack: Vec, // State of variables created through increment or decrement tags. indexes: Object, @@ -145,8 +145,8 @@ impl<'g> Stack<'g> { } } - /// Create a stack initialized with read-only `Globals`. - pub fn with_globals(globals: &'g Globals) -> Self { + /// Create a stack initialized with read-only `ValueStore`. + pub fn with_globals(globals: &'g ValueStore) -> Self { let mut stack = Self::empty(); stack.globals = Some(globals); stack @@ -196,37 +196,37 @@ impl<'g> Stack<'g> { } fn globals(&self) -> Vec<&str> { - let mut globals = self.globals.map(|g| g.globals()).unwrap_or_default(); + let mut globals = self.globals.map(|g| g.roots()).unwrap_or_default(); for frame in self.stack.iter() { - globals.extend(frame.globals()); + globals.extend(frame.roots()); } globals.sort(); globals.dedup(); globals } - fn find_path_frame<'a>(&'a self, path: PathRef) -> Option<&'a Globals> { + fn find_path_frame<'a>(&'a self, path: PathRef) -> Option<&'a ValueStore> { let key = path.iter().next()?; let key = key.to_str(); self.find_frame(key.as_ref()) } - fn find_frame<'a>(&'a self, name: &str) -> Option<&'a Globals> { + fn find_frame<'a>(&'a self, name: &str) -> Option<&'a ValueStore> { for frame in self.stack.iter().rev() { - if frame.contains_global(name) { + if frame.contains_root(name) { return Some(frame); } } if self .globals - .map(|g| g.contains_global(name)) + .map(|g| g.contains_root(name)) .unwrap_or(false) { return self.globals; } - if self.indexes.contains_global(name) { + if self.indexes.contains_root(name) { return Some(&self.indexes); } @@ -292,7 +292,7 @@ impl<'g> Default for Stack<'g> { /// Create processing context for a template. pub struct ContextBuilder<'g> { - globals: Option<&'g Globals>, + globals: Option<&'g ValueStore>, filters: sync::Arc>, } @@ -306,7 +306,7 @@ impl<'g> ContextBuilder<'g> { } /// Initialize the stack with the given globals. - pub fn set_globals(mut self, values: &'g Globals) -> Self { + pub fn set_globals(mut self, values: &'g ValueStore) -> Self { self.globals = Some(values); self } diff --git a/liquid-interpreter/src/store.rs b/liquid-interpreter/src/store.rs index 12457ca22..017a63b1b 100644 --- a/liquid-interpreter/src/store.rs +++ b/liquid-interpreter/src/store.rs @@ -7,12 +7,12 @@ use value::PathRef; use value::Value; /// Immutable view into a template's global variables. -pub trait Globals: fmt::Debug { - /// Check if global variable exists. - fn contains_global(&self, name: &str) -> bool; +pub trait ValueStore: fmt::Debug { + /// Check if root variable exists. + fn contains_root(&self, name: &str) -> bool; - /// Enumerate all globals - fn globals(&self) -> Vec<&str>; + /// Enumerate all root variables. + fn roots(&self) -> Vec<&str>; /// Check if variable exists. /// @@ -36,12 +36,12 @@ pub trait Globals: fmt::Debug { fn get_variable<'a>(&'a self, path: PathRef) -> Result<&'a Value>; } -impl Globals for Object { - fn contains_global(&self, name: &str) -> bool { +impl ValueStore for Object { + fn contains_root(&self, name: &str) -> bool { self.contains_key(name) } - fn globals(&self) -> Vec<&str> { + fn roots(&self) -> Vec<&str> { self.keys().map(|s| s.as_ref()).collect() } diff --git a/src/lib.rs b/src/lib.rs index b339810f9..f8360d5f0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -54,7 +54,7 @@ pub mod value { pub mod filters; pub mod tags; -pub use interpreter::Globals; +pub use interpreter::ValueStore; pub use liquid_error::Error; pub use parser::*; pub use template::*; diff --git a/src/template.rs b/src/template.rs index 8cb1e3612..a71e8a647 100644 --- a/src/template.rs +++ b/src/template.rs @@ -12,7 +12,7 @@ pub struct Template { impl Template { /// Renders an instance of the Template, using the given globals. - pub fn render(&self, globals: &interpreter::Globals) -> Result { + pub fn render(&self, globals: &interpreter::ValueStore) -> Result { const BEST_GUESS: usize = 10_000; let mut data = Vec::with_capacity(BEST_GUESS); self.render_to(&mut data, globals)?; @@ -21,7 +21,7 @@ impl Template { } /// Renders an instance of the Template, using the given globals. - pub fn render_to(&self, writer: &mut Write, globals: &interpreter::Globals) -> Result<()> { + pub fn render_to(&self, writer: &mut Write, globals: &interpreter::ValueStore) -> Result<()> { let mut data = interpreter::ContextBuilder::new() .set_filters(&self.filters) .set_globals(globals)