use std::io; use std::string::FromUtf8Error; pub struct StringWriter { buf: Vec } impl StringWriter { pub fn new() -> Self { Self { buf: Vec::new() } } pub fn to_string(self) -> Result { String::from_utf8(self.buf) } } impl io::Write for StringWriter { fn write(&mut self, bytes: &[u8]) -> io::Result { self.buf.extend_from_slice(bytes); Ok(bytes.len()) } fn flush(&mut self) -> io::Result<()> { Ok(()) } }