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
use core::{
    fmt::{Display, Formatter, Result, Write},
    str,
};

pub mod utils;

pub trait Escaper {
    fn write_escaped<W>(&self, fmt: W, string: &str) -> Result
    where
        W: Write;
}

pub struct XmlEscaper;

impl Escaper for XmlEscaper {
    fn write_escaped<W>(&self, mut fmt: W, string: &str) -> Result
    where
        W: Write,
    {
        for c in string.chars() {
            match c {
                '<' => fmt.write_str("&lt;")?,
                '>' => fmt.write_str("&gt;")?,
                '&' => fmt.write_str("&amp;")?,
                '"' => fmt.write_str("&quot;")?,
                '\'' => fmt.write_str("&#x27;")?,
                _ => fmt.write_char(c)?,
            }
        }
        Ok(())
    }
}

pub struct PlainText;

impl Escaper for PlainText {
    fn write_escaped<W>(&self, mut fmt: W, string: &str) -> Result
    where
        W: Write,
    {
        fmt.write_str(string)
    }
}