cli: do not abstract away underlying output stream at formatter layer

Since the concrete Formatter type is hidden behind the Ui, there wouldn't
be many reasons to use dyn Write at the formatter layer. This allows us
to create a formatter against MutexGuard<Box<dyn Write>> without one more
Box<dyn Write> wrapper.
This commit is contained in:
Yuya Nishihara 2022-10-07 20:37:51 +09:00
parent 885f1d04d1
commit 3392e83486

View file

@ -65,9 +65,9 @@ impl FormatterFactory {
FormatterFactory { kind } FormatterFactory { kind }
} }
pub fn new_formatter<'output>( pub fn new_formatter<'output, W: Write + 'output>(
&self, &self,
output: Box<dyn Write + 'output>, output: W,
) -> Box<dyn Formatter + 'output> { ) -> Box<dyn Formatter + 'output> {
match &self.kind { match &self.kind {
FormatterFactoryKind::PlainText => Box::new(PlainTextFormatter::new(output)), FormatterFactoryKind::PlainText => Box::new(PlainTextFormatter::new(output)),
@ -82,17 +82,17 @@ impl FormatterFactory {
} }
} }
pub struct PlainTextFormatter<'output> { pub struct PlainTextFormatter<W> {
output: Box<dyn Write + 'output>, output: W,
} }
impl<'output> PlainTextFormatter<'output> { impl<W> PlainTextFormatter<W> {
pub fn new(output: Box<dyn Write + 'output>) -> PlainTextFormatter<'output> { pub fn new(output: W) -> PlainTextFormatter<W> {
Self { output } Self { output }
} }
} }
impl Write for PlainTextFormatter<'_> { impl<W: Write> Write for PlainTextFormatter<W> {
fn write(&mut self, data: &[u8]) -> Result<usize, Error> { fn write(&mut self, data: &[u8]) -> Result<usize, Error> {
self.output.write(data) self.output.write(data)
} }
@ -102,7 +102,7 @@ impl Write for PlainTextFormatter<'_> {
} }
} }
impl Formatter for PlainTextFormatter<'_> { impl<W: Write> Formatter for PlainTextFormatter<W> {
fn add_label(&mut self, _label: &str) -> io::Result<()> { fn add_label(&mut self, _label: &str) -> io::Result<()> {
Ok(()) Ok(())
} }
@ -112,8 +112,8 @@ impl Formatter for PlainTextFormatter<'_> {
} }
} }
pub struct ColorFormatter<'output> { pub struct ColorFormatter<W> {
output: Box<dyn Write + 'output>, output: W,
colors: Arc<HashMap<String, String>>, colors: Arc<HashMap<String, String>>,
labels: Vec<String>, labels: Vec<String>,
cached_colors: HashMap<Vec<String>, Vec<u8>>, cached_colors: HashMap<Vec<String>, Vec<u8>>,
@ -248,11 +248,8 @@ fn config_colors(user_settings: &UserSettings) -> HashMap<String, String> {
result result
} }
impl<'output> ColorFormatter<'output> { impl<W> ColorFormatter<W> {
pub fn new( pub fn new(output: W, colors: Arc<HashMap<String, String>>) -> ColorFormatter<W> {
output: Box<dyn Write + 'output>,
colors: Arc<HashMap<String, String>>,
) -> ColorFormatter<'output> {
ColorFormatter { ColorFormatter {
output, output,
colors, colors,
@ -315,7 +312,7 @@ impl<'output> ColorFormatter<'output> {
} }
} }
impl Write for ColorFormatter<'_> { impl<W: Write> Write for ColorFormatter<W> {
fn write(&mut self, data: &[u8]) -> Result<usize, Error> { fn write(&mut self, data: &[u8]) -> Result<usize, Error> {
self.output.write(data) self.output.write(data)
} }
@ -325,7 +322,7 @@ impl Write for ColorFormatter<'_> {
} }
} }
impl Formatter for ColorFormatter<'_> { impl<W: Write> Formatter for ColorFormatter<W> {
fn add_label(&mut self, label: &str) -> io::Result<()> { fn add_label(&mut self, label: &str) -> io::Result<()> {
self.labels.push(label.to_owned()); self.labels.push(label.to_owned());
let new_color = self.current_color(); let new_color = self.current_color();