From 0442f38c83ec12cd93f6e906c4c43d38383d08bb Mon Sep 17 00:00:00 2001 From: Ed Page Date: Tue, 18 Dec 2018 09:25:55 -0700 Subject: [PATCH] fix(compiler): Rename LiquidOptions BREAKING CHANGE: Renamed `LiquidOptions` to `Language` and made it so it can't be directly constructed. --- liquid-compiler/src/block.rs | 12 ++++++------ liquid-compiler/src/filter.rs | 2 +- liquid-compiler/src/lang.rs | 16 ++++++++++++---- liquid-compiler/src/parser.rs | 35 ++++++++++++++--------------------- liquid-compiler/src/tag.rs | 12 ++++++------ src/parser.rs | 13 ++++++------- src/tags/assign_tag.rs | 8 ++++---- src/tags/capture_block.rs | 8 ++++---- src/tags/case_block.rs | 8 ++++---- src/tags/comment_block.rs | 8 ++++---- src/tags/cycle_tag.rs | 10 +++++----- src/tags/for_block.rs | 10 +++++----- src/tags/if_block.rs | 12 ++++++------ src/tags/ifchanged_block.rs | 8 ++++---- src/tags/include_tag.rs | 10 +++++----- src/tags/increment_tags.rs | 10 +++++----- src/tags/interrupt_tags.rs | 10 +++++----- src/tags/raw_block.rs | 8 ++++---- 18 files changed, 100 insertions(+), 100 deletions(-) diff --git a/liquid-compiler/src/block.rs b/liquid-compiler/src/block.rs index 729ff0e79..fb92aca36 100644 --- a/liquid-compiler/src/block.rs +++ b/liquid-compiler/src/block.rs @@ -1,7 +1,7 @@ use liquid_error::Result; use liquid_interpreter::Renderable; -use super::LiquidOptions; +use super::Language; use super::TagBlock; use super::TagTokenIter; @@ -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>; } @@ -42,7 +42,7 @@ impl Clone for Box { } } -pub type FnParseBlock = fn(&str, TagTokenIter, TagBlock, &LiquidOptions) -> Result>; +pub type FnParseBlock = fn(&str, TagTokenIter, TagBlock, &Language) -> Result>; #[derive(Clone)] struct FnBlockParser { @@ -61,7 +61,7 @@ impl ParseBlock for FnBlockParser { tag_name: &str, arguments: TagTokenIter, tokens: TagBlock, - options: &LiquidOptions, + options: &Language, ) -> Result> { (self.parser)(tag_name, arguments, tokens, options) } @@ -84,7 +84,7 @@ impl ParseBlock for BoxedBlockParser { tag_name: &str, arguments: TagTokenIter, tokens: TagBlock, - options: &LiquidOptions, + options: &Language, ) -> Result> { match self.parser { BlockParserEnum::Fun(ref f) => f.parse(tag_name, arguments, tokens, options), diff --git a/liquid-compiler/src/filter.rs b/liquid-compiler/src/filter.rs index e0fb2f49c..48f26eb1f 100644 --- a/liquid-compiler/src/filter.rs +++ b/liquid-compiler/src/filter.rs @@ -11,7 +11,7 @@ pub type FilterResult = Result; /// 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; diff --git a/liquid-compiler/src/lang.rs b/liquid-compiler/src/lang.rs index 2a4f7ff7a..01d89a38c 100644 --- a/liquid-compiler/src/lang.rs +++ b/liquid-compiler/src/lang.rs @@ -6,20 +6,28 @@ use super::NullInclude; use super::PluginRegistry; #[derive(Clone)] -pub struct LiquidOptions { +pub struct Language { pub blocks: PluginRegistry, pub tags: PluginRegistry, pub filters: PluginRegistry, pub include_source: Box, + 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(), } } } diff --git a/liquid-compiler/src/parser.rs b/liquid-compiler/src/parser.rs index 2adefcd5b..c8255652b 100644 --- a/liquid-compiler/src/parser.rs +++ b/liquid-compiler/src/parser.rs @@ -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; @@ -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>> { +pub fn parse(text: &str, options: &Language) -> Result>> { let mut liquid = LiquidParser::parse(Rule::LiquidFile, text) .map_err(convert_pest_error)? .next() @@ -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 { +fn parse_filter(filter: Pair, options: &Language) -> Result { if filter.as_rule() != Rule::Filter { panic!("Expected a filter."); } @@ -202,7 +202,7 @@ fn parse_filter(filter: Pair, options: &LiquidOptions) -> Result { /// 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 { +fn parse_filter_chain(chain: Pair, options: &Language) -> Result { if chain.as_rule() != Rule::FilterChain { panic!("Expected an expression with filters."); } @@ -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>> { + pub fn parse_all(&mut self, options: &Language) -> Result>> { let mut renderables = Vec::new(); while let Some(r) = self.parse_next(options)? { renderables.push(r); @@ -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>> { + pub fn parse_next(&mut self, options: &Language) -> Result>> { match self.next()? { None => Ok(None), Some(element) => Ok(Some(element.parse(self, options)?)), @@ -415,11 +415,7 @@ 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> { + pub fn parse(self, tag_block: &mut TagBlock, options: &Language) -> Result> { self.parse_pair(&mut tag_block.iter, options) } @@ -427,7 +423,7 @@ impl<'a> Tag<'a> { fn parse_pair( self, next_elements: &mut Iterator, - options: &LiquidOptions, + options: &Language, ) -> Result> { let (name, tokens) = (self.name, self.tokens); let position = name.as_span(); @@ -477,7 +473,7 @@ impl<'a> From> 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> { + pub fn parse(self, options: &Language) -> Result> { let filter_chain = self .element .into_inner() @@ -524,7 +520,7 @@ impl<'a> BlockElement<'a> { pub fn parse( self, block: &mut TagBlock<'a, '_>, - options: &LiquidOptions, + options: &Language, ) -> Result> { match self { BlockElement::Raw(raw) => Ok(raw.to_renderable()), @@ -537,7 +533,7 @@ impl<'a> BlockElement<'a> { fn parse_pair( self, next_elements: &mut Iterator, - options: &LiquidOptions, + options: &Language, ) -> Result> { match self { BlockElement::Raw(raw) => Ok(raw.to_renderable()), @@ -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(_) => { @@ -763,7 +756,7 @@ impl<'a> TagToken<'a> { } } - fn expect_filter_chain_err(&mut self, options: &LiquidOptions) -> Result { + fn expect_filter_chain_err(&mut self, options: &Language) -> Result { let t = self .unwrap_filter_chain() .map_err(|_| Error::with_msg("failed to parse"))?; @@ -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)); diff --git a/liquid-compiler/src/tag.rs b/liquid-compiler/src/tag.rs index caf26c63b..b1f9349ff 100644 --- a/liquid-compiler/src/tag.rs +++ b/liquid-compiler/src/tag.rs @@ -1,7 +1,7 @@ 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. @@ -9,13 +9,13 @@ use super::TagTokenIter; /// 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>; } @@ -37,7 +37,7 @@ impl Clone for Box { self.clone_box() } } -pub type FnParseTag = fn(&str, TagTokenIter, &LiquidOptions) -> Result>; +pub type FnParseTag = fn(&str, TagTokenIter, &Language) -> Result>; #[derive(Clone)] struct FnTagParser { @@ -55,7 +55,7 @@ impl ParseTag for FnTagParser { &self, tag_name: &str, arguments: TagTokenIter, - options: &LiquidOptions, + options: &Language, ) -> Result> { (self.parser)(tag_name, arguments, options) } @@ -77,7 +77,7 @@ impl ParseTag for BoxedTagParser { &self, tag_name: &str, arguments: TagTokenIter, - options: &LiquidOptions, + options: &Language, ) -> Result> { match self.parser { TagParserEnum::Fun(ref f) => f.parse(tag_name, arguments, options), diff --git a/src/parser.rs b/src/parser.rs index 49d17591d..12900d442 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -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 { diff --git a/src/tags/assign_tag.rs b/src/tags/assign_tag.rs index cab1b03cb..dbb678573 100644 --- a/src/tags/assign_tag.rs +++ b/src/tags/assign_tag.rs @@ -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; @@ -35,7 +35,7 @@ impl Renderable for Assign { pub fn assign_tag( _tag_name: &str, mut arguments: TagTokenIter, - options: &LiquidOptions, + options: &Language, ) -> Result> { let dst = arguments .expect_next("Identifier expected.")? @@ -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()); diff --git a/src/tags/capture_block.rs b/src/tags/capture_block.rs index 89f77f6bf..d2634f687 100644 --- a/src/tags/capture_block.rs +++ b/src/tags/capture_block.rs @@ -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; @@ -41,7 +41,7 @@ pub fn capture_block( _tag_name: &str, mut arguments: TagTokenIter, mut tokens: TagBlock, - options: &LiquidOptions, + options: &Language, ) -> Result> { let id = arguments .expect_next("Identifier expected")? @@ -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()); diff --git a/src/tags/case_block.rs b/src/tags/case_block.rs index 3cd5507c6..867141335 100644 --- a/src/tags/case_block.rs +++ b/src/tags/case_block.rs @@ -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; @@ -113,7 +113,7 @@ pub fn case_block( _tag_name: &str, mut arguments: TagTokenIter, mut tokens: TagBlock, - options: &LiquidOptions, + options: &Language, ) -> Result> { let target = arguments .expect_next("Value expected.")? @@ -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()); diff --git a/src/tags/comment_block.rs b/src/tags/comment_block.rs index 70c4eba47..9ecfc5e1e 100644 --- a/src/tags/comment_block.rs +++ b/src/tags/comment_block.rs @@ -3,7 +3,7 @@ use std::io::Write; use liquid_error::Result; use compiler::BlockElement; -use compiler::LiquidOptions; +use compiler::Language; use compiler::TagBlock; use compiler::TagTokenIter; use interpreter::Context; @@ -22,7 +22,7 @@ pub fn comment_block( tag_name: &str, mut arguments: TagTokenIter, mut tokens: TagBlock, - options: &LiquidOptions, + options: &Language, ) -> Result> { // no arguments should be supplied, trying to supply them is an error arguments.expect_nothing()?; @@ -46,8 +46,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("comment", (comment_block as compiler::FnParseBlock).into()); diff --git a/src/tags/cycle_tag.rs b/src/tags/cycle_tag.rs index a1deb37b2..82262907b 100644 --- a/src/tags/cycle_tag.rs +++ b/src/tags/cycle_tag.rs @@ -4,7 +4,7 @@ use std::io::Write; use itertools; use liquid_error::{Error, Result, ResultLiquidChainExt, ResultLiquidExt}; -use compiler::LiquidOptions; +use compiler::Language; use compiler::TagToken; use compiler::TagTokenIter; use compiler::TryMatchToken; @@ -40,7 +40,7 @@ impl Renderable for Cycle { } /// Internal implementation of cycle, to allow easier testing. -fn parse_cycle(mut arguments: TagTokenIter, _options: &LiquidOptions) -> Result { +fn parse_cycle(mut arguments: TagTokenIter, _options: &Language) -> Result { let mut name = String::new(); let mut values = Vec::new(); @@ -97,7 +97,7 @@ fn parse_cycle(mut arguments: TagTokenIter, _options: &LiquidOptions) -> Result< pub fn cycle_tag( _tag_name: &str, arguments: TagTokenIter, - options: &LiquidOptions, + options: &Language, ) -> Result> { parse_cycle(arguments, options).map(|opt| Box::new(opt) as Box) } @@ -137,8 +137,8 @@ mod test { use interpreter; use value::Value; - fn options() -> LiquidOptions { - let mut options = LiquidOptions::default(); + fn options() -> Language { + let mut options = Language::default(); options .tags .register("cycle", (cycle_tag as compiler::FnParseTag).into()); diff --git a/src/tags/for_block.rs b/src/tags/for_block.rs index 202d0f23f..10abd699d 100644 --- a/src/tags/for_block.rs +++ b/src/tags/for_block.rs @@ -6,7 +6,7 @@ use liquid_error::{Error, Result, ResultLiquidChainExt, ResultLiquidExt}; use liquid_value::{Object, Scalar, Value}; use compiler::BlockElement; -use compiler::LiquidOptions; +use compiler::Language; use compiler::TagBlock; use compiler::TagTokenIter; use compiler::TryMatchToken; @@ -236,7 +236,7 @@ pub fn for_block( _tag_name: &str, mut arguments: TagTokenIter, mut tokens: TagBlock, - options: &LiquidOptions, + options: &Language, ) -> Result> { let var_name = arguments .expect_next("Identifier expected.")? @@ -430,7 +430,7 @@ pub fn tablerow_block( _tag_name: &str, mut arguments: TagTokenIter, mut tokens: TagBlock, - options: &LiquidOptions, + options: &Language, ) -> Result> { let var_name = arguments .expect_next("Identifier expected.")? @@ -505,8 +505,8 @@ mod test { use super::*; - fn options() -> LiquidOptions { - let mut options = LiquidOptions::default(); + fn options() -> Language { + let mut options = Language::default(); options .blocks .register("for", (for_block as compiler::FnParseBlock).into()); diff --git a/src/tags/if_block.rs b/src/tags/if_block.rs index 4674d1edc..e605bbc92 100644 --- a/src/tags/if_block.rs +++ b/src/tags/if_block.rs @@ -5,7 +5,7 @@ use liquid_error::{Error, Result, ResultLiquidExt}; use liquid_value::Value; use compiler::BlockElement; -use compiler::LiquidOptions; +use compiler::Language; use compiler::TagBlock; use compiler::TagToken; use compiler::TagTokenIter; @@ -301,7 +301,7 @@ pub fn unless_block( _tag_name: &str, arguments: TagTokenIter, mut tokens: TagBlock, - options: &LiquidOptions, + options: &Language, ) -> Result> { let condition = parse_condition(arguments)?; @@ -338,7 +338,7 @@ fn parse_if( tag_name: &'static str, arguments: TagTokenIter, tokens: &mut TagBlock, - options: &LiquidOptions, + options: &Language, ) -> Result> { let condition = parse_condition(arguments)?; @@ -378,7 +378,7 @@ pub fn if_block( _tag_name: &str, arguments: TagTokenIter, mut tokens: TagBlock, - options: &LiquidOptions, + options: &Language, ) -> Result> { let conditional = parse_if("if", arguments, &mut tokens, options)?; @@ -405,8 +405,8 @@ mod test { use value::Object; use value::Value; - fn options() -> LiquidOptions { - let mut options = LiquidOptions::default(); + fn options() -> Language { + let mut options = Language::default(); options .blocks .register("if", (if_block as compiler::FnParseBlock).into()); diff --git a/src/tags/ifchanged_block.rs b/src/tags/ifchanged_block.rs index 344243670..ffa443137 100644 --- a/src/tags/ifchanged_block.rs +++ b/src/tags/ifchanged_block.rs @@ -2,7 +2,7 @@ use std::io::Write; use liquid_error::{Result, ResultLiquidChainExt, ResultLiquidExt}; -use compiler::LiquidOptions; +use compiler::Language; use compiler::TagBlock; use compiler::TagTokenIter; use interpreter::Context; @@ -40,7 +40,7 @@ pub fn ifchanged_block( _tag_name: &str, mut arguments: TagTokenIter, mut tokens: TagBlock, - options: &LiquidOptions, + options: &Language, ) -> Result> { // no arguments should be supplied, trying to supply them is an error arguments.expect_nothing()?; @@ -79,8 +79,8 @@ mod test { use interpreter; use tags; - fn options() -> LiquidOptions { - let mut options = LiquidOptions::default(); + fn options() -> Language { + let mut options = Language::default(); options.blocks.register( "ifchanged", (ifchanged_block as compiler::FnParseBlock).into(), diff --git a/src/tags/include_tag.rs b/src/tags/include_tag.rs index f0bb8ad5e..61de46dc5 100644 --- a/src/tags/include_tag.rs +++ b/src/tags/include_tag.rs @@ -3,7 +3,7 @@ use std::io::Write; use liquid_error::{Result, ResultLiquidExt}; use compiler::parse; -use compiler::LiquidOptions; +use compiler::Language; use compiler::TagTokenIter; use compiler::TryMatchToken; use interpreter::Context; @@ -26,7 +26,7 @@ impl Renderable for Include { } } -fn parse_partial(name: &str, options: &LiquidOptions) -> Result