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 Jun 15, 2017
1 parent 9f9b62e commit 6010f6a
Show file tree
Hide file tree
Showing 22 changed files with 1,324 additions and 917 deletions.
17 changes: 11 additions & 6 deletions benches/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,13 @@ fn make_data() -> BTreeMap<String, Json> {

let mut teams = Vec::new();

for v in vec![("Jiangsu", 43u16),
("Beijing", 27u16),
("Guangzhou", 22u16),
("Shandong", 12u16)]
.iter() {
for v in vec![
("Jiangsu", 43u16),
("Beijing", 27u16),
("Guangzhou", 22u16),
("Shandong", 12u16),
].iter()
{
let (name, score) = *v;
let mut t = BTreeMap::new();
t.insert("name".to_string(), to_json(&name));
Expand All @@ -56,7 +58,10 @@ 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
137 changes: 75 additions & 62 deletions examples/decorator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,46 +6,50 @@ extern crate serde_json;
extern crate serde_derive;
use serde_json::value::{Value as Json, Map};

use handlebars::{Handlebars, RenderError, RenderContext, Helper, JsonRender, Decorator,
to_json};
use handlebars::{Handlebars, RenderError, RenderContext, Helper, JsonRender, Decorator, to_json};

// default format helper
fn format_helper(h: &Helper, _: &Handlebars, rc: &mut RenderContext) -> Result<(), RenderError> {
// 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!("{} pts", param.value().render());
try!(rc.writer.write(rendered.into_bytes().as_ref()));
Ok(())
}

fn format_decorator(d: &Decorator,
_: &Handlebars,
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| {
// 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 rendered = format!("{} {}", param.value().render(), suffix);
try!(rc.writer.write(rendered.into_bytes().as_ref()));
Ok(())
}));
fn format_decorator(
d: &Decorator,
_: &Handlebars,
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| {
// 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 rendered = format!("{} {}", param.value().render(), suffix);
try!(rc.writer.write(rendered.into_bytes().as_ref()));
Ok(())
}),
);
Ok(())
}

// another custom helper
fn rank_helper(h: &Helper, _: &Handlebars, rc: &mut RenderContext) -> Result<(), RenderError> {
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)
.and_then(|v| v.value().as_array())
.ok_or(RenderError::new("Param 1 with array type is required for rank helper")));
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).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();
if rank == 0 {
try!(rc.writer.write("champion".as_bytes()));
Expand All @@ -72,38 +76,40 @@ pub fn make_data() -> Map<String, Json> {

data.insert("year".to_string(), to_json(&"2015".to_owned()));

let teams = vec![Team {
name: "Jiangsu Suning".to_string(),
pts: 43u16,
},
Team {
name: "Shanghai SIPG".to_string(),
pts: 39u16,
},
Team {
name: "Hebei CFFC".to_string(),
pts: 27u16,
},
Team {
name: "Guangzhou Evergrand".to_string(),
pts: 22u16,
},
Team {
name: "Shandong Luneng".to_string(),
pts: 12u16,
},
Team {
name: "Beijing Guoan".to_string(),
pts: 7u16,
},
Team {
name: "Hangzhou Greentown".to_string(),
pts: 7u16,
},
Team {
name: "Shanghai Shenhua".to_string(),
pts: 4u16,
}];
let teams = vec![
Team {
name: "Jiangsu Suning".to_string(),
pts: 43u16,
},
Team {
name: "Shanghai SIPG".to_string(),
pts: 39u16,
},
Team {
name: "Hebei CFFC".to_string(),
pts: 27u16,
},
Team {
name: "Guangzhou Evergrand".to_string(),
pts: 22u16,
},
Team {
name: "Shandong Luneng".to_string(),
pts: 12u16,
},
Team {
name: "Beijing Guoan".to_string(),
pts: 7u16,
},
Team {
name: "Hangzhou Greentown".to_string(),
pts: 7u16,
},
Team {
name: "Shanghai Shenhua".to_string(),
pts: 4u16,
},
];

data.insert("teams".to_string(), to_json(&teams));
data.insert("engine".to_string(), to_json(&TYPES.to_owned()));
Expand All @@ -117,8 +123,11 @@ 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 All @@ -129,6 +138,10 @@ fn main() {

// make data and render it
let data = make_data();
println!("{}",
handlebars.render("table", &data).unwrap_or_else(|e| format!("{}", e)));
println!(
"{}",
handlebars.render("table", &data).unwrap_or_else(
|e| format!("{}", e),
)
);
}
96 changes: 53 additions & 43 deletions examples/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,21 @@ use serde_json::value::{Value as Json, Map};
use handlebars::{Handlebars, RenderError, RenderContext, Helper, JsonRender, to_json};

fn format_helper(h: &Helper, _: &Handlebars, rc: &mut RenderContext) -> Result<(), RenderError> {
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!("{} pts", param.value().render());
try!(rc.writer.write(rendered.into_bytes().as_ref()));
Ok(())
}

fn rank_helper(h: &Helper, _: &Handlebars, rc: &mut RenderContext) -> Result<(), RenderError> {
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)
.and_then(|v| v.value().as_array())
.ok_or(RenderError::new("Param 1 with array type is required for rank helper")));
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).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();
if rank == 0 {
try!(rc.writer.write("champion".as_bytes()));
Expand All @@ -50,38 +51,40 @@ pub fn make_data() -> Map<String, Json> {

data.insert("year".to_string(), to_json(&"2015".to_owned()));

let teams = vec![Team {
name: "Jiangsu Suning".to_string(),
pts: 43u16,
},
Team {
name: "Shanghai SIPG".to_string(),
pts: 39u16,
},
Team {
name: "Hebei CFFC".to_string(),
pts: 27u16,
},
Team {
name: "Guangzhou Evergrand".to_string(),
pts: 22u16,
},
Team {
name: "Shandong Luneng".to_string(),
pts: 12u16,
},
Team {
name: "Beijing Guoan".to_string(),
pts: 7u16,
},
Team {
name: "Hangzhou Greentown".to_string(),
pts: 7u16,
},
Team {
name: "Shanghai Shenhua".to_string(),
pts: 4u16,
}];
let teams = vec![
Team {
name: "Jiangsu Suning".to_string(),
pts: 43u16,
},
Team {
name: "Shanghai SIPG".to_string(),
pts: 39u16,
},
Team {
name: "Hebei CFFC".to_string(),
pts: 27u16,
},
Team {
name: "Guangzhou Evergrand".to_string(),
pts: 22u16,
},
Team {
name: "Shandong Luneng".to_string(),
pts: 12u16,
},
Team {
name: "Beijing Guoan".to_string(),
pts: 7u16,
},
Team {
name: "Hangzhou Greentown".to_string(),
pts: 7u16,
},
Team {
name: "Shanghai Shenhua".to_string(),
pts: 4u16,
},
];

data.insert("teams".to_string(), to_json(&teams));
data.insert("engine".to_string(), to_json(&TYPES.to_owned()));
Expand All @@ -102,13 +105,20 @@ 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));
// handlebars.register_helper("format", Box::new(FORMAT_HELPER));

let data = make_data();
println!("{}",
handlebars.render("table", &data).unwrap_or_else(|e| format!("{}", e)));
println!(
"{}",
handlebars.render("table", &data).unwrap_or_else(
|e| format!("{}", e),
)
);
}
26 changes: 16 additions & 10 deletions examples/partials.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,25 +24,31 @@ fn main() {
.ok()
.unwrap();

let data0 = btreemap! {
let data0 =
btreemap! {
"title".to_string() => "example 0".to_string(),
"parent".to_string() => "base0".to_string()
};
let data1 = btreemap! {
let data1 =
btreemap! {
"title".to_string() => "example 1".to_string(),
"parent".to_string() => "base1".to_string()
};

println!("Page 0");
println!("{}",
handlebars
.render("template", &data0)
.unwrap_or_else(|e| format!("{}", e)));
println!(
"{}",
handlebars.render("template", &data0).unwrap_or_else(
|e| format!("{}", e),
)
);
println!("=======================================================");

println!("Page 1");
println!("{}",
handlebars
.render("template", &data1)
.unwrap_or_else(|e| format!("{}", e)));
println!(
"{}",
handlebars.render("template", &data1).unwrap_or_else(
|e| format!("{}", e),
)
);
}
Loading

0 comments on commit 6010f6a

Please sign in to comment.