aboutsummaryrefslogtreecommitdiffstats
path: root/askama_escape/src/lib.rs
diff options
context:
space:
mode:
authorLibravatar yossyJ <28825627+yossyJ@users.noreply.github.com>2019-01-06 22:28:22 +0900
committerLibravatar Dirkjan Ochtman <dirkjan@ochtman.nl>2019-01-06 14:28:22 +0100
commited47f17f3c0d9155be51964e377790a29d254a21 (patch)
treede9868ecec71f21dee00d4ede3f375ffcf48ae6b /askama_escape/src/lib.rs
parentbef88f1696b828caf72695561c2270456f35894d (diff)
downloadaskama-ed47f17f3c0d9155be51964e377790a29d254a21.tar.gz
askama-ed47f17f3c0d9155be51964e377790a29d254a21.tar.bz2
askama-ed47f17f3c0d9155be51964e377790a29d254a21.zip
Improved rendering time (#190)
* Improved rendering time * Fix useless codes
Diffstat (limited to '')
-rw-r--r--askama_escape/src/lib.rs24
1 files changed, 23 insertions, 1 deletions
diff --git a/askama_escape/src/lib.rs b/askama_escape/src/lib.rs
index 8c23530..a591a9c 100644
--- a/askama_escape/src/lib.rs
+++ b/askama_escape/src/lib.rs
@@ -1,4 +1,5 @@
use std::fmt::{self, Display, Formatter};
+use std::io::{self, prelude::*};
use std::str;
#[derive(Debug, PartialEq)]
@@ -37,12 +38,33 @@ where
{
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
match *self {
- MarkupDisplay::Unsafe(ref t) => escape(&t.to_string()).fmt(f),
+ MarkupDisplay::Unsafe(ref t) => {
+ let mut w = EscapeWriter { fmt: f };
+ write!(w, "{}", t).map_err(|_e| fmt::Error)
+ }
MarkupDisplay::Safe(ref t) => t.fmt(f),
}
}
}
+pub struct EscapeWriter<'a, 'b: 'a> {
+ fmt: &'a mut fmt::Formatter<'b>,
+}
+
+impl io::Write for EscapeWriter<'_, '_> {
+ fn write(&mut self, bytes: &[u8]) -> io::Result<usize> {
+ let escaped = Escaped { bytes };
+ escaped
+ .fmt(self.fmt)
+ .map_err(|e| io::Error::new(io::ErrorKind::Other, e.to_string()))?;
+ Ok(bytes.len())
+ }
+
+ fn flush(&mut self) -> std::io::Result<()> {
+ Ok(())
+ }
+}
+
pub fn escape(s: &str) -> Escaped<'_> {
Escaped {
bytes: s.as_bytes(),