]> git.proxmox.com Git - rustc.git/blame - vendor/handlebars/src/output.rs
New upstream version 1.65.0+dfsg1
[rustc.git] / vendor / handlebars / src / output.rs
CommitLineData
9fa01778
XL
1use std::io::{Error as IOError, Write};
2use std::string::FromUtf8Error;
3
f9f354fc
XL
4/// The Output API.
5///
6/// Handlebars uses this trait to define rendered output.
9fa01778
XL
7pub trait Output {
8 fn write(&mut self, seg: &str) -> Result<(), IOError>;
9}
10
11pub struct WriteOutput<W: Write> {
12 write: W,
13}
14
15impl<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
21impl<W: Write> WriteOutput<W> {
22 pub fn new(write: W) -> WriteOutput<W> {
23 WriteOutput { write }
24 }
25}
26
27pub struct StringOutput {
28 buf: Vec<u8>,
29}
30
31impl 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
38impl 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
50impl Default for StringOutput {
51 fn default() -> Self {
52 StringOutput::new()
53 }
54}