Skip to content

Commit

Permalink
(style) update rustfmt
Browse files Browse the repository at this point in the history
Signed-off-by: Ning Sun <sunng@about.me>
  • Loading branch information
sunng87 committed Mar 23, 2017
1 parent 8bb3d70 commit db5cc8d
Show file tree
Hide file tree
Showing 21 changed files with 309 additions and 285 deletions.
6 changes: 2 additions & 4 deletions benches/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ fn make_data() -> BTreeMap<String, Json> {
("Beijing", 27u16),
("Guangzhou", 22u16),
("Shandong", 12u16)]
.iter() {
.iter() {
let (name, score) = *v;
let mut t = BTreeMap::new();
t.insert("name".to_string(), name.to_json());
Expand All @@ -55,9 +55,7 @@ fn parse_template(b: &mut test::Bencher) {
#[bench]
fn render_template(b: &mut test::Bencher) {
let mut handlebars = Handlebars::new();
handlebars.register_template_string("table", SOURCE)
.ok()
.expect("Invalid template format");
handlebars.register_template_string("table", SOURCE).ok().expect("Invalid template format");

let data = make_data();
b.iter(|| handlebars.render("table", &data).ok().unwrap())
Expand Down
15 changes: 10 additions & 5 deletions examples/decorator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,13 @@ fn format_decorator(d: &Decorator,
rc: &mut RenderContext)
-> Result<(), RenderError> {
let suffix = d.param(0).map(|v| v.value().render()).unwrap_or("".to_owned());
rc.register_local_helper("format", Box::new(move |h: &Helper, _: &Handlebars, rc: &mut RenderContext|{
rc.register_local_helper("format",
Box::new(move |h: &Helper,
_: &Handlebars,
rc: &mut RenderContext| {
// get parameter from helper or throw an error
let param = try!(h.param(0).ok_or(RenderError::new("Param 0 is required for format helper.")));
let param =
try!(h.param(0).ok_or(RenderError::new("Param 0 is required for format helper.")));
let rendered = format!("{} {}", param.value().render(), suffix);
try!(rc.writer.write(rendered.into_bytes().as_ref()));
Ok(())
Expand All @@ -59,7 +63,8 @@ fn rank_helper(h: &Helper, _: &Handlebars, rc: &mut RenderContext) -> Result<(),
let rank = try!(h.param(0)
.and_then(|v| v.value().as_u64())
.ok_or(RenderError::new("Param 0 with u64 type is required for rank helper."))) as usize;
let teams = try!(h.param(1)
let teams =
try!(h.param(1)
.and_then(|v| v.value().as_array())
.ok_or(RenderError::new("Param 1 with array type is required for rank helper")));
let total = teams.len();
Expand Down Expand Up @@ -137,8 +142,8 @@ fn main() {

// register template from a file and assign a name to it
// deal with errors
if let Err(e) = handlebars.register_template_file("table",
"./examples/decorator/template.hbs") {
if let Err(e) =
handlebars.register_template_file("table", "./examples/decorator/template.hbs") {
panic!("{}", e);
}

Expand Down
7 changes: 3 additions & 4 deletions examples/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ fn rank_helper(h: &Helper, _: &Handlebars, rc: &mut RenderContext) -> Result<(),
let rank = try!(h.param(0)
.and_then(|v| v.value().as_u64())
.ok_or(RenderError::new("Param 0 with u64 type is required for rank helper."))) as usize;
let teams = try!(h.param(1)
let teams =
try!(h.param(1)
.and_then(|v| v.value().as_array())
.ok_or(RenderError::new("Param 1 with array type is required for rank helper")));
let total = teams.len();
Expand Down Expand Up @@ -125,9 +126,7 @@ fn main() {
println!("{}", e);
}

handlebars.register_template_file("table", "./examples/error/template.hbs")
.ok()
.unwrap();
handlebars.register_template_file("table", "./examples/error/template.hbs").ok().unwrap();

handlebars.register_helper("format", Box::new(format_helper));
handlebars.register_helper("ranking_label", Box::new(rank_helper));
Expand Down
24 changes: 12 additions & 12 deletions examples/partials.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ fn main() {

handlebars.register_template_file("template",
&Path::new("./examples/partials_legacy/template2.hbs"))
.ok()
.unwrap();
.ok()
.unwrap();
handlebars.register_template_file("base0", &Path::new("./examples/partials_legacy/base0.hbs"))
.ok()
.unwrap();
.ok()
.unwrap();
handlebars.register_template_file("base1", &Path::new("./examples/partials_legacy/base1.hbs"))
.ok()
.unwrap();
.ok()
.unwrap();

let data0 = btreemap! {
"title".to_string() => "example 0".to_string(),
Expand Down Expand Up @@ -53,15 +53,15 @@ fn main() {
let mut handlebars = Handlebars::new();

handlebars.register_template_file("template", &Path::new("./examples/partials/template2.hbs"))
.ok()
.unwrap();
.ok()
.unwrap();

handlebars.register_template_file("base0", &Path::new("./examples/partials/base0.hbs"))
.ok()
.unwrap();
.ok()
.unwrap();
handlebars.register_template_file("base1", &Path::new("./examples/partials/base1.hbs"))
.ok()
.unwrap();
.ok()
.unwrap();

let data0 = btreemap! {
"title".to_string() => "example 0".to_string(),
Expand Down
6 changes: 2 additions & 4 deletions examples/render-cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ fn usage() -> ! {
writeln!(&mut io::stderr(),
"{}",
r#"Usage: ./render-cli template.hbs '{"json": "data"}'"#)
.ok();
.ok();
process::exit(1);
}

Expand All @@ -44,9 +44,7 @@ fn main() {

let mut handlebars = Handlebars::new();

handlebars.register_template_file(&filename, &filename)
.ok()
.unwrap();
handlebars.register_template_file(&filename, &filename).ok().unwrap();
match handlebars.render(&filename, &data) {
Ok(data) => {
println!("{}", data);
Expand Down
3 changes: 2 additions & 1 deletion examples/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ fn rank_helper(h: &Helper, _: &Handlebars, rc: &mut RenderContext) -> Result<(),
let rank = try!(h.param(0)
.and_then(|v| v.value().as_u64())
.ok_or(RenderError::new("Param 0 with u64 type is required for rank helper."))) as usize;
let teams = try!(h.param(1)
let teams =
try!(h.param(1)
.and_then(|v| v.value().as_array())
.ok_or(RenderError::new("Param 1 with array type is required for rank helper")));
let total = teams.len();
Expand Down
3 changes: 2 additions & 1 deletion examples/render_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ fn rank_helper(h: &Helper, _: &Handlebars, rc: &mut RenderContext) -> Result<(),
let rank = try!(h.param(0)
.and_then(|v| v.value().as_u64())
.ok_or(RenderError::new("Param 0 with u64 type is required for rank helper."))) as usize;
let teams = try!(h.param(1)
let teams =
try!(h.param(1)
.and_then(|v| v.value().as_array())
.ok_or(RenderError::new("Param 1 with array type is required for rank helper")));
let total = teams.len();
Expand Down
8 changes: 5 additions & 3 deletions src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ fn parse_json_visitor_inner<'a>(path_stack: &mut VecDeque<&'a str>, path: &'a st
Rule::path_up => {
path_stack.pop_back();
}
Rule::path_id | Rule::path_raw_id | Rule::path_num_id => {
Rule::path_id |
Rule::path_raw_id |
Rule::path_num_id => {
let id = &path[seg.start..seg.end];
path_stack.push_back(id);
}
Expand Down Expand Up @@ -159,8 +161,8 @@ impl Context {
data = match *data {
Json::Array(ref l) => {
p.parse::<usize>()
.and_then(|idx_u| Ok(l.get(idx_u).unwrap_or(&DEFAULT_VALUE)))
.unwrap_or(&DEFAULT_VALUE)
.and_then(|idx_u| Ok(l.get(idx_u).unwrap_or(&DEFAULT_VALUE)))
.unwrap_or(&DEFAULT_VALUE)
}
Json::Object(ref m) => m.get(*p).unwrap_or(&DEFAULT_VALUE),
_ => &DEFAULT_VALUE,
Expand Down
24 changes: 12 additions & 12 deletions src/directives/inline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,31 @@ pub struct InlineDirective;
#[cfg(all(feature = "rustc_ser_type", not(feature = "serde_type")))]
fn get_name<'a>(d: &'a Directive) -> Result<&'a str, RenderError> {
d.param(0)
.ok_or_else(|| RenderError::new("Param required for directive \"inline\""))
.and_then(|v| {
v.value()
.ok_or_else(|| RenderError::new("Param required for directive \"inline\""))
.and_then(|v| {
v.value()
.as_string()
.ok_or_else(|| RenderError::new("inline name must be string"))
})
})
}

#[cfg(feature = "serde_type")]
fn get_name<'a>(d: &'a Directive) -> Result<&'a str, RenderError> {
d.param(0)
.ok_or_else(|| RenderError::new("Param required for directive \"inline\""))
.and_then(|v| {
v.value()
.ok_or_else(|| RenderError::new("Param required for directive \"inline\""))
.and_then(|v| {
v.value()
.as_str()
.ok_or_else(|| RenderError::new("inline name must be string"))
})
})
}

impl DirectiveDef for InlineDirective {
fn call(&self, d: &Directive, _: &Registry, rc: &mut RenderContext) -> Result<(), RenderError> {
let name = try!(get_name(d));

let template = try!(d.template()
.ok_or_else(|| RenderError::new("inline should have a block")));
let template =
try!(d.template().ok_or_else(|| RenderError::new("inline should have a block")));


rc.set_partial(name.to_owned(), template.clone());
Expand All @@ -56,8 +56,8 @@ mod test {
let t0 =
Template::compile("{{#*inline \"hello\"}}the hello world inline partial.{{/inline}}"
.to_string())
.ok()
.unwrap();
.ok()
.unwrap();

let hbs = Registry::new();

Expand Down
Loading

0 comments on commit db5cc8d

Please sign in to comment.