Skip to content

Commit

Permalink
Remove pipe for BufferWriter and implement handeling in Writer
Browse files Browse the repository at this point in the history
  • Loading branch information
TilCreator committed Jun 14, 2021
1 parent 13cafce commit fbeebcf
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 23 deletions.
16 changes: 11 additions & 5 deletions src/fmt/writer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ impl Default for WriteStyle {
pub(crate) struct Writer {
inner: BufferWriter,
write_style: WriteStyle,
target_pipe: Option<WritableTarget>,
}

impl Writer {
Expand All @@ -117,7 +118,11 @@ impl Writer {
}

pub(super) fn print(&self, buf: &Buffer) -> io::Result<()> {
self.inner.print(buf)
match &self.target_pipe {
Some(WritableTarget::Pipe(pipe)) => pipe.lock().unwrap().write_all(&buf.bytes()),
Some(_) => unreachable!(),
None => self.inner.print(buf)
}
}
}

Expand Down Expand Up @@ -190,15 +195,16 @@ impl Builder {
color_choice => color_choice,
};

let writer = match mem::take(&mut self.target) {
WritableTarget::Stderr => BufferWriter::stderr(self.is_test, color_choice),
WritableTarget::Stdout => BufferWriter::stdout(self.is_test, color_choice),
WritableTarget::Pipe(pipe) => BufferWriter::pipe(self.is_test, color_choice, pipe),
let (writer, target_pipe) = match mem::take(&mut self.target) {
WritableTarget::Stderr => (BufferWriter::stderr(self.is_test, color_choice), None),
WritableTarget::Stdout => (BufferWriter::stdout(self.is_test, color_choice), None),
WritableTarget::Pipe(pipe) => (BufferWriter::stderr(self.is_test, color_choice), Some(WritableTarget::Pipe(pipe))),
};

Writer {
inner: writer,
write_style: self.write_style,
target_pipe,
}
}
}
Expand Down
19 changes: 1 addition & 18 deletions src/fmt/writer/termcolor/extern_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use std::cell::RefCell;
use std::fmt;
use std::io::{self, Write};
use std::rc::Rc;
use std::sync::Mutex;

use log::Level;
use termcolor::{self, ColorChoice, ColorSpec, WriteColor};
Expand Down Expand Up @@ -102,22 +101,6 @@ impl BufferWriter {
}
}

pub(in crate::fmt::writer) fn pipe(
is_test: bool,
write_style: WriteStyle,
pipe: Box<Mutex<dyn io::Write + Send + 'static>>,
) -> Self {
BufferWriter {
// The inner Buffer is never printed from, but it is still needed to handle coloring and other formating
inner: termcolor::BufferWriter::stderr(write_style.into_color_choice()),
test_target: if is_test {
Some(WritableTarget::Pipe(pipe))
} else {
None
},
}
}

pub(in crate::fmt::writer) fn buffer(&self) -> Buffer {
Buffer {
inner: self.inner.buffer(),
Expand All @@ -135,7 +118,7 @@ impl BufferWriter {
match target {
WritableTarget::Stderr => eprint!("{}", log),
WritableTarget::Stdout => print!("{}", log),
WritableTarget::Pipe(pipe) => write!(pipe.lock().unwrap(), "{}", log)?,
WritableTarget::Pipe(_) => unreachable!(),
}

Ok(())
Expand Down

0 comments on commit fbeebcf

Please sign in to comment.