1 use std::{io, sync::Mutex};
2 
3 use crate::fmt::{WritableTarget, WriteStyle};
4 
5 pub(in crate::fmt::writer) struct BufferWriter {
6     target: WritableTarget,
7 }
8 
9 impl BufferWriter {
stderr(is_test: bool, _write_style: WriteStyle) -> Self10     pub(in crate::fmt::writer) fn stderr(is_test: bool, _write_style: WriteStyle) -> Self {
11         BufferWriter {
12             target: if is_test {
13                 WritableTarget::PrintStderr
14             } else {
15                 WritableTarget::WriteStderr
16             },
17         }
18     }
19 
stdout(is_test: bool, _write_style: WriteStyle) -> Self20     pub(in crate::fmt::writer) fn stdout(is_test: bool, _write_style: WriteStyle) -> Self {
21         BufferWriter {
22             target: if is_test {
23                 WritableTarget::PrintStdout
24             } else {
25                 WritableTarget::WriteStdout
26             },
27         }
28     }
29 
pipe(pipe: Box<Mutex<dyn io::Write + Send + 'static>>) -> Self30     pub(in crate::fmt::writer) fn pipe(pipe: Box<Mutex<dyn io::Write + Send + 'static>>) -> Self {
31         BufferWriter {
32             target: WritableTarget::Pipe(pipe),
33         }
34     }
35 
write_style(&self) -> WriteStyle36     pub(in crate::fmt::writer) fn write_style(&self) -> WriteStyle {
37         WriteStyle::Never
38     }
39 
buffer(&self) -> Buffer40     pub(in crate::fmt::writer) fn buffer(&self) -> Buffer {
41         Buffer(Vec::new())
42     }
43 
print(&self, buf: &Buffer) -> io::Result<()>44     pub(in crate::fmt::writer) fn print(&self, buf: &Buffer) -> io::Result<()> {
45         self.target.print(buf)
46     }
47 }
48 
49 pub(in crate::fmt) struct Buffer(Vec<u8>);
50 
51 impl Buffer {
clear(&mut self)52     pub(in crate::fmt) fn clear(&mut self) {
53         self.0.clear();
54     }
55 
write(&mut self, buf: &[u8]) -> io::Result<usize>56     pub(in crate::fmt) fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
57         self.0.extend(buf);
58         Ok(buf.len())
59     }
60 
flush(&mut self) -> io::Result<()>61     pub(in crate::fmt) fn flush(&mut self) -> io::Result<()> {
62         Ok(())
63     }
64 
as_bytes(&self) -> &[u8]65     pub(in crate::fmt) fn as_bytes(&self) -> &[u8] {
66         &self.0
67     }
68 }
69