Skip to content

Commit

Permalink
refactor(fmt): Pull is_test into the target
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Jan 17, 2024
1 parent 87008fd commit f5f3392
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 23 deletions.
30 changes: 17 additions & 13 deletions src/fmt/writer/buffer/plain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Mutex<dyn io::Write + Send + 'static>>) -> Self {
BufferWriter {
target: WritableTarget::Pipe(pipe),
is_test: false,
}
}

Expand All @@ -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(())
Expand Down
10 changes: 6 additions & 4 deletions src/fmt/writer/buffer/termcolor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
Expand All @@ -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
},
Expand Down Expand Up @@ -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)?,
}

Expand Down
20 changes: 14 additions & 6 deletions src/fmt/writer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Mutex<dyn io::Write + Send + 'static>>),
}
Expand All @@ -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",
}
)
Expand Down

0 comments on commit f5f3392

Please sign in to comment.