Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ cache:
- target

rust:
- 1.46.0
- stable
- nightly
- beta
Expand Down
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@ name = "mustache"
description = "Rust implementation of Mustache"
repository = "https://github.com/nickel-org/rust-mustache"
documentation = "http://nickel-org.github.io/rust-mustache"
version = "0.9.0"
version = "0.9.1"
authors = ["[email protected]"]
license = "MIT/Apache-2.0"

[features]
unstable = []

[dependencies]
log = "0.3.5"
log = "0.4"
serde = "1.0.0"

[dev-dependencies]
serde_derive = "1.0.0"
serde_json = "1.0.0"
tempdir = "0.3.4"
tempfile = "3"

[[test]]
name = "test"
Expand Down
24 changes: 12 additions & 12 deletions src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ impl MapBuilder {
let MapBuilder { mut data } = self;
let value = to_data(value)?;
data.insert(key.into(), value);
Ok(MapBuilder { data: data })
Ok(MapBuilder { data })
}

/// Add a `String` to the `MapBuilder`.
Expand All @@ -56,7 +56,7 @@ impl MapBuilder {
{
let MapBuilder { mut data } = self;
data.insert(key.into(), Data::String(value.into()));
MapBuilder { data: data }
MapBuilder { data }
}

/// Add a `bool` to the `MapBuilder`.
Expand All @@ -74,7 +74,7 @@ impl MapBuilder {
{
let MapBuilder { mut data } = self;
data.insert(key.into(), Data::Bool(value));
MapBuilder { data: data }
MapBuilder { data }
}

/// Add a `Vec` to the `MapBuilder`.
Expand All @@ -97,7 +97,7 @@ impl MapBuilder {
let MapBuilder { mut data } = self;
let builder = f(VecBuilder::new());
data.insert(key.into(), builder.build());
MapBuilder { data: data }
MapBuilder { data }
}

/// Add a `Map` to the `MapBuilder`.
Expand Down Expand Up @@ -126,7 +126,7 @@ impl MapBuilder {
let MapBuilder { mut data } = self;
let builder = f(MapBuilder::new());
data.insert(key.into(), builder.build());
MapBuilder { data: data }
MapBuilder { data }
}

/// Add a function to the `MapBuilder`.
Expand All @@ -147,7 +147,7 @@ impl MapBuilder {
{
let MapBuilder { mut data } = self;
data.insert(key.to_string(), Data::Fun(RefCell::new(Box::new(f))));
MapBuilder { data: data }
MapBuilder { data }
}

/// Return the built `Data`.
Expand Down Expand Up @@ -183,7 +183,7 @@ impl VecBuilder {
let VecBuilder { mut data } = self;
let value = to_data(value)?;
data.push(value);
Ok(VecBuilder { data: data })
Ok(VecBuilder { data })
}

/// Add a `String` to the `VecBuilder`.
Expand All @@ -199,7 +199,7 @@ impl VecBuilder {
pub fn push_str<T: ToString>(self, value: T) -> VecBuilder {
let VecBuilder { mut data } = self;
data.push(Data::String(value.to_string()));
VecBuilder { data: data }
VecBuilder { data }
}

/// Add a `bool` to the `VecBuilder`.
Expand All @@ -215,7 +215,7 @@ impl VecBuilder {
pub fn push_bool(self, value: bool) -> VecBuilder {
let VecBuilder { mut data } = self;
data.push(Data::Bool(value));
VecBuilder { data: data }
VecBuilder { data }
}

/// Add a `Vec` to the `MapBuilder`.
Expand All @@ -237,7 +237,7 @@ impl VecBuilder {
let VecBuilder { mut data } = self;
let builder = f(VecBuilder::new());
data.push(builder.build());
VecBuilder { data: data }
VecBuilder { data }
}

/// Add a `Map` to the `VecBuilder`.
Expand All @@ -264,7 +264,7 @@ impl VecBuilder {
let VecBuilder { mut data } = self;
let builder = f(MapBuilder::new());
data.push(builder.build());
VecBuilder { data: data }
VecBuilder { data }
}

/// Add a function to the `VecBuilder`.
Expand All @@ -285,7 +285,7 @@ impl VecBuilder {
{
let VecBuilder { mut data } = self;
data.push(Data::Fun(RefCell::new(Box::new(f))));
VecBuilder { data: data }
VecBuilder { data }
}

#[inline]
Expand Down
15 changes: 6 additions & 9 deletions src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ impl<T: Iterator<Item = char>> Compiler<T> {
/// Construct a default compiler.
pub fn new(ctx: Context, reader: T) -> Compiler<T> {
Compiler {
ctx: ctx,
reader: reader,
ctx,
reader,
partials: HashMap::new(),
otag: "{{".to_string(),
ctag: "}}".to_string(),
Expand All @@ -38,13 +38,7 @@ impl<T: Iterator<Item = char>> Compiler<T> {
otag: String,
ctag: String)
-> Compiler<T> {
Compiler {
ctx: ctx,
reader: reader,
partials: partials,
otag: otag,
ctag: ctag,
}
Compiler { ctx, reader, partials, otag, ctag }
}

/// Compiles a template into a series of tokens.
Expand All @@ -59,6 +53,9 @@ impl<T: Iterator<Item = char>> Compiler<T> {
let path =
self.ctx.template_path.join(&(name.clone() + "." + &self.ctx.template_extension));

// This is too complicated to just be
// self.partials.entry(&name).or_insert(...)
#[allow(clippy::map_entry)]
if !self.partials.contains_key(&name) {
// Insert a placeholder so we don't recurse off to infinity.
self.partials.insert(name.to_string(), Vec::new());
Expand Down
2 changes: 1 addition & 1 deletion src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::cell::RefCell;
use std::fmt;

// for bug!
use log::{log, error};
use log::error;

pub enum Data {
Null,
Expand Down
5 changes: 1 addition & 4 deletions src/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,14 @@ use super::{Data, to_data};
/// This type is not intended to be matched exhaustively as new variants
/// may be added in future without a version bump.
#[derive(Debug)]
#[non_exhaustive]
pub enum Error {
NestedOptions,
UnsupportedType,
MissingElements,
KeyIsNotString,
NoDataToEncode,
Message(String),

#[doc(hidden)]
__Nonexhaustive,
}

impl serde::ser::Error for Error {
Expand All @@ -42,7 +40,6 @@ impl fmt::Display for Error {
Error::KeyIsNotString => "key is not a string",
Error::NoDataToEncode => "the encodable type created no data",
Error::Message(ref s) => s,
Error::__Nonexhaustive => unreachable!(),
})
}
}
Expand Down
5 changes: 1 addition & 4 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,14 @@ use encoder;
/// This type is not intended to be matched exhaustively as new variants
/// may be added in future without a version bump.
#[derive(Debug)]
#[non_exhaustive]
pub enum Error {
InvalidStr,
NoFilename,
IncompleteSection,
Io(StdIoError),
Parser(parser::Error),
Encoder(encoder::Error),

#[doc(hidden)]
__Nonexhaustive,
}

pub type Result<T> = StdResult<T, Error>;
Expand All @@ -33,7 +31,6 @@ impl fmt::Display for Error {
Error::Io(ref err) => err.to_string(),
Error::Parser(ref err) => err.to_string(),
Error::Encoder(ref err) => err.to_string(),
Error::__Nonexhaustive => unreachable!(),
})
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ pub fn compile_iter<T: Iterator<Item = char>>(iter: T) -> Result<Template> {
}

/// Compiles a template from a path.
/// returns None if the file cannot be read OR the file is not UTF-8 encoded
/// returns [Error] if the file cannot be read OR the file is not UTF-8 encoded
pub fn compile_path<U: AsRef<Path>>(path: U) -> Result<Template> {
let path = path.as_ref();

match path.file_name() {
Some(filename) => {
let template_dir = path.parent().unwrap_or(Path::new("."));
let template_dir = path.parent().unwrap_or_else(|| Path::new("."));
// FIXME: Should work with OsStrings, this will not use provided extension if
// the extension is not utf8 :(
let extension = path.extension().and_then(|ext| ext.to_str()).unwrap_or("mustache");
Expand Down
15 changes: 6 additions & 9 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::mem;
use std::fmt;

// for bug!
use log::{log, error};
use log::error;

/// `Token` is a section of a compiled mustache string.
#[derive(Clone, Debug, PartialEq)]
Expand All @@ -21,6 +21,7 @@ pub enum Token {
/// This type is not intended to be matched exhaustively as new variants
/// may be added in future without a version bump.
#[derive(Debug, PartialEq)]
#[non_exhaustive]
pub enum Error {
BadClosingTag(char, char),
UnclosedTag,
Expand All @@ -30,9 +31,6 @@ pub enum Error {
EarlySectionClose(String),
MissingSetDelimeterClosingTag,
InvalidSetDelimeterSyntax,

#[doc(hidden)]
__Nonexhaustive,
}

impl StdError for Error { }
Expand All @@ -49,7 +47,6 @@ impl fmt::Display for Error {
Error::EmptyTag => write!(f, "found an empty tag",),
Error::MissingSetDelimeterClosingTag => write!(f, "missing the new closing tag in set delimeter tag"),
Error::InvalidSetDelimeterSyntax => write!(f, "invalid set delimeter tag syntax"),
Error::__Nonexhaustive => unreachable!(),
}
}
}
Expand Down Expand Up @@ -88,7 +85,7 @@ enum ParserState {
impl<'a, T: Iterator<Item = char>> Parser<'a, T> {
pub fn new(reader: &'a mut T, opening_tag: &str, closing_tag: &str) -> Parser<'a, T> {
let mut parser = Parser {
reader: reader,
reader,
ch: None,
lookahead: None,
line: 1,
Expand Down Expand Up @@ -172,7 +169,7 @@ impl<'a, T: Iterator<Item = char>> Parser<'a, T> {
curly_brace_tag = false;
self.state = ParserState::Tag;
} else {
self.tag_position = self.tag_position + 1;
self.tag_position += 1;
}
} else {
// We don't have a tag, so add all the tag parts we've seen
Expand Down Expand Up @@ -523,7 +520,7 @@ impl<'a, T: Iterator<Item = char>> Parser<'a, T> {

fn not_otag(&mut self) {
for (i, ch) in self.opening_tag_chars.iter().enumerate() {
if !(i < self.tag_position) {
if i >= self.tag_position {
break;
}
self.content.push(*ch);
Expand All @@ -532,7 +529,7 @@ impl<'a, T: Iterator<Item = char>> Parser<'a, T> {

fn not_ctag(&mut self) {
for (i, ch) in self.closing_tag_chars.iter().enumerate() {
if !(i < self.tag_position) {
if i >= self.tag_position {
break;
}
self.content.push(*ch);
Expand Down
29 changes: 14 additions & 15 deletions src/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::str;

use compiler::Compiler;
// for bug!
use log::{log, error};
use log::error;
use parser::Token;
use serde::Serialize;

Expand All @@ -22,11 +22,7 @@ pub struct Template {
/// Construct a `Template`. This is not part of the impl of Template so it is
/// not exported outside of mustache.
pub fn new(ctx: Context, tokens: Vec<Token>, partials: HashMap<String, Vec<Token>>) -> Template {
Template {
ctx: ctx,
tokens: tokens,
partials: partials,
}
Template { ctx, tokens, partials }
}

impl Template {
Expand Down Expand Up @@ -71,8 +67,8 @@ struct RenderContext<'a> {
impl<'a> RenderContext<'a> {
fn new(template: &'a Template) -> RenderContext<'a> {
RenderContext {
template: template,
indent: "".to_string(),
template,
indent: String::new(),
line_start: true,
}
}
Expand Down Expand Up @@ -210,6 +206,11 @@ impl<'a> RenderContext<'a> {
self.render(wr, stack, &tokens)?;
}

Data::Bool(val) => {
let s = if val { "true" } else { "false" };
self.write_tracking_newlines(wr, s)?;
}

ref value => {
bug!("render_utag: unexpected value {:?}", value);
}
Expand Down Expand Up @@ -238,6 +239,7 @@ impl<'a> RenderContext<'a> {
self.render(wr, stack, children)
}

#[allow(clippy::too_many_arguments)]
fn render_section<W: Write>(&mut self,
wr: &mut W,
stack: &mut Vec<&Data>,
Expand Down Expand Up @@ -340,14 +342,11 @@ impl<'a> RenderContext<'a> {
let mut value = None;

for data in stack.iter().rev() {
match **data {
Data::Map(ref m) => {
if let Some(v) = m.get(&path[0]) {
value = Some(v);
break;
}
if let Data::Map(m) = *data {
if let Some(v) = m.get(&path[0]) {
value = Some(v);
break;
}
_ => { /* continue searching the stack */ },
}
}

Expand Down
Loading