From f5f3392886cb3f2a1a3180122666fa40a2150efe Mon Sep 17 00:00:00 2001 From: Ed Page Date: Wed, 17 Jan 2024 16:31:19 -0600 Subject: [PATCH] refactor(fmt): Pull is_test into the target --- src/fmt/writer/buffer/plain.rs | 30 +++++++++++++++++------------- src/fmt/writer/buffer/termcolor.rs | 10 ++++++---- src/fmt/writer/mod.rs | 20 ++++++++++++++------ 3 files changed, 37 insertions(+), 23 deletions(-) diff --git a/src/fmt/writer/buffer/plain.rs b/src/fmt/writer/buffer/plain.rs index 094f80b..0ebd653 100644 --- a/src/fmt/writer/buffer/plain.rs +++ b/src/fmt/writer/buffer/plain.rs @@ -4,28 +4,32 @@ use crate::fmt::{WritableTarget, WriteStyle}; pub(in crate::fmt::writer) struct BufferWriter { target: WritableTarget, - is_test: bool, } impl BufferWriter { pub(in crate::fmt::writer) fn stderr(is_test: bool, _write_style: WriteStyle) -> Self { BufferWriter { - target: WritableTarget::Stderr, - is_test, + target: if is_test { + WritableTarget::PrintStderr + } else { + WritableTarget::WriteStderr + }, } } pub(in crate::fmt::writer) fn stdout(is_test: bool, _write_style: WriteStyle) -> Self { BufferWriter { - target: WritableTarget::Stdout, - is_test, + target: if is_test { + WritableTarget::PrintStdout + } else { + WritableTarget::WriteStdout + }, } } pub(in crate::fmt::writer) fn pipe(pipe: Box>) -> Self { BufferWriter { target: WritableTarget::Pipe(pipe), - is_test: false, } } @@ -43,17 +47,17 @@ impl BufferWriter { // This impl uses the `eprint` and `print` macros // instead of using the streams directly. // This is so their output can be captured by `cargo test`. - match (&self.target, self.is_test) { - // Safety: If the target type is `Pipe`, `target_pipe` will always be non-empty. - (WritableTarget::Pipe(pipe), _) => pipe.lock().unwrap().write_all(&buf.0)?, - (WritableTarget::Stdout, true) => print!("{}", String::from_utf8_lossy(&buf.0)), - (WritableTarget::Stdout, false) => { + match &self.target { + WritableTarget::WriteStdout => { write!(std::io::stdout(), "{}", String::from_utf8_lossy(&buf.0))? } - (WritableTarget::Stderr, true) => eprint!("{}", String::from_utf8_lossy(&buf.0)), - (WritableTarget::Stderr, false) => { + WritableTarget::PrintStdout => print!("{}", String::from_utf8_lossy(&buf.0)), + WritableTarget::WriteStderr => { write!(std::io::stderr(), "{}", String::from_utf8_lossy(&buf.0))? } + WritableTarget::PrintStderr => eprint!("{}", String::from_utf8_lossy(&buf.0)), + // Safety: If the target type is `Pipe`, `target_pipe` will always be non-empty. + WritableTarget::Pipe(pipe) => pipe.lock().unwrap().write_all(&buf.0)?, } Ok(()) diff --git a/src/fmt/writer/buffer/termcolor.rs b/src/fmt/writer/buffer/termcolor.rs index 9d7374a..f854ae6 100644 --- a/src/fmt/writer/buffer/termcolor.rs +++ b/src/fmt/writer/buffer/termcolor.rs @@ -16,7 +16,7 @@ impl BufferWriter { BufferWriter { inner: termcolor::BufferWriter::stderr(write_style.into_color_choice()), uncolored_target: if is_test { - Some(WritableTarget::Stderr) + Some(WritableTarget::PrintStderr) } else { None }, @@ -28,7 +28,7 @@ impl BufferWriter { BufferWriter { inner: termcolor::BufferWriter::stdout(write_style.into_color_choice()), uncolored_target: if is_test { - Some(WritableTarget::Stdout) + Some(WritableTarget::PrintStdout) } else { None }, @@ -65,8 +65,10 @@ impl BufferWriter { let log = String::from_utf8_lossy(buf.bytes()); match target { - WritableTarget::Stderr => eprint!("{}", log), - WritableTarget::Stdout => print!("{}", log), + WritableTarget::WriteStdout => print!("{}", log), + WritableTarget::PrintStdout => print!("{}", log), + WritableTarget::WriteStderr => eprint!("{}", log), + WritableTarget::PrintStderr => eprint!("{}", log), WritableTarget::Pipe(pipe) => write!(pipe.lock().unwrap(), "{}", log)?, } diff --git a/src/fmt/writer/mod.rs b/src/fmt/writer/mod.rs index d3e7345..6764843 100644 --- a/src/fmt/writer/mod.rs +++ b/src/fmt/writer/mod.rs @@ -46,10 +46,16 @@ impl fmt::Debug for Target { /// /// Same as `Target`, except the pipe is wrapped in a mutex for interior mutability. pub(super) enum WritableTarget { - /// Logs will be sent to standard output. - Stdout, - /// Logs will be sent to standard error. - Stderr, + /// Logs will be written to standard output. + #[allow(dead_code)] + WriteStdout, + /// Logs will be printed to standard output. + PrintStdout, + /// Logs will be written to standard error. + #[allow(dead_code)] + WriteStderr, + /// Logs will be printed to standard error. + PrintStderr, /// Logs will be sent to a custom pipe. Pipe(Box>), } @@ -60,8 +66,10 @@ impl fmt::Debug for WritableTarget { f, "{}", match self { - Self::Stdout => "stdout", - Self::Stderr => "stderr", + Self::WriteStdout => "stdout", + Self::PrintStdout => "stdout", + Self::WriteStderr => "stderr", + Self::PrintStderr => "stderr", Self::Pipe(_) => "pipe", } )