Skip to content

Commit

Permalink
fix(compiler): Rename LiquidOptions
Browse files Browse the repository at this point in the history
BREAKING CHANGE: Renamed `LiquidOptions` to `Language` and made it so it
can't be directly constructed.
  • Loading branch information
epage committed Dec 18, 2018
1 parent 8d029b6 commit 0442f38
Show file tree
Hide file tree
Showing 18 changed files with 100 additions and 100 deletions.
12 changes: 6 additions & 6 deletions liquid-compiler/src/block.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use liquid_error::Result;
use liquid_interpreter::Renderable;

use super::LiquidOptions;
use super::Language;
use super::TagBlock;
use super::TagTokenIter;

Expand All @@ -12,14 +12,14 @@ use super::TagTokenIter;
/// a new `Renderable` based on its parameters. The received parameters specify the name
/// of the block, the argument [Tokens](lexer/enum.Token.html) passed to
/// the block, a Vec of all [Elements](lexer/enum.Element.html) inside the block and
/// the global [`LiquidOptions`](struct.LiquidOptions.html).
/// the global [`Language`](struct.Language.html).
pub trait ParseBlock: Send + Sync + ParseBlockClone {
fn parse(
&self,
tag_name: &str,
arguments: TagTokenIter,
block: TagBlock,
options: &LiquidOptions,
options: &Language,
) -> Result<Box<Renderable>>;
}

Expand All @@ -42,7 +42,7 @@ impl Clone for Box<ParseBlock> {
}
}

pub type FnParseBlock = fn(&str, TagTokenIter, TagBlock, &LiquidOptions) -> Result<Box<Renderable>>;
pub type FnParseBlock = fn(&str, TagTokenIter, TagBlock, &Language) -> Result<Box<Renderable>>;

#[derive(Clone)]
struct FnBlockParser {
Expand All @@ -61,7 +61,7 @@ impl ParseBlock for FnBlockParser {
tag_name: &str,
arguments: TagTokenIter,
tokens: TagBlock,
options: &LiquidOptions,
options: &Language,
) -> Result<Box<Renderable>> {
(self.parser)(tag_name, arguments, tokens, options)
}
Expand All @@ -84,7 +84,7 @@ impl ParseBlock for BoxedBlockParser {
tag_name: &str,
arguments: TagTokenIter,
tokens: TagBlock,
options: &LiquidOptions,
options: &Language,
) -> Result<Box<Renderable>> {
match self.parser {
BlockParserEnum::Fun(ref f) => f.parse(tag_name, arguments, tokens, options),
Expand Down
2 changes: 1 addition & 1 deletion liquid-compiler/src/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub type FilterResult = Result<Value, liquid_error::Error>;
/// This function will be called whenever the parser encounters a tag and returns
/// a new [Renderable](trait.Renderable.html) based on its parameters. The received parameters
/// specify the name of the tag, the argument [Tokens](lexer/enum.Token.html) passed to
/// the tag and the global [`LiquidOptions`](struct.LiquidOptions.html).
/// the tag and the global [`Language`](struct.Language.html).
pub trait FilterValue: Send + Sync + FilterValueClone + Debug {
/// Filter `input` based on `arguments`.
fn filter(&self, input: &Value, arguments: &[Value]) -> FilterResult;
Expand Down
16 changes: 12 additions & 4 deletions liquid-compiler/src/lang.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,28 @@ use super::NullInclude;
use super::PluginRegistry;

#[derive(Clone)]
pub struct LiquidOptions {
pub struct Language {
pub blocks: PluginRegistry<BoxedBlockParser>,
pub tags: PluginRegistry<BoxedTagParser>,
pub filters: PluginRegistry<BoxedValueFilter>,
pub include_source: Box<Include>,
non_exhaustive: (),
}

impl Default for LiquidOptions {
fn default() -> LiquidOptions {
LiquidOptions {
impl Language {
pub fn empty() -> Self {
Default::default()
}
}

impl Default for Language {
fn default() -> Language {
Language {
blocks: Default::default(),
tags: Default::default(),
filters: Default::default(),
include_source: Box::new(NullInclude::new()),
non_exhaustive: Default::default(),
}
}
}
35 changes: 14 additions & 21 deletions liquid-compiler/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use liquid_interpreter::Renderable;
use liquid_interpreter::Variable;
use liquid_value::Value;

use super::LiquidOptions;
use super::Language;
use super::ParseBlock;
use super::ParseTag;
use super::Text;
Expand Down Expand Up @@ -60,7 +60,7 @@ fn error_from_pair(pair: Pair, msg: String) -> Error {
}

/// Parses the provided &str into a number of Renderable items.
pub fn parse(text: &str, options: &LiquidOptions) -> Result<Vec<Box<Renderable>>> {
pub fn parse(text: &str, options: &Language) -> Result<Vec<Box<Renderable>>> {
let mut liquid = LiquidParser::parse(Rule::LiquidFile, text)
.map_err(convert_pest_error)?
.next()
Expand Down Expand Up @@ -175,7 +175,7 @@ fn parse_value(value: Pair) -> Expression {

/// Parses a `FilterCall` from a `Pair` with a filter.
/// This `Pair` must be `Rule::Filter`.
fn parse_filter(filter: Pair, options: &LiquidOptions) -> Result<FilterCall> {
fn parse_filter(filter: Pair, options: &Language) -> Result<FilterCall> {
if filter.as_rule() != Rule::Filter {
panic!("Expected a filter.");
}
Expand All @@ -202,7 +202,7 @@ fn parse_filter(filter: Pair, options: &LiquidOptions) -> Result<FilterCall> {

/// Parses a `FilterChain` from a `Pair` with a filter chain.
/// This `Pair` must be `Rule::FilterChain`.
fn parse_filter_chain(chain: Pair, options: &LiquidOptions) -> Result<FilterChain> {
fn parse_filter_chain(chain: Pair, options: &Language) -> Result<FilterChain> {
if chain.as_rule() != Rule::FilterChain {
panic!("Expected an expression with filters.");
}
Expand Down Expand Up @@ -290,7 +290,7 @@ impl<'a, 'b> TagBlock<'a, 'b> {
}

/// A convenient method that parses every element remaining in the block.
pub fn parse_all(&mut self, options: &LiquidOptions) -> Result<Vec<Box<Renderable>>> {
pub fn parse_all(&mut self, options: &Language) -> Result<Vec<Box<Renderable>>> {
let mut renderables = Vec::new();
while let Some(r) = self.parse_next(options)? {
renderables.push(r);
Expand All @@ -301,7 +301,7 @@ impl<'a, 'b> TagBlock<'a, 'b> {
/// Parses the next element in the block just as if it weren't inside any block.
///
/// Returns none if no element is left and raises the same errors as `next()`.
pub fn parse_next(&mut self, options: &LiquidOptions) -> Result<Option<Box<Renderable>>> {
pub fn parse_next(&mut self, options: &Language) -> Result<Option<Box<Renderable>>> {
match self.next()? {
None => Ok(None),
Some(element) => Ok(Some(element.parse(self, options)?)),
Expand Down Expand Up @@ -415,19 +415,15 @@ impl<'a> Tag<'a> {
}

/// Parses the tag just as if it weren't inside any block.
pub fn parse(
self,
tag_block: &mut TagBlock,
options: &LiquidOptions,
) -> Result<Box<Renderable>> {
pub fn parse(self, tag_block: &mut TagBlock, options: &Language) -> Result<Box<Renderable>> {
self.parse_pair(&mut tag_block.iter, options)
}

/// The same as `parse`, but directly takes an iterator over `Pair`s instead of a TagBlock.
fn parse_pair(
self,
next_elements: &mut Iterator<Item = Pair>,
options: &LiquidOptions,
options: &Language,
) -> Result<Box<Renderable>> {
let (name, tokens) = (self.name, self.tokens);
let position = name.as_span();
Expand Down Expand Up @@ -477,7 +473,7 @@ impl<'a> From<Pair<'a>> for Exp<'a> {

impl<'a> Exp<'a> {
/// Parses the expression just as if it weren't inside any block.
pub fn parse(self, options: &LiquidOptions) -> Result<Box<Renderable>> {
pub fn parse(self, options: &Language) -> Result<Box<Renderable>> {
let filter_chain = self
.element
.into_inner()
Expand Down Expand Up @@ -524,7 +520,7 @@ impl<'a> BlockElement<'a> {
pub fn parse(
self,
block: &mut TagBlock<'a, '_>,
options: &LiquidOptions,
options: &Language,
) -> Result<Box<Renderable>> {
match self {
BlockElement::Raw(raw) => Ok(raw.to_renderable()),
Expand All @@ -537,7 +533,7 @@ impl<'a> BlockElement<'a> {
fn parse_pair(
self,
next_elements: &mut Iterator<Item = Pair>,
options: &LiquidOptions,
options: &Language,
) -> Result<Box<Renderable>> {
match self {
BlockElement::Raw(raw) => Ok(raw.to_renderable()),
Expand Down Expand Up @@ -750,10 +746,7 @@ impl<'a> TagToken<'a> {
}

/// Tries to obtain a `FilterChain` from this token.
pub fn expect_filter_chain(
mut self,
options: &LiquidOptions,
) -> TryMatchToken<'a, FilterChain> {
pub fn expect_filter_chain(mut self, options: &Language) -> TryMatchToken<'a, FilterChain> {
match self.expect_filter_chain_err(options) {
Ok(t) => TryMatchToken::Matches(t),
Err(_) => {
Expand All @@ -763,7 +756,7 @@ impl<'a> TagToken<'a> {
}
}

fn expect_filter_chain_err(&mut self, options: &LiquidOptions) -> Result<FilterChain> {
fn expect_filter_chain_err(&mut self, options: &Language) -> Result<FilterChain> {
let t = self
.unwrap_filter_chain()
.map_err(|_| Error::with_msg("failed to parse"))?;
Expand Down Expand Up @@ -953,7 +946,7 @@ mod test {

#[test]
fn test_whitespace_control() {
let options = LiquidOptions::default();
let options = Language::default();

let mut context = Context::new();
context.stack_mut().set_global("exp", Value::scalar(5));
Expand Down
12 changes: 6 additions & 6 deletions liquid-compiler/src/tag.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
use liquid_error::Result;
use liquid_interpreter::Renderable;

use super::LiquidOptions;
use super::Language;
use super::TagTokenIter;

/// A trait for creating custom tags. This is a simple type alias for a function.
///
/// This function will be called whenever the parser encounters a tag and returns
/// a new [Renderable](trait.Renderable.html) based on its parameters. The received parameters
/// specify the name of the tag, the argument [Tokens](lexer/enum.Token.html) passed to
/// the tag and the global [`LiquidOptions`](struct.LiquidOptions.html).
/// the tag and the global [`Language`](struct.Language.html).
pub trait ParseTag: Send + Sync + ParseTagClone {
fn parse(
&self,
tag_name: &str,
arguments: TagTokenIter,
options: &LiquidOptions,
options: &Language,
) -> Result<Box<Renderable>>;
}

Expand All @@ -37,7 +37,7 @@ impl Clone for Box<ParseTag> {
self.clone_box()
}
}
pub type FnParseTag = fn(&str, TagTokenIter, &LiquidOptions) -> Result<Box<Renderable>>;
pub type FnParseTag = fn(&str, TagTokenIter, &Language) -> Result<Box<Renderable>>;

#[derive(Clone)]
struct FnTagParser {
Expand All @@ -55,7 +55,7 @@ impl ParseTag for FnTagParser {
&self,
tag_name: &str,
arguments: TagTokenIter,
options: &LiquidOptions,
options: &Language,
) -> Result<Box<Renderable>> {
(self.parser)(tag_name, arguments, options)
}
Expand All @@ -77,7 +77,7 @@ impl ParseTag for BoxedTagParser {
&self,
tag_name: &str,
arguments: TagTokenIter,
options: &LiquidOptions,
options: &Language,
) -> Result<Box<Renderable>> {
match self.parser {
TagParserEnum::Fun(ref f) => f.parse(tag_name, arguments, options),
Expand Down
13 changes: 6 additions & 7 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,19 +192,18 @@ impl ParserBuilder {
let include_source =
include_source.unwrap_or_else(|| Box::new(compiler::NullInclude::new()));

let options = compiler::LiquidOptions {
blocks,
tags,
filters,
include_source,
};
let mut options = compiler::Language::empty();
options.blocks = blocks;
options.tags = tags;
options.filters = filters;
options.include_source = include_source;
Parser { options }
}
}

#[derive(Default, Clone)]
pub struct Parser {
options: compiler::LiquidOptions,
options: compiler::Language,
}

impl Parser {
Expand Down
8 changes: 4 additions & 4 deletions src/tags/assign_tag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use liquid_error::Result;
use liquid_error::ResultLiquidExt;

use compiler::FilterChain;
use compiler::LiquidOptions;
use compiler::Language;
use compiler::TagTokenIter;
use interpreter::Context;
use interpreter::Renderable;
Expand Down Expand Up @@ -35,7 +35,7 @@ impl Renderable for Assign {
pub fn assign_tag(
_tag_name: &str,
mut arguments: TagTokenIter,
options: &LiquidOptions,
options: &Language,
) -> Result<Box<Renderable>> {
let dst = arguments
.expect_next("Identifier expected.")?
Expand Down Expand Up @@ -68,8 +68,8 @@ mod test {
use value::Scalar;
use value::Value;

fn options() -> LiquidOptions {
let mut options = LiquidOptions::default();
fn options() -> Language {
let mut options = Language::default();
options
.tags
.register("assign", (assign_tag as compiler::FnParseTag).into());
Expand Down
8 changes: 4 additions & 4 deletions src/tags/capture_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::io::Write;
use liquid_error::{Result, ResultLiquidExt};
use liquid_value::Value;

use compiler::LiquidOptions;
use compiler::Language;
use compiler::TagBlock;
use compiler::TagTokenIter;
use interpreter::Context;
Expand Down Expand Up @@ -41,7 +41,7 @@ pub fn capture_block(
_tag_name: &str,
mut arguments: TagTokenIter,
mut tokens: TagBlock,
options: &LiquidOptions,
options: &Language,
) -> Result<Box<Renderable>> {
let id = arguments
.expect_next("Identifier expected")?
Expand Down Expand Up @@ -69,8 +69,8 @@ mod test {
use interpreter;
use value::Scalar;

fn options() -> LiquidOptions {
let mut options = LiquidOptions::default();
fn options() -> Language {
let mut options = Language::default();
options
.blocks
.register("capture", (capture_block as compiler::FnParseBlock).into());
Expand Down
8 changes: 4 additions & 4 deletions src/tags/case_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use liquid_error::{Result, ResultLiquidExt};
use liquid_value::Value;

use compiler::BlockElement;
use compiler::LiquidOptions;
use compiler::Language;
use compiler::TagBlock;
use compiler::TagTokenIter;
use compiler::TryMatchToken;
Expand Down Expand Up @@ -113,7 +113,7 @@ pub fn case_block(
_tag_name: &str,
mut arguments: TagTokenIter,
mut tokens: TagBlock,
options: &LiquidOptions,
options: &Language,
) -> Result<Box<Renderable>> {
let target = arguments
.expect_next("Value expected.")?
Expand Down Expand Up @@ -170,8 +170,8 @@ mod test {
use compiler;
use interpreter;

fn options() -> LiquidOptions {
let mut options = LiquidOptions::default();
fn options() -> Language {
let mut options = Language::default();
options
.blocks
.register("case", (case_block as compiler::FnParseBlock).into());
Expand Down
Loading

0 comments on commit 0442f38

Please sign in to comment.