1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
use std::fmt;
#[macro_export]
macro_rules! escape {
($e:expr) => {
$crate::Escape($e)
};
({$e:expr}) => {
$crate::Escape($e)
};
($($tt:tt)*) => {
$crate::Escape($crate::FnFmt(|_f| { $crate::_template_!({_f} $($tt)*); Ok(()) }))
};
}
#[derive(Copy, Clone, Debug)]
#[repr(transparent)]
pub struct Escape<T>(pub T);
impl<T: fmt::Display> fmt::Display for Escape<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
escape_w(&self.0.to_string(), f)
}
}
fn escape_w<F: fmt::Write>(s: &str, result: &mut F) -> fmt::Result {
for chr in s.chars() {
match chr {
'&' => result.write_str("&"),
'<' => result.write_str("<"),
'>' => result.write_str(">"),
'\'' => result.write_str("'"),
'\"' => result.write_str("""),
chr => result.write_char(chr),
}?;
}
Ok(())
}