1 use std::io;
2 
3 use crate::fmt::{Target, WriteStyle};
4 
5 pub(in crate::fmt::writer) mod glob {}
6 
7 pub(in crate::fmt::writer) struct BufferWriter {
8     target: Target,
9 }
10 
11 pub(in crate::fmt) struct Buffer(Vec<u8>);
12 
13 impl BufferWriter {
stderr(_is_test: bool, _write_style: WriteStyle) -> Self14     pub(in crate::fmt::writer) fn stderr(_is_test: bool, _write_style: WriteStyle) -> Self {
15         BufferWriter {
16             target: Target::Stderr,
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: Target::Stdout,
23         }
24     }
25 
buffer(&self) -> Buffer26     pub(in crate::fmt::writer) fn buffer(&self) -> Buffer {
27         Buffer(Vec::new())
28     }
29 
print(&self, buf: &Buffer) -> io::Result<()>30     pub(in crate::fmt::writer) fn print(&self, buf: &Buffer) -> io::Result<()> {
31         // This impl uses the `eprint` and `print` macros
32         // instead of using the streams directly.
33         // This is so their output can be captured by `cargo test`
34         let log = String::from_utf8_lossy(&buf.0);
35 
36         match self.target {
37             Target::Stderr => eprint!("{}", log),
38             Target::Stdout => print!("{}", log),
39         }
40 
41         Ok(())
42     }
43 }
44 
45 impl Buffer {
clear(&mut self)46     pub(in crate::fmt) fn clear(&mut self) {
47         self.0.clear();
48     }
49 
write(&mut self, buf: &[u8]) -> io::Result<usize>50     pub(in crate::fmt) fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
51         self.0.extend(buf);
52         Ok(buf.len())
53     }
54 
flush(&mut self) -> io::Result<()>55     pub(in crate::fmt) fn flush(&mut self) -> io::Result<()> {
56         Ok(())
57     }
58 
59     #[cfg(test)]
bytes(&self) -> &[u8]60     pub(in crate::fmt) fn bytes(&self) -> &[u8] {
61         &self.0
62     }
63 }
64