diff options
Diffstat (limited to '')
-rw-r--r-- | askama_shared/src/escaping.rs | 47 |
1 files changed, 34 insertions, 13 deletions
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<T> where T: Display { +pub enum MarkupDisplay<T> +where + T: Display, +{ Safe(T), Unsafe(T), } -impl<T> MarkupDisplay<T> where T: Display { +impl<T> MarkupDisplay<T> +where + T: Display, +{ pub fn mark_safe(self) -> MarkupDisplay<T> { match self { MarkupDisplay::Unsafe(t) => MarkupDisplay::Safe(t), @@ -21,13 +26,19 @@ impl<T> MarkupDisplay<T> where T: Display { } } -impl<T> From<T> for MarkupDisplay<T> where T: Display { +impl<T> From<T> for MarkupDisplay<T> +where + T: Display, +{ fn from(t: T) -> MarkupDisplay<T> { MarkupDisplay::Unsafe(t) } } -impl<T> Display 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())), @@ -36,7 +47,6 @@ impl<T> Display for MarkupDisplay<T> 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::*; |