Skip to content

Commit

Permalink
refactor(fmt): Consolidate target printing
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Jan 17, 2024
1 parent f5f3392 commit e8674a2
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 37 deletions.
2 changes: 1 addition & 1 deletion src/fmt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
23 changes: 2 additions & 21 deletions src/fmt/writer/buffer/plain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand All @@ -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
}
}
17 changes: 2 additions & 15 deletions src/fmt/writer/buffer/termcolor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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()
}

Expand Down
22 changes: 22 additions & 0 deletions src/fmt/writer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,28 @@ pub(super) enum WritableTarget {
Pipe(Box<Mutex<dyn io::Write + Send + 'static>>),
}

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!(
Expand Down

0 comments on commit e8674a2

Please sign in to comment.