use crate::Render;
use std::fmt;
pub(crate) fn escape(s: &str, f: &mut fmt::Formatter) -> fmt::Result {
use fmt::Write;
for ch in s.chars() {
match ch {
'&' => write!(f, "&")?,
'<' => write!(f, "<")?,
'>' => write!(f, ">")?,
'"' => write!(f, """)?,
ch => f.write_char(ch)?,
}
}
Ok(())
}
#[derive(Debug, Clone, Copy)]
pub struct Escaped<T>(pub T);
impl<T: fmt::Display> fmt::Display for Escaped<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
escape(&self.0.to_string(), f)
}
}
impl<T: fmt::Display> Render for Escaped<T> {
fn render_to(&self, f: &mut fmt::Formatter) -> fmt::Result {
escape(&self.0.to_string(), f)
}
}