Skip to content

Commit

Permalink
fix(interpreter): Rename Globals to ValueStore
Browse files Browse the repository at this point in the history
This isn't being used just for globals but also stack frames.

BREAKING CHANGE: Renamed `Globals` to `ValueStore`.
  • Loading branch information
epage committed Dec 10, 2018
1 parent 3110410 commit fbe4f2c
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 24 deletions.
26 changes: 13 additions & 13 deletions liquid-interpreter/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -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<Object>,
// State of variables created through increment or decrement tags.
indexes: Object,
Expand All @@ -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
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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<PluginRegistry<BoxedValueFilter>>,
}

Expand All @@ -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
}
Expand Down
16 changes: 8 additions & 8 deletions liquid-interpreter/src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand All @@ -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()
}

Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
4 changes: 2 additions & 2 deletions src/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> {
pub fn render(&self, globals: &interpreter::ValueStore) -> Result<String> {
const BEST_GUESS: usize = 10_000;
let mut data = Vec::with_capacity(BEST_GUESS);
self.render_to(&mut data, globals)?;
Expand All @@ -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)
Expand Down

0 comments on commit fbe4f2c

Please sign in to comment.