Skip to content

Commit

Permalink
(style) rustfmt
Browse files Browse the repository at this point in the history
  • Loading branch information
sunng87 committed Dec 13, 2018
1 parent 0b6adb7 commit c6f78bc
Show file tree
Hide file tree
Showing 16 changed files with 167 additions and 230 deletions.
3 changes: 2 additions & 1 deletion benches/bench.rs
Expand Up @@ -39,7 +39,8 @@ fn make_data() -> BTreeMap<String, Json> {
("Beijing", 27u16),
("Guangzhou", 22u16),
("Shandong", 12u16),
].iter()
]
.iter()
{
let (name, score) = *v;
let mut t = BTreeMap::new();
Expand Down
3 changes: 2 additions & 1 deletion examples/render-cli.rs
Expand Up @@ -17,7 +17,8 @@ fn usage() -> ! {
&mut io::stderr(),
"{}",
r#"Usage: ./render-cli template.hbs '{"json": "data"}'"#
).ok();
)
.ok();
process::exit(1);
}

Expand Down
1 change: 0 additions & 1 deletion rustfmt.toml
@@ -1,2 +1 @@
format_strings = false
error_on_line_overflow_comments = false
6 changes: 3 additions & 3 deletions src/context.rs
Expand Up @@ -163,9 +163,9 @@ impl Context {
#[cfg(test)]
mod test {
use crate::context::{self, Context};
use crate::value::{self, JsonRender};
use serde_json::value::Map;
use std::collections::VecDeque;
use crate::value::{self, JsonRender};

#[derive(Serialize)]
struct Address {
Expand Down Expand Up @@ -289,7 +289,7 @@ mod test {
fn test_merge_json() {
let map = json!({ "age": 4 });
let s = "hello".to_owned();
let hash = btreemap!{
let hash = btreemap! {
"tag".to_owned() => value::to_json("h1")
};

Expand Down Expand Up @@ -332,7 +332,7 @@ mod test {

#[test]
fn test_key_name_with_this() {
let m = btreemap!{
let m = btreemap! {
"this_name".to_string() => "the_value".to_string()
};
let ctx = Context::wraps(&m).unwrap();
Expand Down
5 changes: 3 additions & 2 deletions src/directives/inline.rs
Expand Up @@ -49,8 +49,9 @@ mod test {
fn test_inline() {
let t0 = Template::compile(
"{{#*inline \"hello\"}}the hello world inline partial.{{/inline}}".to_string(),
).ok()
.unwrap();
)
.ok()
.unwrap();

let hbs = Registry::new();

Expand Down
8 changes: 6 additions & 2 deletions src/directives/mod.rs
Expand Up @@ -70,8 +70,12 @@ pub trait DirectiveDef: Send + Sync {
impl<
F: Send
+ Sync
+ for<'reg, 'rc> Fn(&Directive<'reg, 'rc>, &'reg Registry, &'rc Context, &mut RenderContext)
-> DirectiveResult,
+ for<'reg, 'rc> Fn(
&Directive<'reg, 'rc>,
&'reg Registry,
&'rc Context,
&mut RenderContext,
) -> DirectiveResult,
> DirectiveDef for F
{
fn call<'reg: 'rc, 'rc>(
Expand Down
3 changes: 2 additions & 1 deletion src/grammar.rs
Expand Up @@ -32,7 +32,8 @@ macro_rules! assert_not_rule {
.last()
.unwrap()
.into_span()
.end() != $in.len()
.end()
!= $in.len()
);
};
}
Expand Down
119 changes: 47 additions & 72 deletions src/helpers/helper_each.rs
Expand Up @@ -149,22 +149,15 @@ mod test {
#[test]
fn test_each() {
let mut handlebars = Registry::new();
assert!(
handlebars
.register_template_string(
"t0",
"{{#each this}}{{@first}}|{{@last}}|{{@index}}:{{this}}|{{/each}}"
)
.is_ok()
);
assert!(
handlebars
.register_template_string(
"t1",
"{{#each this}}{{@first}}|{{@key}}:{{this}}|{{/each}}"
)
.is_ok()
);
assert!(handlebars
.register_template_string(
"t0",
"{{#each this}}{{@first}}|{{@last}}|{{@index}}:{{this}}|{{/each}}"
)
.is_ok());
assert!(handlebars
.register_template_string("t1", "{{#each this}}{{@first}}|{{@key}}:{{this}}|{{/each}}")
.is_ok());

let r0 = handlebars.render("t0", &vec![1u16, 2u16, 3u16]);
assert_eq!(
Expand All @@ -190,11 +183,9 @@ mod test {
// previously, to access the parent in an each block,
// a user would need to specify ../../b, as the path
// that is computed includes the array index: ./a.c.[0]
assert!(
handlebars
.register_template_string("t0", "{{#each a.c}} d={{d}} b={{../a.a}} {{/each}}")
.is_ok()
);
assert!(handlebars
.register_template_string("t0", "{{#each a.c}} d={{d}} b={{../a.a}} {{/each}}")
.is_ok());

let r1 = handlebars.render("t0", &data);
assert_eq!(r1.ok().unwrap(), " d=100 b=99 d=200 b=99 ".to_string());
Expand All @@ -206,14 +197,12 @@ mod test {

let data = Json::from_str(json_str).unwrap();
let mut handlebars = Registry::new();
assert!(
handlebars
.register_template_string(
"t0",
"{{#each a}}{{#each b}}{{d}}:{{../c}}{{/each}}{{/each}}"
)
.is_ok()
);
assert!(handlebars
.register_template_string(
"t0",
"{{#each a}}{{#each b}}{{d}}:{{../c}}{{/each}}{{/each}}"
)
.is_ok());

let r1 = handlebars.render("t0", &data);
assert_eq!(r1.ok().unwrap(), "100:200".to_string());
Expand All @@ -226,14 +215,12 @@ mod test {
let data = Json::from_str(json_str).unwrap();

let mut handlebars = Registry::new();
assert!(
handlebars
.register_template_string(
"t0",
"{{#each b}}{{#if ../a}}{{#each this}}{{this}}{{/each}}{{/if}}{{/each}}"
)
.is_ok()
);
assert!(handlebars
.register_template_string(
"t0",
"{{#each b}}{{#if ../a}}{{#each this}}{{this}}{{/each}}{{/if}}{{/each}}"
)
.is_ok());

let r1 = handlebars.render("t0", &data);
assert_eq!(r1.ok().unwrap(), "12345".to_string());
Expand All @@ -242,11 +229,9 @@ mod test {
#[test]
fn test_nested_array() {
let mut handlebars = Registry::new();
assert!(
handlebars
.register_template_string("t0", "{{#each this.[0]}}{{this}}{{/each}}")
.is_ok()
);
assert!(handlebars
.register_template_string("t0", "{{#each this.[0]}}{{this}}{{/each}}")
.is_ok());

let r0 = handlebars.render("t0", &(vec![vec![1, 2, 3]]));

Expand All @@ -256,11 +241,9 @@ mod test {
#[test]
fn test_empty_key() {
let mut handlebars = Registry::new();
assert!(
handlebars
.register_template_string("t0", "{{#each this}}{{@key}}-{{value}}\n{{/each}}")
.is_ok()
);
assert!(handlebars
.register_template_string("t0", "{{#each this}}{{@key}}-{{value}}\n{{/each}}")
.is_ok());

let r0 = handlebars
.render(
Expand Down Expand Up @@ -291,18 +274,16 @@ mod test {
#[test]
fn test_each_else() {
let mut handlebars = Registry::new();
assert!(
handlebars
.register_template_string("t0", "{{#each a}}1{{else}}empty{{/each}}")
.is_ok()
);
assert!(handlebars
.register_template_string("t0", "{{#each a}}1{{else}}empty{{/each}}")
.is_ok());
let m1 = btreemap! {
"a".to_string() => Vec::<String>::new(),
};
let r0 = handlebars.render("t0", &m1).unwrap();
assert_eq!(r0, "empty");

let m2 = btreemap!{
let m2 = btreemap! {
"b".to_string() => Vec::<String>::new()
};
let r1 = handlebars.render("t0", &m2).unwrap();
Expand All @@ -312,11 +293,9 @@ mod test {
#[test]
fn test_block_param() {
let mut handlebars = Registry::new();
assert!(
handlebars
.register_template_string("t0", "{{#each a as |i|}}{{i}}{{/each}}")
.is_ok()
);
assert!(handlebars
.register_template_string("t0", "{{#each a as |i|}}{{i}}{{/each}}")
.is_ok());
let m1 = btreemap! {
"a".to_string() => vec![1,2,3,4,5]
};
Expand All @@ -332,7 +311,7 @@ mod test {
{{/each}}";
assert!(handlebars.register_template_string("t0", template).is_ok());

let m = btreemap!{
let m = btreemap! {
"ftp".to_string() => 21,
"http".to_string() => 80
};
Expand All @@ -343,14 +322,12 @@ mod test {
#[test]
fn test_nested_each_with_path_ups() {
let mut handlebars = Registry::new();
assert!(
handlebars
.register_template_string(
"t0",
"{{#each a.b}}{{#each c}}{{../../d}}{{/each}}{{/each}}"
)
.is_ok()
);
assert!(handlebars
.register_template_string(
"t0",
"{{#each a.b}}{{#each c}}{{../../d}}{{/each}}{{/each}}"
)
.is_ok());

let data = btreemap! {
"a".to_string() => to_json(&btreemap! {
Expand Down Expand Up @@ -381,11 +358,9 @@ mod test {
#[test]
fn test_key_iteration_with_unicode() {
let mut handlebars = Registry::new();
assert!(
handlebars
.register_template_string("t0", "{{#each this}}{{@key}}: {{this}}\n{{/each}}")
.is_ok()
);
assert!(handlebars
.register_template_string("t0", "{{#each this}}{{@key}}: {{this}}\n{{/each}}")
.is_ok());
let data = json!({
"normal": 1,
"你好": 2,
Expand Down
38 changes: 15 additions & 23 deletions src/helpers/helper_if.rs
Expand Up @@ -55,16 +55,12 @@ mod test {
#[test]
fn test_if() {
let mut handlebars = Registry::new();
assert!(
handlebars
.register_template_string("t0", "{{#if this}}hello{{/if}}")
.is_ok()
);
assert!(
handlebars
.register_template_string("t1", "{{#unless this}}hello{{else}}world{{/unless}}")
.is_ok()
);
assert!(handlebars
.register_template_string("t0", "{{#if this}}hello{{/if}}")
.is_ok());
assert!(handlebars
.register_template_string("t1", "{{#unless this}}hello{{else}}world{{/unless}}")
.is_ok());

let r0 = handlebars.render("t0", &true);
assert_eq!(r0.ok().unwrap(), "hello".to_string());
Expand All @@ -83,19 +79,15 @@ mod test {

let mut handlebars = Registry::new();
handlebars.register_helper("with", Box::new(WITH_HELPER));
assert!(
handlebars
.register_template_string("t0", "{{#if a.c.d}}hello {{a.b}}{{/if}}")
.is_ok()
);
assert!(
handlebars
.register_template_string(
"t1",
"{{#with a}}{{#if c.d}}hello {{../a.b}}{{/if}}{{/with}}"
)
.is_ok()
);
assert!(handlebars
.register_template_string("t0", "{{#if a.c.d}}hello {{a.b}}{{/if}}")
.is_ok());
assert!(handlebars
.register_template_string(
"t1",
"{{#with a}}{{#if c.d}}hello {{../a.b}}{{/if}}{{/with}}"
)
.is_ok());

let r0 = handlebars.render("t0", &data);
assert_eq!(r0.ok().unwrap(), "hello 99".to_string());
Expand Down
17 changes: 12 additions & 5 deletions src/helpers/helper_log.rs
Expand Up @@ -28,16 +28,23 @@ impl HelperDef for LogHelper {
let param = h
.param(0)
.ok_or_else(|| RenderError::new("Param not found for helper \"log\""))?;
let level = h.hash_get("level")
let level = h
.hash_get("level")
.and_then(|v| v.value().as_str())
.unwrap_or("info");

if let Ok(log_level) = Level::from_str(level) {
log!(log_level, "{}: {}",
param.path().unwrap_or(&"".to_owned()),
param.value().render())
log!(
log_level,
"{}: {}",
param.path().unwrap_or(&"".to_owned()),
param.value().render()
)
} else {
return Err(RenderError::new(&format!("Unsupported logging level {}", level)));
return Err(RenderError::new(&format!(
"Unsupported logging level {}",
level
)));
}
Ok(())
}
Expand Down

0 comments on commit c6f78bc

Please sign in to comment.