summaryrefslogtreecommitdiffstats
path: root/vendor/handlebars/src/output.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:02:58 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:02:58 +0000
commit698f8c2f01ea549d77d7dc3338a12e04c11057b9 (patch)
tree173a775858bd501c378080a10dca74132f05bc50 /vendor/handlebars/src/output.rs
parentInitial commit. (diff)
downloadrustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.tar.xz
rustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.zip
Adding upstream version 1.64.0+dfsg1.upstream/1.64.0+dfsg1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r--vendor/handlebars/src/output.rs48
1 files changed, 48 insertions, 0 deletions
diff --git a/vendor/handlebars/src/output.rs b/vendor/handlebars/src/output.rs
new file mode 100644
index 000000000..f1c5865a5
--- /dev/null
+++ b/vendor/handlebars/src/output.rs
@@ -0,0 +1,48 @@
+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<W: Write> {
+ write: W,
+}
+
+impl<W: Write> Output for WriteOutput<W> {
+ fn write(&mut self, seg: &str) -> Result<(), IOError> {
+ self.write.write_all(seg.as_bytes())
+ }
+}
+
+impl<W: Write> WriteOutput<W> {
+ pub fn new(write: W) -> WriteOutput<W> {
+ WriteOutput { write }
+ }
+}
+
+pub struct StringOutput {
+ buf: Vec<u8>,
+}
+
+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, FromUtf8Error> {
+ String::from_utf8(self.buf)
+ }
+}