1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#![crate_name = "mustache"]
#![crate_type = "dylib"]
#![crate_type = "rlib"]
extern crate rustc_serialize;
extern crate log;
#[cfg(test)]
extern crate tempdir;
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::Result as StdResult;
pub use self::Data::*;
pub use builder::{MapBuilder, VecBuilder};
pub use encoder::{Encoder, EncoderResult};
pub use template::Template;
pub use error::Error;
#[macro_use]
mod macros;
pub mod builder;
pub mod encoder;
pub mod parser {
pub use parser_internals::Error;
}
#[path = "parser.rs"]
mod parser_internals;
mod compiler;
mod template;
mod error;
pub enum Data {
OptVal(Option<Box<Data>>),
StrVal(String),
Bool(bool),
VecVal(Vec<Data>),
Map(HashMap<String, Data>),
Fun(RefCell<Box<FnMut(String) -> String + Send>>),
}
pub type Result<T = ()> = StdResult<T, Error>;
impl PartialEq for Data {
#[inline]
fn eq(&self, other: &Data) -> bool {
match (self, other) {
(&OptVal(ref v0), &OptVal(ref v1)) => v0 == v1,
(&StrVal(ref v0), &StrVal(ref v1)) => v0 == v1,
(&Bool(ref v0), &Bool(ref v1)) => v0 == v1,
(&VecVal(ref v0), &VecVal(ref v1)) => v0 == v1,
(&Map(ref v0), &Map(ref v1)) => v0 == v1,
(&Fun(_), &Fun(_)) => bug!("Cannot compare closures"),
(_, _) => false,
}
}
}
impl fmt::Debug for Data {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
OptVal(ref v) => write!(f, "OptVal({:?})", v),
StrVal(ref v) => write!(f, "StrVal({})", v),
Bool(v) => write!(f, "Bool({:?})", v),
VecVal(ref v) => write!(f, "VecVal({:?})", v),
Map(ref v) => write!(f, "Map({:?})", v),
Fun(_) => write!(f, "Fun(...)"),
}
}
}
#[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 {
pub fn new(path: PathBuf) -> Context {
Context {
template_path: path,
template_extension: "mustache".to_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))
}
pub fn compile_path<U: AsRef<Path>>(&self, path: U) -> Result<Template> {
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));
let template = match str::from_utf8(&*s) {
Ok(string) => string,
_ => {
return Err(Error::InvalidStr);
}
};
self.compile(template.chars())
}
}
pub fn compile_iter<T: Iterator<Item = char>>(iter: T) -> Result<Template> {
Context::new(PathBuf::from(".")).compile(iter)
}
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 extension = path.extension().and_then(|ext| ext.to_str()).unwrap_or("mustache");
let context = Context {
template_path: template_dir.to_path_buf(),
template_extension: extension.to_string(),
};
context.compile_path(filename)
}
None => Err(Error::NoFilename),
}
}
pub fn compile_str(template: &str) -> Result<Template> {
compile_iter(template.chars())
}