]>
Commit | Line | Data |
---|---|---|
9fa01778 XL |
1 | use std::io::{Error as IOError, Write}; |
2 | use std::string::FromUtf8Error; | |
3 | ||
f9f354fc XL |
4 | /// The Output API. |
5 | /// | |
6 | /// Handlebars uses this trait to define rendered output. | |
9fa01778 XL |
7 | pub trait Output { |
8 | fn write(&mut self, seg: &str) -> Result<(), IOError>; | |
9 | } | |
10 | ||
11 | pub struct WriteOutput<W: Write> { | |
12 | write: W, | |
13 | } | |
14 | ||
15 | impl<W: Write> Output for WriteOutput<W> { | |
16 | fn write(&mut self, seg: &str) -> Result<(), IOError> { | |
17 | self.write.write_all(seg.as_bytes()) | |
18 | } | |
19 | } | |
20 | ||
21 | impl<W: Write> WriteOutput<W> { | |
22 | pub fn new(write: W) -> WriteOutput<W> { | |
23 | WriteOutput { write } | |
24 | } | |
25 | } | |
26 | ||
27 | pub struct StringOutput { | |
28 | buf: Vec<u8>, | |
29 | } | |
30 | ||
31 | impl Output for StringOutput { | |
32 | fn write(&mut self, seg: &str) -> Result<(), IOError> { | |
33 | self.buf.extend_from_slice(seg.as_bytes()); | |
34 | Ok(()) | |
35 | } | |
36 | } | |
37 | ||
38 | impl StringOutput { | |
39 | pub fn new() -> StringOutput { | |
40 | StringOutput { | |
41 | buf: Vec::with_capacity(8 * 1024), | |
42 | } | |
43 | } | |
44 | ||
45 | pub fn into_string(self) -> Result<String, FromUtf8Error> { | |
46 | String::from_utf8(self.buf) | |
47 | } | |
48 | } | |
f2b60f7d FG |
49 | |
50 | impl Default for StringOutput { | |
51 | fn default() -> Self { | |
52 | StringOutput::new() | |
53 | } | |
54 | } |