use std::fmt;
pub struct Escape<'a>(pub &'a str);
impl<'a> fmt::Display for Escape<'a> {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
let Escape(s) = *self;
let pile_o_bits = s;
let mut last = 0;
for (i, ch) in s.bytes().enumerate() {
match ch as char {
'<' | '>' | '&' | '\'' | '"' => {
try!(fmt.write_str(&pile_o_bits[last..i]));
let s = match ch as char {
'>' => ">",
'<' => "<",
'&' => "&",
'\'' => "'",
'"' => """,
_ => unreachable!(),
};
try!(fmt.write_str(s));
last = i + 1;
}
_ => {}
}
}
if last < s.len() {
try!(fmt.write_str(&pile_o_bits[last..]));
}
Ok(())
}
}