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
pub trait ToHtml {
fn to_html(&self, out: &mut Write) -> io::Result<()>;
}
#[allow(dead_code)]
pub struct Html<T> (pub T);
impl<T: Display> ToHtml for Html<T> {
fn to_html(&self, out: &mut Write) -> io::Result<()> {
write!(out, "{}", self.0)
}
}
impl<T: Display> ToHtml for T {
fn to_html(&self, out: &mut Write) -> io::Result<()> {
let mut buf = Vec::new();
write!(buf, "{}", self)?;
out.write_all(&buf.into_iter().fold(Vec::new(), |mut v, c| {
match c {
b'<' => v.extend_from_slice(b"<"),
b'>' => v.extend_from_slice(b">"),
b'&' => v.extend_from_slice(b"&"),
c => v.push(c),
};
v
}))
}
}