use std::io::{Error as IOError, Write}; use std::string::FromUtf8Error; /// The Output API. /// /// Handlebars uses this trait to define rendered output. pub trait Output { fn write(&mut self, seg: &str) -> Result<(), IOError>; } pub struct WriteOutput { write: W, } impl Output for WriteOutput { fn write(&mut self, seg: &str) -> Result<(), IOError> { self.write.write_all(seg.as_bytes()) } } impl WriteOutput { pub fn new(write: W) -> WriteOutput { WriteOutput { write } } } pub struct StringOutput { buf: Vec, } impl Output for StringOutput { fn write(&mut self, seg: &str) -> Result<(), IOError> { self.buf.extend_from_slice(seg.as_bytes()); Ok(()) } } impl StringOutput { pub fn new() -> StringOutput { StringOutput { buf: Vec::with_capacity(8 * 1024), } } pub fn into_string(self) -> Result { String::from_utf8(self.buf) } } impl Default for StringOutput { fn default() -> Self { StringOutput::new() } }