use std::fmt;
#[macro_export]
macro_rules! escape {
($e:expr) => {
$crate::Escape($e)
};
({$e:expr}) => {
$crate::Escape($e)
};
($($tt:tt)*) => {
$crate::Escape($crate::template!{$($tt)*})
};
}
#[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(())
}