diff options
author | Dirkjan Ochtman <dirkjan@ochtman.nl> | 2017-11-21 20:53:57 +0100 |
---|---|---|
committer | Dirkjan Ochtman <dirkjan@ochtman.nl> | 2017-11-21 20:57:56 +0100 |
commit | 83cc6e0ca3e9878c47593ab737e6bf106ea062db (patch) | |
tree | e3ec5ac6d25c5835792d7fdc094253ab2fade059 /askama_shared/src | |
parent | 958680aee0fc2d58200382223937e8123fae7459 (diff) | |
download | askama-83cc6e0ca3e9878c47593ab737e6bf106ea062db.tar.gz askama-83cc6e0ca3e9878c47593ab737e6bf106ea062db.tar.bz2 askama-83cc6e0ca3e9878c47593ab737e6bf106ea062db.zip |
Apply suggestions from rustfmt to improve style
Diffstat (limited to 'askama_shared/src')
-rw-r--r-- | askama_shared/src/escaping.rs | 12 | ||||
-rw-r--r-- | askama_shared/src/filters/json.rs | 8 | ||||
-rw-r--r-- | askama_shared/src/filters/mod.rs | 27 | ||||
-rw-r--r-- | askama_shared/src/generator.rs | 94 | ||||
-rw-r--r-- | askama_shared/src/input.rs | 4 | ||||
-rw-r--r-- | askama_shared/src/lib.rs | 6 | ||||
-rw-r--r-- | askama_shared/src/path.rs | 3 |
7 files changed, 86 insertions, 68 deletions
diff --git a/askama_shared/src/escaping.rs b/askama_shared/src/escaping.rs index 99babfd..896fc2a 100644 --- a/askama_shared/src/escaping.rs +++ b/askama_shared/src/escaping.rs @@ -11,12 +11,12 @@ impl<T> MarkupDisplay<T> where T: Display { pub fn mark_safe(self) -> MarkupDisplay<T> { match self { MarkupDisplay::Unsafe(t) => MarkupDisplay::Safe(t), - _ => { self }, + _ => self, } } pub fn unsafe_string(&self) -> String { match *self { - MarkupDisplay::Safe(ref t) | MarkupDisplay::Unsafe(ref t) => format!("{}", t) + MarkupDisplay::Safe(ref t) | MarkupDisplay::Unsafe(ref t) => format!("{}", t), } } } @@ -30,12 +30,8 @@ impl<T> From<T> for MarkupDisplay<T> where T: Display { impl<T> Display for MarkupDisplay<T> where T: Display { fn fmt(&self, f: &mut Formatter) -> fmt::Result { match *self { - MarkupDisplay::Unsafe(_) => { - write!(f, "{}", escape(self.unsafe_string())) - }, - MarkupDisplay::Safe(ref t) => { - t.fmt(f) - }, + MarkupDisplay::Unsafe(_) => write!(f, "{}", escape(self.unsafe_string())), + MarkupDisplay::Safe(ref t) => t.fmt(f), } } } diff --git a/askama_shared/src/filters/json.rs b/askama_shared/src/filters/json.rs index c388128..6b818eb 100644 --- a/askama_shared/src/filters/json.rs +++ b/askama_shared/src/filters/json.rs @@ -26,10 +26,12 @@ mod tests { fn test_json() { assert_eq!(json(&true).unwrap().unsafe_string(), "true"); assert_eq!(json(&"foo").unwrap().unsafe_string(), r#""foo""#); - assert_eq!(json(&vec!["foo", "bar"]).unwrap().unsafe_string(), -r#"[ + assert_eq!( + json(&vec!["foo", "bar"]).unwrap().unsafe_string(), + r#"[ "foo", "bar" -]"#); +]"# + ); } } diff --git a/askama_shared/src/filters/mod.rs b/askama_shared/src/filters/mod.rs index 8b1f814..32a7ccc 100644 --- a/askama_shared/src/filters/mod.rs +++ b/askama_shared/src/filters/mod.rs @@ -67,7 +67,7 @@ where /// Rust). All arguments are passed through to the `format!()` /// [macro](https://doc.rust-lang.org/stable/std/macro.format.html) by /// the Askama code generator. -pub fn format() { } +pub fn format() {} /// Converts to lowercase. pub fn lower(s: &fmt::Display) -> Result<String> { @@ -145,16 +145,28 @@ mod tests { #[test] fn test_join() { - assert_eq!(join((&["hello", "world"]).into_iter(), ", ").unwrap(), "hello, world"); + assert_eq!( + join((&["hello", "world"]).into_iter(), ", ").unwrap(), + "hello, world" + ); assert_eq!(join((&["hello"]).into_iter(), ", ").unwrap(), "hello"); let empty: &[&str] = &[]; assert_eq!(join(empty.into_iter(), ", ").unwrap(), ""); let input: Vec<String> = vec!["foo".into(), "bar".into(), "bazz".into()]; - assert_eq!(join((&input).into_iter(), ":".to_string()).unwrap(), "foo:bar:bazz"); - assert_eq!(join(input.clone().into_iter(), ":").unwrap(), "foo:bar:bazz"); - assert_eq!(join(input.clone().into_iter(), ":".to_string()).unwrap(), "foo:bar:bazz"); + assert_eq!( + join((&input).into_iter(), ":".to_string()).unwrap(), + "foo:bar:bazz" + ); + assert_eq!( + join(input.clone().into_iter(), ":").unwrap(), + "foo:bar:bazz" + ); + assert_eq!( + join(input.clone().into_iter(), ":".to_string()).unwrap(), + "foo:bar:bazz" + ); let input: &[String] = &["foo".into(), "bar".into()]; assert_eq!(join(input.into_iter(), ":").unwrap(), "foo:bar"); @@ -164,6 +176,9 @@ mod tests { let input: Vec<&str> = vec![&real]; assert_eq!(join(input.into_iter(), ";").unwrap(), "blah"); - assert_eq!(join((&&&&&["foo", "bar"]).into_iter(), ", ").unwrap(), "foo, bar"); + assert_eq!( + join((&&&&&["foo", "bar"]).into_iter(), ", ").unwrap(), + "foo, bar" + ); } } diff --git a/askama_shared/src/generator.rs b/askama_shared/src/generator.rs index f8d8ddf..63ebeb3 100644 --- a/askama_shared/src/generator.rs +++ b/askama_shared/src/generator.rs @@ -3,7 +3,7 @@ use input::TemplateInput; use parser::{self, Cond, Expr, Macro, MatchParameter, MatchVariant, Node, Target, When, WS}; use path; -use quote::{Tokens, ToTokens}; +use quote::{ToTokens, Tokens}; use std::{cmp, hash, str}; use std::path::Path; @@ -34,11 +34,11 @@ impl<'a> State<'a> { let mut macros = HashMap::new(); for n in nodes.iter() { match *n { - Node::Extends(ref path) => { - match base { - Some(_) => panic!("multiple extend blocks found"), - None => { base = Some(path); }, - } + Node::Extends(ref path) => match base { + Some(_) => panic!("multiple extend blocks found"), + None => { + base = Some(path); + }, }, ref def @ Node::BlockDef(_, _, _, _) => { blocks.push(def); @@ -65,9 +65,7 @@ impl<'a> State<'a> { fn trait_name_for_path(base: &Option<&Expr>, path: &Path) -> String { let rooted_path = match *base { - Some(&Expr::StrLit(user_path)) => { - path::find_template_from_path(user_path, Some(path)) - }, + Some(&Expr::StrLit(user_path)) => path::find_template_from_path(user_path, Some(path)), _ => path.to_path_buf(), }; @@ -85,17 +83,15 @@ fn trait_name_for_path(base: &Option<&Expr>, path: &Path) -> String { fn get_parent_type(ast: &syn::DeriveInput) -> Option<&syn::Ty> { match ast.body { - syn::Body::Struct(ref data) => { - data.fields().iter().filter_map(|f| { - f.ident.as_ref().and_then(|name| { - if name.as_ref() == "_parent" { - Some(&f.ty) - } else { - None - } - }) + syn::Body::Struct(ref data) => data.fields().iter().filter_map(|f| { + f.ident.as_ref().and_then(|name| { + if name.as_ref() == "_parent" { + Some(&f.ty) + } else { + None + } }) - }, + }), _ => panic!("derive(Template) only works for struct items"), }.next() } @@ -110,7 +106,6 @@ struct Generator<'a> { } impl<'a> Generator<'a> { - fn new<'n>(locals: SetChain<'n, &'n str>, indent: u8) -> Generator<'n> { Generator { buf: String::new(), @@ -142,7 +137,11 @@ impl<'a> Generator<'a> { self.deref_to_parent(state, parent_type); } - let trait_nodes = if !state.derived { Some(&state.nodes[..]) } else { None }; + let trait_nodes = if !state.derived { + Some(&state.nodes[..]) + } else { + None + }; self.impl_trait(state, trait_nodes); self.impl_template_for_trait(state); } else { @@ -201,7 +200,8 @@ impl<'a> Generator<'a> { self.writeln(&format!( "fn render_trait_into(&self, timpl: &{}, writer: &mut ::std::fmt::Write) \ -> ::askama::Result<()> {{", - state.trait_name)); + state.trait_name + )); self.writeln("#[allow(unused_imports)] use ::std::ops::Deref as HiddenDerefTrait;"); if let Some(nodes) = nodes { @@ -335,11 +335,19 @@ impl<'a> Generator<'a> { fn handle(&mut self, state: &'a State, nodes: &'a [Node], level: AstLevel) { for n in nodes { match *n { - Node::Lit(lws, val, rws) => { self.write_lit(lws, val, rws); } + Node::Lit(lws, val, rws) => { + self.write_lit(lws, val, rws); + }, Node::Comment() => {}, - Node::Expr(ref ws, ref val) => { self.write_expr(state, ws, val); }, - Node::LetDecl(ref ws, ref var) => { self.write_let_decl(ws, var); }, - Node::Let(ref ws, ref var, ref val) => { self.write_let(ws, var, val); }, + Node::Expr(ref ws, ref val) => { + self.write_expr(state, ws, val); + }, + Node::LetDecl(ref ws, ref var) => { + self.write_let_decl(ws, var); + }, + Node::Let(ref ws, ref var, ref val) => { + self.write_let(ws, var, val); + }, Node::Cond(ref conds, ref ws) => { self.write_cond(state, conds, ws); }, @@ -392,7 +400,8 @@ impl<'a> Generator<'a> { self.writeln(&format!( "fn render_block_{}_into(&self, writer: &mut ::std::fmt::Write) \ -> ::askama::Result<()> {{", - name)); + name + )); self.prepare_ws(ws1); self.locals.push(); @@ -606,8 +615,7 @@ impl<'a> Generator<'a> { assert!(rws.is_empty()); self.next_ws = Some(lws); } else { - self.writeln(&format!("writer.write_str({:#?})?;", - lws)); + self.writeln(&format!("writer.write_str({:#?})?;", lws)); } } if !val.is_empty() { @@ -629,11 +637,11 @@ impl<'a> Generator<'a> { Expr::Array(ref elements) => self.visit_array(elements), Expr::Attr(ref obj, name) => self.visit_attr(obj, name), Expr::Filter(name, ref args) => self.visit_filter(name, args), - Expr::BinOp(op, ref left, ref right) => - self.visit_binop(op, left, right), + Expr::BinOp(op, ref left, ref right) => self.visit_binop(op, left, right), Expr::Group(ref inner) => self.visit_group(inner), - Expr::MethodCall(ref obj, method, ref args) => - self.visit_method_call(obj, method, args), + Expr::MethodCall(ref obj, method, ref args) => { + self.visit_method_call(obj, method, args) + }, } } @@ -649,12 +657,12 @@ impl<'a> Generator<'a> { self.write("&"); self.write(s); DisplayWrap::Unwrapped - } + }, MatchVariant::Path(ref s) => { self.write("&"); self.write(&s.join("::")); DisplayWrap::Unwrapped - } + }, } } @@ -666,7 +674,7 @@ impl<'a> Generator<'a> { self.write("ref "); self.write(s); DisplayWrap::Unwrapped - } + }, } } @@ -817,7 +825,7 @@ impl<'a> Generator<'a> { fn visit_target<'t>(&mut self, target: &'t Target) -> Vec<&'t str> { match *target { - Target::Name(s) => { self.visit_target_single(s) }, + Target::Name(s) => self.visit_target_single(s), } } @@ -837,8 +845,7 @@ impl<'a> Generator<'a> { if self.next_ws.is_some() && !ws.0 { let val = self.next_ws.unwrap(); if !val.is_empty() { - self.writeln(&format!("writer.write_str({:#?})?;", - val)); + self.writeln(&format!("writer.write_str({:#?})?;", val)); } } self.next_ws = None; @@ -903,11 +910,10 @@ impl<'a, T: 'a> SetChain<'a, T> where T: cmp::Eq + hash::Hash { SetChain { parent: Some(parent), scopes: vec![HashSet::new()] } } fn contains(&self, val: T) -> bool { - self.scopes.iter().rev().any(|set| set.contains(&val)) || - match self.parent { - Some(set) => set.contains(val), - None => false, - } + self.scopes.iter().rev().any(|set| set.contains(&val)) || match self.parent { + Some(set) => set.contains(val), + None => false, + } } fn insert(&mut self, val: T) { self.scopes.last_mut().unwrap().insert(val); diff --git a/askama_shared/src/input.rs b/askama_shared/src/input.rs index bbff4b4..25382cb 100644 --- a/askama_shared/src/input.rs +++ b/askama_shared/src/input.rs @@ -92,7 +92,7 @@ impl<'a> TemplateMeta<'a> { } else { panic!("ext value must be string literal"); }, - attr @ _ => { panic!("unsupported annotation key '{}' found", attr) } + attr @ _ => panic!("unsupported annotation key '{}' found", attr), } } } @@ -123,7 +123,7 @@ impl<'a> TemplateMeta<'a> { } else { EscapeMode::None } - } + }, }; TemplateMeta { source, print, escaping, ext } } diff --git a/askama_shared/src/lib.rs b/askama_shared/src/lib.rs index 88439dc..0f46b91 100644 --- a/askama_shared/src/lib.rs +++ b/askama_shared/src/lib.rs @@ -21,7 +21,7 @@ mod input; mod parser; use input::Print; -use parser::{Node, Macro}; +use parser::{Macro, Node}; use std::borrow::Cow; use std::collections::HashMap; @@ -50,10 +50,10 @@ pub fn build_template(ast: &syn::DeriveInput) -> String { pub struct Imports<'a> { - pub sources: HashMap<&'a str, Cow<'a, str>> + pub sources: HashMap<&'a str, Cow<'a, str>>, } -impl <'a> Imports<'a> { +impl<'a> Imports<'a> { pub fn new(parent_nodes: &'a [Node], parent_path: &'a Path) -> Imports<'a> { let sources = parent_nodes.iter().filter_map(|n| { match *n { diff --git a/askama_shared/src/path.rs b/askama_shared/src/path.rs index 80e364f..f7a3762 100644 --- a/askama_shared/src/path.rs +++ b/askama_shared/src/path.rs @@ -8,8 +8,7 @@ pub fn get_template_source(tpl_path: &Path) -> String { path.push(tpl_path); let mut f = match File::open(&path) { Err(_) => { - let msg = format!("unable to open template file '{}'", - &path.to_str().unwrap()); + let msg = format!("unable to open template file '{}'", &path.to_str().unwrap()); panic!(msg) }, Ok(f) => f, |