Skip to content

Commit

Permalink
fix: Cleanup each crate's API
Browse files Browse the repository at this point in the history
There is still a lot of work on this.

BREAKING CHANGE: A lot, but the most notable is `liquid::Value` is now
`liquid::value::Value`.
  • Loading branch information
epage committed Sep 23, 2018
1 parent 192f8b0 commit 2e4ab66
Show file tree
Hide file tree
Showing 52 changed files with 363 additions and 256 deletions.
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -34,8 +34,8 @@ let template = liquid::ParserBuilder::with_liquid()
.build()
.parse("Liquid! {{num | minus: 2}}").unwrap();

let mut globals = liquid::Object::new();
globals.insert("num".into(), liquid::Value::scalar(4f64));
let mut globals = liquid::value::Object::new();
globals.insert("num".into(), liquid::value::Value::scalar(4f64));

let output = template.render(&globals).unwrap();
assert_eq!(output, "Liquid! 2".to_string());
Expand Down
6 changes: 3 additions & 3 deletions benches/liquid.rs
Expand Up @@ -20,7 +20,7 @@ fn bench_render_text(b: &mut test::Bencher) {
.parse(TEXT_ONLY)
.expect("Benchmark template parsing failed");

let data = liquid::Object::new();
let data = liquid::value::Object::new();

b.iter(|| template.render(&data));
}
Expand Down Expand Up @@ -49,7 +49,7 @@ fn bench_render_variable(b: &mut test::Bencher) {
.parse(VARIABLE_ONLY)
.expect("Benchmark template parsing failed");

let data: liquid::Object =
let data: liquid::value::Object =
serde_yaml::from_str(VARIABLE_ONLY_OBJECT).expect("Benchmark object parsing failed");

b.iter(|| template.render(&data));
Expand Down Expand Up @@ -97,7 +97,7 @@ fn bench_render_template(b: &mut test::Bencher) {
.parse(ITERATE)
.expect("Benchmark template parsing failed");

let data: liquid::Object =
let data: liquid::value::Object =
serde_yaml::from_str(ITERATE_OBJECT).expect("Benchmark object parsing failed");

b.iter(|| template.render(&data));
Expand Down
1 change: 1 addition & 0 deletions liquid-compiler/Cargo.toml
Expand Up @@ -17,6 +17,7 @@ appveyor = { repository = "johannhof/liquid-rust" }
[dependencies]
regex = "1.0"
lazy_static = "1.0"
# Exposed in API
liquid-error = { version = "0.15.0", path = "../liquid-error" }
liquid-value = { version = "0.15.0", path = "../liquid-value" }
liquid-interpreter = { version = "0.15.0", path = "../liquid-interpreter" }
9 changes: 5 additions & 4 deletions liquid-compiler/src/block.rs
@@ -1,8 +1,9 @@
use liquid_interpreter::Renderable;

use super::error::Result;
use super::Element;
use super::LiquidOptions;
use super::Result;
use super::Token;
use interpreter::Renderable;

/// A trait for creating custom custom block-size tags (`{% if something %}{% endif %}`).
/// This is a simple type alias for a function.
Expand Down Expand Up @@ -45,11 +46,11 @@ pub type FnParseBlock = fn(&str, &[Token], &[Element], &LiquidOptions) -> Result

#[derive(Clone)]
struct FnBlockParser {
pub parser: FnParseBlock,
parser: FnParseBlock,
}

impl FnBlockParser {
pub fn new(parser: FnParseBlock) -> Self {
fn new(parser: FnParseBlock) -> Self {
Self { parser }
}
}
Expand Down
2 changes: 1 addition & 1 deletion liquid-compiler/src/include.rs
Expand Up @@ -2,7 +2,7 @@ use std::fs::File;
use std::io::prelude::Read;
use std::path;

use super::{Error, Result, ResultLiquidChainExt, ResultLiquidExt};
use super::error::{Error, Result, ResultLiquidChainExt, ResultLiquidExt};

pub trait Include: Send + Sync + IncludeClone {
fn include(&self, path: &str) -> Result<String>;
Expand Down
3 changes: 1 addition & 2 deletions liquid-compiler/src/lexer.rs
Expand Up @@ -7,8 +7,7 @@ use std::fmt;

use regex::Regex;

use super::{Error, Result};

use super::error::{Error, Result};
use super::ComparisonOperator;
use super::Token;

Expand Down
40 changes: 19 additions & 21 deletions liquid-compiler/src/lib.rs
@@ -1,17 +1,12 @@
#![warn(unreachable_pub)]
#![warn(unused_extern_crates)]

#[macro_use]
extern crate lazy_static;
extern crate regex;
extern crate liquid_error;
extern crate liquid_value;
extern crate liquid_interpreter;

// Minimize retrofits
mod interpreter {
pub(crate) use liquid_interpreter::*;
}
mod value {
pub(crate) use liquid_value::*;
}
extern crate liquid_value;
extern crate regex;

mod block;
mod include;
Expand All @@ -21,15 +16,18 @@ mod parser;
mod tag;
mod token;

pub use liquid_error::{Error, Result, ResultLiquidChainExt, ResultLiquidExt};
pub mod error {
pub use liquid_error::*;
}

pub mod value {
pub use liquid_value::*;
}

pub use self::block::{BoxedBlockParser, FnParseBlock, ParseBlock, ParseBlockClone};
pub use self::include::{FilesystemInclude, Include, IncludeClone, NullInclude};
pub use self::lexer::{tokenize, Element};
pub use self::options::LiquidOptions;
pub use self::parser::{
consume_value_token, expect, parse, parse_indexes, parse_output, split_block,
unexpected_token_error, value_token, BlockSplit,
};
pub use self::tag::{BoxedTagParser, FnParseTag, ParseTag, ParseTagClone};
pub use self::token::{ComparisonOperator, Token};
pub use block::*;
pub use include::*;
pub use lexer::*;
pub use options::*;
pub use parser::*;
pub use tag::*;
pub use token::*;
34 changes: 19 additions & 15 deletions liquid-compiler/src/parser.rs
Expand Up @@ -7,18 +7,18 @@ use std::collections::HashSet;
use std::iter::FromIterator;
use std::slice::Iter;

use super::{Error, Result};
use liquid_interpreter::Renderable;
use liquid_interpreter::Text;
use liquid_interpreter::Variable;
use liquid_interpreter::{FilterPrototype, Output};
use liquid_value::Index;

use super::error::{Error, Result};
use super::Element;
use super::LiquidOptions;
use super::ParseBlock;
use super::ParseTag;
use super::Token;
use interpreter::Renderable;
use interpreter::Text;
use interpreter::Variable;
use interpreter::{FilterPrototype, Output};
use value::Index;

/// Parses the provided elements into a number of Renderable items
/// This is the internal version of parse that accepts Elements tokenized
Expand Down Expand Up @@ -324,10 +324,12 @@ pub fn split_block<'a>(

#[cfg(test)]
mod test_parse_output {
use super::super::lexer::granularize;
use super::*;
use interpreter::Argument;
use value::Value;

use liquid_interpreter::Argument;
use liquid_value::Value;

use super::super::lexer::granularize;

#[test]
fn parses_filters() {
Expand Down Expand Up @@ -404,17 +406,19 @@ mod test_expect {

#[cfg(test)]
mod test_split_block {
use super::*;

use std::collections::HashMap;
use std::io::Write;

use liquid_interpreter;
use liquid_interpreter::Context;
use liquid_interpreter::Renderable;

use super::super::split_block;
use super::super::tokenize;
use super::super::BoxedBlockParser;
use super::super::FnParseBlock;
use super::*;
use interpreter;
use interpreter::Context;
use interpreter::Renderable;

#[derive(Debug)]
struct NullBlock;
Expand All @@ -425,7 +429,7 @@ mod test_split_block {
}
}

pub fn null_block(
fn null_block(
_tag_name: &str,
_arguments: &[Token],
_tokens: &[Element],
Expand All @@ -450,7 +454,7 @@ mod test_split_block {
let text = "{{}}";

let tokens = tokenize(&text).unwrap();
let template = parse(&tokens, &options()).map(interpreter::Template::new);
let template = parse(&tokens, &options()).map(liquid_interpreter::Template::new);
assert!(template.is_err());
}

Expand Down
8 changes: 4 additions & 4 deletions liquid-compiler/src/tag.rs
@@ -1,8 +1,8 @@
use super::Result;
use liquid_interpreter::Renderable;

use super::error::Result;
use super::LiquidOptions;
use super::Token;
use interpreter::Renderable;

/// A trait for creating custom tags. This is a simple type alias for a function.
///
Expand Down Expand Up @@ -42,11 +42,11 @@ pub type FnParseTag = fn(&str, &[Token], &LiquidOptions) -> Result<Box<Renderabl

#[derive(Clone)]
struct FnTagParser {
pub parser: FnParseTag,
parser: FnParseTag,
}

impl FnTagParser {
pub fn new(parser: FnParseTag) -> Self {
fn new(parser: FnParseTag) -> Self {
Self { parser }
}
}
Expand Down
12 changes: 7 additions & 5 deletions liquid-compiler/src/token.rs
@@ -1,10 +1,11 @@
use std::fmt;

use liquid_interpreter::Argument;
use liquid_interpreter::Variable;
use liquid_value::{Index, Value};

use super::error::Result;
use super::parser::unexpected_token_error;
use super::Result;
use interpreter::Argument;
use interpreter::Variable;
use value::{Index, Value};

#[derive(Copy, Clone, Debug, PartialEq)]
pub enum ComparisonOperator {
Expand Down Expand Up @@ -119,7 +120,8 @@ impl fmt::Display for Token {
#[cfg(test)]
mod test {
use super::*;
use interpreter::Context;

use liquid_interpreter::Context;

#[test]
fn evaluate_handles_string_literals() {
Expand Down
11 changes: 8 additions & 3 deletions liquid-error/src/error.rs
Expand Up @@ -16,7 +16,6 @@ pub trait ResultLiquidChainExt<T, E> {
F: FnOnce() -> String;
}

/// `Result` convenience extension methods for working with `Error`.
impl<T, E> ResultLiquidChainExt<T, E> for result::Result<T, E>
where
E: error::Error + Send + Sync + 'static,
Expand All @@ -33,13 +32,19 @@ where
}
}

/// Add context to a `liquid_error::Error`.
pub trait ResultLiquidExt<T> {
/// Add a new stack frame to the `liquid_error::Error`.
fn trace_with<F>(self, trace: F) -> Result<T>
where
F: FnOnce() -> Trace;

/// Add state the current stack frame.
fn context<S>(self, key: &'static str, value: &S) -> Result<T>
where
S: ToString;

/// Add state the current stack frame.
fn context_with<F>(self, context: F) -> Result<T>
where
F: FnOnce() -> (borrow::Cow<'static, str>, String);
Expand Down Expand Up @@ -215,11 +220,11 @@ impl Trace {
self.context.push((key, value));
}

pub fn get_trace(&self) -> Option<&str> {
pub(self) fn get_trace(&self) -> Option<&str> {
self.trace.as_ref().map(|s| s.as_ref())
}

pub fn get_context(&self) -> &[(borrow::Cow<'static, str>, String)] {
pub(self) fn get_context(&self) -> &[(borrow::Cow<'static, str>, String)] {
self.context.as_ref()
}
}
Expand Down
7 changes: 7 additions & 0 deletions liquid-error/src/lib.rs
@@ -1,3 +1,10 @@
//! Liquid Processing Errors.

#![warn(missing_docs)]
#![warn(missing_debug_implementations)]
#![warn(unreachable_pub)]
#![warn(unused_extern_crates)]

mod error;

pub use error::*;
1 change: 1 addition & 0 deletions liquid-interpreter/Cargo.toml
Expand Up @@ -17,6 +17,7 @@ appveyor = { repository = "johannhof/liquid-rust" }
[dependencies]
lazy_static = "1.0"
itertools = "0.7.0"
# Exposed in API
liquid-error = { version = "0.15.0", path = "../liquid-error" }
liquid-value = { version = "0.15.0", path = "../liquid-value" }

Expand Down
6 changes: 5 additions & 1 deletion liquid-interpreter/src/argument.rs
Expand Up @@ -3,16 +3,20 @@ use std::fmt;
use error::Result;
use value::Value;

use super::variable::Variable;
use super::Context;
use variable::Variable;

/// An un-evaluated `Value`.
#[derive(Debug, Clone, PartialEq)]
pub enum Argument {
/// Un-evaluated.
Var(Variable),
/// Evaluated.
Val(Value),
}

impl Argument {
/// Convert to a `Value`.
pub fn evaluate(&self, context: &Context) -> Result<Value> {
let val = match *self {
Argument::Val(ref x) => x.clone(),
Expand Down

0 comments on commit 2e4ab66

Please sign in to comment.