From 31b90ccc657a5468de6bdffea8d309f502cd0d07 Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Thu, 21 Jun 2018 12:19:54 +0200 Subject: Fix formatting with cargo fmt --- askama_shared/src/error.rs | 16 ++++++------- askama_shared/src/escaping.rs | 47 ++++++++++++++++++++++++++++----------- askama_shared/src/filters/json.rs | 4 +--- askama_shared/src/filters/mod.rs | 18 +++++++-------- askama_shared/src/lib.rs | 2 +- askama_shared/src/path.rs | 10 ++++++--- 6 files changed, 58 insertions(+), 39 deletions(-) (limited to 'askama_shared/src') diff --git a/askama_shared/src/error.rs b/askama_shared/src/error.rs index cdd6b55..df175cd 100644 --- a/askama_shared/src/error.rs +++ b/askama_shared/src/error.rs @@ -1,5 +1,5 @@ +use std::error::Error as ErrorTrait; use std::fmt::{self, Display}; -use std::error::{Error as ErrorTrait}; pub type Result = ::std::result::Result; @@ -23,7 +23,7 @@ pub type Result = ::std::result::Result; /// bring to this crate are small, which is why /// `std::error::Error` was used. /// -#[derive(Debug, )] +#[derive(Debug)] pub enum Error { /// formatting error Fmt(fmt::Error), @@ -40,22 +40,21 @@ pub enum Error { } impl ErrorTrait for Error { - fn description(&self) -> &str { match *self { Error::Fmt(ref err) => err.description(), #[cfg(feature = "serde-json")] Error::Json(ref err) => err.description(), - _ => "unknown error: __Nonexhaustive" + _ => "unknown error: __Nonexhaustive", } } fn cause(&self) -> Option<&ErrorTrait> { match *self { Error::Fmt(ref err) => err.cause(), - #[cfg(feature = "serde-json")] + #[cfg(feature = "serde-json")] Error::Json(ref err) => err.cause(), - _ => None + _ => None, } } } @@ -67,7 +66,7 @@ impl Display for Error { #[cfg(feature = "serde-json")] Error::Json(ref err) => write!(formatter, "json conversion error: {}", err), - _ => write!(formatter, "unknown error: __Nonexhaustive") + _ => write!(formatter, "unknown error: __Nonexhaustive"), } } } @@ -85,11 +84,10 @@ impl From<::serde_json::Error> for Error { } } - #[cfg(test)] mod tests { use super::Error; trait AssertSendSyncStatic: Send + Sync + 'static {} impl AssertSendSyncStatic for Error {} -} \ No newline at end of file +} diff --git a/askama_shared/src/escaping.rs b/askama_shared/src/escaping.rs index 896fc2a..e22e0ce 100644 --- a/askama_shared/src/escaping.rs +++ b/askama_shared/src/escaping.rs @@ -1,13 +1,18 @@ use std::fmt::{self, Display, Formatter}; - #[derive(Debug, PartialEq)] -pub enum MarkupDisplay where T: Display { +pub enum MarkupDisplay +where + T: Display, +{ Safe(T), Unsafe(T), } -impl MarkupDisplay where T: Display { +impl MarkupDisplay +where + T: Display, +{ pub fn mark_safe(self) -> MarkupDisplay { match self { MarkupDisplay::Unsafe(t) => MarkupDisplay::Safe(t), @@ -21,13 +26,19 @@ impl MarkupDisplay where T: Display { } } -impl From for MarkupDisplay where T: Display { +impl From for MarkupDisplay +where + T: Display, +{ fn from(t: T) -> MarkupDisplay { MarkupDisplay::Unsafe(t) } } -impl Display for MarkupDisplay where T: Display { +impl Display for MarkupDisplay +where + T: Display, +{ fn fmt(&self, f: &mut Formatter) -> fmt::Result { match *self { MarkupDisplay::Unsafe(_) => write!(f, "{}", escape(self.unsafe_string())), @@ -36,7 +47,6 @@ impl Display for MarkupDisplay where T: Display { } } - fn escapable(b: u8) -> bool { match b { b'<' | b'>' | b'&' | b'"' | b'\'' | b'/' => true, @@ -65,12 +75,24 @@ pub fn escape(s: String) -> String { } start = *idx + 1; match bytes[*idx] { - b'<' => { res.extend(b"<"); }, - b'>' => { res.extend(b">"); }, - b'&' => { res.extend(b"&"); }, - b'"' => { res.extend(b"""); }, - b'\'' => { res.extend(b"'"); }, - b'/' => { res.extend(b"/"); }, + b'<' => { + res.extend(b"<"); + } + b'>' => { + res.extend(b">"); + } + b'&' => { + res.extend(b"&"); + } + b'"' => { + res.extend(b"""); + } + b'\'' => { + res.extend(b"'"); + } + b'/' => { + res.extend(b"/"); + } _ => panic!("incorrect indexing"), } } @@ -81,7 +103,6 @@ pub fn escape(s: String) -> String { String::from_utf8(res).unwrap() } - #[cfg(test)] mod tests { use super::*; diff --git a/askama_shared/src/filters/json.rs b/askama_shared/src/filters/json.rs index 5d1b3ac..ffc5310 100644 --- a/askama_shared/src/filters/json.rs +++ b/askama_shared/src/filters/json.rs @@ -1,9 +1,8 @@ +use error::{Error, Result}; use serde::Serialize; use serde_json; -use error::{Error, Result}; use MarkupDisplay; - /// Serialize to JSON (requires `serde-json` feature) /// /// ## Errors @@ -17,7 +16,6 @@ pub fn json(s: &S) -> Result> { } } - #[cfg(test)] mod tests { use super::*; diff --git a/askama_shared/src/filters/mod.rs b/askama_shared/src/filters/mod.rs index 32a7ccc..9fc03b6 100644 --- a/askama_shared/src/filters/mod.rs +++ b/askama_shared/src/filters/mod.rs @@ -11,9 +11,8 @@ pub use self::json::json; use std::fmt; -use escaping::{self, MarkupDisplay}; use super::Result; - +use escaping::{self, MarkupDisplay}; // This is used by the code generator to decide whether a named filter is part of // Askama or should refer to a local `filters` module. It should contain all the @@ -32,11 +31,10 @@ pub const BUILT_IN_FILTERS: [&str; 10] = [ "json", // Optional feature; reserve the name anyway ]; - pub fn safe(v: I) -> Result> where D: fmt::Display, - MarkupDisplay: From + MarkupDisplay: From, { let res: MarkupDisplay = v.into(); Ok(res.mark_safe()) @@ -46,7 +44,7 @@ where pub fn escape(i: I) -> Result> where D: fmt::Display, - MarkupDisplay: From + MarkupDisplay: From, { let md: MarkupDisplay = i.into(); Ok(MarkupDisplay::Safe(escaping::escape(md.unsafe_string()))) @@ -56,7 +54,7 @@ where pub fn e(i: I) -> Result> where D: fmt::Display, - MarkupDisplay: From + MarkupDisplay: From, { escape(i) } @@ -99,9 +97,10 @@ pub fn trim(s: &fmt::Display) -> Result { /// Joins iterable into a string separated by provided argument pub fn join(input: I, separator: S) -> Result - where T: fmt::Display, - I: Iterator, - S: AsRef +where + T: fmt::Display, + I: Iterator, + S: AsRef, { let separator: &str = separator.as_ref(); @@ -118,7 +117,6 @@ pub fn join(input: I, separator: S) -> Result Ok(rv) } - #[cfg(test)] mod tests { use super::*; diff --git a/askama_shared/src/lib.rs b/askama_shared/src/lib.rs index cbb99d9..9427dff 100644 --- a/askama_shared/src/lib.rs +++ b/askama_shared/src/lib.rs @@ -5,8 +5,8 @@ extern crate serde; #[cfg(feature = "serde-json")] extern crate serde_json; -pub use escaping::MarkupDisplay; pub use error::{Error, Result}; +pub use escaping::MarkupDisplay; mod error; pub mod filters; pub mod path; diff --git a/askama_shared/src/path.rs b/askama_shared/src/path.rs index c84575f..8b92250 100644 --- a/askama_shared/src/path.rs +++ b/askama_shared/src/path.rs @@ -10,7 +10,7 @@ pub fn get_template_source(tpl_path: &Path) -> String { Err(_) => { let msg = format!("unable to open template file '{}'", &path.to_str().unwrap()); panic!(msg) - }, + } Ok(f) => f, }; let mut s = String::new(); @@ -38,7 +38,11 @@ pub fn find_template_from_path(path: &str, start_at: Option<&Path>) -> PathBuf { if fs_abs_path.exists() { path.to_owned() } else { - panic!(format!("template {:?} not found at {:?}", path.to_str().unwrap(), fs_abs_path)); + panic!(format!( + "template {:?} not found at {:?}", + path.to_str().unwrap(), + fs_abs_path + )); } } @@ -50,8 +54,8 @@ pub fn template_dir() -> PathBuf { #[cfg(test)] mod tests { - use super::{find_template_from_path, get_template_source}; use super::Path; + use super::{find_template_from_path, get_template_source}; #[test] fn get_source() { -- cgit