struct EscapedString<'a> {
s: std::str::Chars<'a>,
}
impl<'a> Iterator for EscapedString<'a> {
type Item = Result<char, String>;
fn next(&mut self) -> Option<Self::Item> {
self.s.next().map(|c| match c {
'\\' => match self.s.next() {
None => Err("Escape char at end of str.".to_string()),
Some('n') => Ok('\n'),
Some('\\') => Ok('\\'),
Some('{') => Ok('{'),
Some('}') => Ok('}'),
Some('"') => Ok('"'),
Some(c) => Err(format!("Unknown escape char `{}`", c)),
},
c => Ok(c),
})
}
}
pub fn process_string(str: &str) -> Result<String, String> {
let s = EscapedString { s: str.chars() };
s.collect()
}