Skip to content

Commit

Permalink
Stop exporting the modules builder, encoder, and parser
Browse files Browse the repository at this point in the history
  • Loading branch information
erickt committed Jun 24, 2017
1 parent 7ee5f45 commit d7644ce
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 134 deletions.
9 changes: 5 additions & 4 deletions src/compiler.rs
Expand Up @@ -3,7 +3,7 @@ use std::io::ErrorKind::NotFound;
use std::io::Read;
use std::fs::File;

use parser_internals::{Parser, Token};
use parser::{Parser, Token};
use super::Context;

use Result;
Expand Down Expand Up @@ -99,11 +99,12 @@ impl<T: Iterator<Item = char>> Compiler<T> {

#[cfg(test)]
mod tests {
use parser_internals::Token;
use super::Compiler;
use super::super::Context;
use std::path::PathBuf;

use parser::Token;
use compiler::Compiler;
use context::Context;

fn compile_str(template: &str) -> Vec<Token> {
let ctx = Context::new(PathBuf::from("."));
let (tokens, _) = Compiler::new(ctx, template.chars())
Expand Down
5 changes: 4 additions & 1 deletion src/error.rs
@@ -1,6 +1,7 @@
use std::error::Error as StdError;
use std::io::Error as StdIoError;
use std::fmt;
use std::io::Error as StdIoError;
use std::result;

use parser;
use encoder;
Expand All @@ -21,6 +22,8 @@ pub enum Error {
__Nonexhaustive,
}

pub type Result<T> = result::Result<T, Error>;

impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.description().fmt(f)
Expand Down
135 changes: 15 additions & 120 deletions src/lib.rs
@@ -1,50 +1,31 @@
extern crate log;
extern crate serde;

use std::cell::RefCell;
use std::collections::HashMap;
use std::fmt;
use std::fs::File;
use std::io::Read;
use std::str;
use std::path::{PathBuf, Path};
use std::result;

pub use builder::{MapBuilder, VecBuilder};
pub use encoder::Encoder;
pub use encoder::Error as EncoderError;
pub use error::Error;
pub use parser::Error as ParserError;
pub use template::Template;

#[macro_use]
mod macros;

pub mod builder;
pub mod encoder;


// FIXME: When pub(crate) lands then this can be made a lot less awkward.
// Alternatively, decide on a decent part of the parser api to consider stable.
pub mod parser {
pub use parser_internals::Error;
}

#[path = "parser.rs"]
mod parser_internals;

mod builder;
mod compiler;
mod template;
mod context;
mod data;
mod encoder;
mod error;
mod parser;
mod template;

pub enum Data {
Null,
String(String),
Bool(bool),
Vec(Vec<Data>),
Map(HashMap<String, Data>),
Fun(RefCell<Box<FnMut(String) -> String + Send>>),
}
pub use builder::{MapBuilder, VecBuilder};
pub use context::Context;
pub use data::Data;
pub use encoder::Encoder;
pub use encoder::Error as EncoderError;
pub use encoder::{SerializeVec, SerializeTupleVariant, SerializeMap, SerializeStructVariant};
pub use error::{Error, Result};
pub use parser::Error as ParserError;
pub use template::Template;

pub fn to_data<T>(value: T) -> result::Result<Data, encoder::Error>
where
Expand All @@ -53,92 +34,6 @@ where
value.serialize(Encoder)
}

pub type Result<T> = result::Result<T, Error>;

impl PartialEq for Data {
#[inline]
fn eq(&self, other: &Data) -> bool {
match (self, other) {
(&Data::Null, &Data::Null) => true,
(&Data::String(ref v0), &Data::String(ref v1)) => v0 == v1,
(&Data::Bool(ref v0), &Data::Bool(ref v1)) => v0 == v1,
(&Data::Vec(ref v0), &Data::Vec(ref v1)) => v0 == v1,
(&Data::Map(ref v0), &Data::Map(ref v1)) => v0 == v1,
(&Data::Fun(_), &Data::Fun(_)) => bug!("Cannot compare closures"),
(_, _) => false,
}
}
}

impl fmt::Debug for Data {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Data::Null => write!(f, "Null"),
Data::String(ref v) => write!(f, "StrVal({})", v),
Data::Bool(v) => write!(f, "Bool({:?})", v),
Data::Vec(ref v) => write!(f, "VecVal({:?})", v),
Data::Map(ref v) => write!(f, "Map({:?})", v),
Data::Fun(_) => write!(f, "Fun(...)"),
}
}
}

/// Represents the shared metadata needed to compile and render a mustache
/// template.
#[derive(Clone)]
pub struct Context {
pub template_path: PathBuf,
pub template_extension: String,
}

impl fmt::Debug for Context {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f,
"Context {{ template_path: {:?}, template_extension: {} }}",
&*self.template_path,
self.template_extension)
}
}

impl Context {
/// Configures a mustache context the specified path to the templates.
pub fn new(path: PathBuf) -> Context {
Context {
template_path: path,
template_extension: "mustache".to_string(),
}
}

/// Compiles a template from a string
pub fn compile<IT: Iterator<Item = char>>(&self, reader: IT) -> Result<Template> {
let compiler = compiler::Compiler::new(self.clone(), reader);
let (tokens, partials) = try!(compiler.compile());

Ok(template::new(self.clone(), tokens, partials))
}

/// Compiles a template from a path.
pub fn compile_path<U: AsRef<Path>>(&self, path: U) -> Result<Template> {
// FIXME(#6164): This should use the file decoding tools when they are
// written. For now we'll just read the file and treat it as UTF-8file.
let mut path = self.template_path.join(path.as_ref());
path.set_extension(&self.template_extension);
let mut s = vec![];
let mut file = try!(File::open(&path));
try!(file.read_to_end(&mut s));

// TODO: maybe allow UTF-16 as well?
let template = match str::from_utf8(&*s) {
Ok(string) => string,
_ => {
return Err(Error::InvalidStr);
}
};

self.compile(template.chars())
}
}

/// Compiles a template from an `Iterator<char>`.
pub fn compile_iter<T: Iterator<Item = char>>(iter: T) -> Result<Template> {
Context::new(PathBuf::from(".")).compile(iter)
Expand Down
17 changes: 8 additions & 9 deletions src/template.rs
Expand Up @@ -5,8 +5,7 @@ use std::str;
use serde::Serialize;

use compiler::Compiler;
use parser_internals::Token;
use parser_internals::Token::*;
use parser::Token;

use super::{Context, Data, Result, to_data};

Expand Down Expand Up @@ -72,25 +71,25 @@ impl<'a> RenderContext<'a> {

fn render_token<W: Write>(&mut self, wr: &mut W, stack: &mut Vec<&Data>, token: &Token) -> Result<()> {
match *token {
Text(ref value) => {
Token::Text(ref value) => {
self.render_text(wr, value)
}
EscapedTag(ref path, _) => {
Token::EscapedTag(ref path, _) => {
self.render_etag(wr, stack, path)
}
UnescapedTag(ref path, _) => {
Token::UnescapedTag(ref path, _) => {
self.render_utag(wr, stack, path)
}
Section(ref path, true, ref children, _, _, _, _, _) => {
Token::Section(ref path, true, ref children, _, _, _, _, _) => {
self.render_inverted_section(wr, stack, path, children)
}
Section(ref path, false, ref children, ref otag, _, ref src, _, ref ctag) => {
Token::Section(ref path, false, ref children, ref otag, _, ref src, _, ref ctag) => {
self.render_section(wr, stack, path, children, src, otag, ctag)
}
Partial(ref name, ref indent, _) => {
Token::Partial(ref name, ref indent, _) => {
self.render_partial(wr, stack, name, indent)
}
IncompleteSection(..) => {
Token::IncompleteSection(..) => {
bug!("render_token should not encounter IncompleteSections")
}
}
Expand Down

0 comments on commit d7644ce

Please sign in to comment.