Skip to content

Commit e8674a2

Browse files
committed
refactor(fmt): Consolidate target printing
1 parent f5f3392 commit e8674a2

4 files changed

Lines changed: 27 additions & 37 deletions

File tree

src/fmt/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,7 @@ mod tests {
468468
fmt.write(&record).expect("failed to write record");
469469

470470
let buf = buf.borrow();
471-
String::from_utf8(buf.bytes().to_vec()).expect("failed to read record")
471+
String::from_utf8(buf.as_bytes().to_vec()).expect("failed to read record")
472472
}
473473

474474
fn write_target(target: &str, fmt: DefaultFormat) -> String {

src/fmt/writer/buffer/plain.rs

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -42,25 +42,7 @@ impl BufferWriter {
4242
}
4343

4444
pub(in crate::fmt::writer) fn print(&self, buf: &Buffer) -> io::Result<()> {
45-
use std::io::Write as _;
46-
47-
// This impl uses the `eprint` and `print` macros
48-
// instead of using the streams directly.
49-
// This is so their output can be captured by `cargo test`.
50-
match &self.target {
51-
WritableTarget::WriteStdout => {
52-
write!(std::io::stdout(), "{}", String::from_utf8_lossy(&buf.0))?
53-
}
54-
WritableTarget::PrintStdout => print!("{}", String::from_utf8_lossy(&buf.0)),
55-
WritableTarget::WriteStderr => {
56-
write!(std::io::stderr(), "{}", String::from_utf8_lossy(&buf.0))?
57-
}
58-
WritableTarget::PrintStderr => eprint!("{}", String::from_utf8_lossy(&buf.0)),
59-
// Safety: If the target type is `Pipe`, `target_pipe` will always be non-empty.
60-
WritableTarget::Pipe(pipe) => pipe.lock().unwrap().write_all(&buf.0)?,
61-
}
62-
63-
Ok(())
45+
self.target.print(buf)
6446
}
6547
}
6648

@@ -80,8 +62,7 @@ impl Buffer {
8062
Ok(())
8163
}
8264

83-
#[cfg(test)]
84-
pub(in crate::fmt) fn bytes(&self) -> &[u8] {
65+
pub(in crate::fmt) fn as_bytes(&self) -> &[u8] {
8566
&self.0
8667
}
8768
}

src/fmt/writer/buffer/termcolor.rs

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -59,20 +59,7 @@ impl BufferWriter {
5959

6060
pub(in crate::fmt::writer) fn print(&self, buf: &Buffer) -> io::Result<()> {
6161
if let Some(target) = &self.uncolored_target {
62-
// This impl uses the `eprint` and `print` macros
63-
// instead of `termcolor`'s buffer.
64-
// This is so their output can be captured by `cargo test`
65-
let log = String::from_utf8_lossy(buf.bytes());
66-
67-
match target {
68-
WritableTarget::WriteStdout => print!("{}", log),
69-
WritableTarget::PrintStdout => print!("{}", log),
70-
WritableTarget::WriteStderr => eprint!("{}", log),
71-
WritableTarget::PrintStderr => eprint!("{}", log),
72-
WritableTarget::Pipe(pipe) => write!(pipe.lock().unwrap(), "{}", log)?,
73-
}
74-
75-
Ok(())
62+
target.print(buf)
7663
} else {
7764
self.inner.print(&buf.inner)
7865
}
@@ -97,7 +84,7 @@ impl Buffer {
9784
self.inner.flush()
9885
}
9986

100-
pub(in crate::fmt) fn bytes(&self) -> &[u8] {
87+
pub(in crate::fmt) fn as_bytes(&self) -> &[u8] {
10188
self.inner.as_slice()
10289
}
10390

src/fmt/writer/mod.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,28 @@ pub(super) enum WritableTarget {
6060
Pipe(Box<Mutex<dyn io::Write + Send + 'static>>),
6161
}
6262

63+
impl WritableTarget {
64+
fn print(&self, buf: &Buffer) -> io::Result<()> {
65+
use std::io::Write as _;
66+
67+
let buf = buf.as_bytes();
68+
match self {
69+
WritableTarget::WriteStdout => {
70+
write!(std::io::stdout(), "{}", String::from_utf8_lossy(buf))?
71+
}
72+
WritableTarget::PrintStdout => print!("{}", String::from_utf8_lossy(buf)),
73+
WritableTarget::WriteStderr => {
74+
write!(std::io::stderr(), "{}", String::from_utf8_lossy(buf))?
75+
}
76+
WritableTarget::PrintStderr => eprint!("{}", String::from_utf8_lossy(buf)),
77+
// Safety: If the target type is `Pipe`, `target_pipe` will always be non-empty.
78+
WritableTarget::Pipe(pipe) => pipe.lock().unwrap().write_all(buf)?,
79+
}
80+
81+
Ok(())
82+
}
83+
}
84+
6385
impl fmt::Debug for WritableTarget {
6486
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
6587
write!(

0 commit comments

Comments
 (0)