diff --git a/src/fmt/mod.rs b/src/fmt/mod.rs index 30b1384..5f1d5c8 100644 --- a/src/fmt/mod.rs +++ b/src/fmt/mod.rs @@ -468,7 +468,7 @@ mod tests { fmt.write(&record).expect("failed to write record"); let buf = buf.borrow(); - String::from_utf8(buf.bytes().to_vec()).expect("failed to read record") + String::from_utf8(buf.as_bytes().to_vec()).expect("failed to read record") } fn write_target(target: &str, fmt: DefaultFormat) -> String { diff --git a/src/fmt/writer/buffer/plain.rs b/src/fmt/writer/buffer/plain.rs index 0ebd653..e6809b0 100644 --- a/src/fmt/writer/buffer/plain.rs +++ b/src/fmt/writer/buffer/plain.rs @@ -42,25 +42,7 @@ impl BufferWriter { } pub(in crate::fmt::writer) fn print(&self, buf: &Buffer) -> io::Result<()> { - use std::io::Write as _; - - // 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 { - WritableTarget::WriteStdout => { - write!(std::io::stdout(), "{}", String::from_utf8_lossy(&buf.0))? - } - 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(()) + self.target.print(buf) } } @@ -80,8 +62,7 @@ impl Buffer { Ok(()) } - #[cfg(test)] - pub(in crate::fmt) fn bytes(&self) -> &[u8] { + pub(in crate::fmt) fn as_bytes(&self) -> &[u8] { &self.0 } } diff --git a/src/fmt/writer/buffer/termcolor.rs b/src/fmt/writer/buffer/termcolor.rs index f854ae6..d3090a1 100644 --- a/src/fmt/writer/buffer/termcolor.rs +++ b/src/fmt/writer/buffer/termcolor.rs @@ -59,20 +59,7 @@ impl BufferWriter { pub(in crate::fmt::writer) fn print(&self, buf: &Buffer) -> io::Result<()> { if let Some(target) = &self.uncolored_target { - // This impl uses the `eprint` and `print` macros - // instead of `termcolor`'s buffer. - // This is so their output can be captured by `cargo test` - let log = String::from_utf8_lossy(buf.bytes()); - - match target { - WritableTarget::WriteStdout => print!("{}", log), - WritableTarget::PrintStdout => print!("{}", log), - WritableTarget::WriteStderr => eprint!("{}", log), - WritableTarget::PrintStderr => eprint!("{}", log), - WritableTarget::Pipe(pipe) => write!(pipe.lock().unwrap(), "{}", log)?, - } - - Ok(()) + target.print(buf) } else { self.inner.print(&buf.inner) } @@ -97,7 +84,7 @@ impl Buffer { self.inner.flush() } - pub(in crate::fmt) fn bytes(&self) -> &[u8] { + pub(in crate::fmt) fn as_bytes(&self) -> &[u8] { self.inner.as_slice() } diff --git a/src/fmt/writer/mod.rs b/src/fmt/writer/mod.rs index 6764843..8dafd26 100644 --- a/src/fmt/writer/mod.rs +++ b/src/fmt/writer/mod.rs @@ -60,6 +60,28 @@ pub(super) enum WritableTarget { Pipe(Box>), } +impl WritableTarget { + fn print(&self, buf: &Buffer) -> io::Result<()> { + use std::io::Write as _; + + let buf = buf.as_bytes(); + match self { + WritableTarget::WriteStdout => { + write!(std::io::stdout(), "{}", String::from_utf8_lossy(buf))? + } + WritableTarget::PrintStdout => print!("{}", String::from_utf8_lossy(buf)), + WritableTarget::WriteStderr => { + write!(std::io::stderr(), "{}", String::from_utf8_lossy(buf))? + } + WritableTarget::PrintStderr => eprint!("{}", String::from_utf8_lossy(buf)), + // Safety: If the target type is `Pipe`, `target_pipe` will always be non-empty. + WritableTarget::Pipe(pipe) => pipe.lock().unwrap().write_all(buf)?, + } + + Ok(()) + } +} + impl fmt::Debug for WritableTarget { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(