dejavu_engine/escaper/
mod.rs

1use core::{
2    fmt::{Display, Formatter, Result, Write},
3    str,
4};
5
6pub mod utils;
7
8pub trait Escaper {
9    fn write_escaped<W>(&self, fmt: W, string: &str) -> Result
10    where
11        W: Write;
12}
13
14pub struct XmlEscaper;
15
16impl Escaper for XmlEscaper {
17    fn write_escaped<W>(&self, mut fmt: W, string: &str) -> Result
18    where
19        W: Write,
20    {
21        for c in string.chars() {
22            match c {
23                '<' => fmt.write_str("&lt;")?,
24                '>' => fmt.write_str("&gt;")?,
25                '&' => fmt.write_str("&amp;")?,
26                '"' => fmt.write_str("&quot;")?,
27                '\'' => fmt.write_str("&#x27;")?,
28                _ => fmt.write_char(c)?,
29            }
30        }
31        Ok(())
32    }
33}
34
35pub struct PlainText;
36
37impl Escaper for PlainText {
38    fn write_escaped<W>(&self, mut fmt: W, string: &str) -> Result
39    where
40        W: Write,
41    {
42        fmt.write_str(string)
43    }
44}