From c748642f3b801aa5ff4044ab60b0d0c29037a802 Mon Sep 17 00:00:00 2001 From: Daniel Date: Sat, 4 Apr 2020 14:13:59 +0200 Subject: make pipeline working --- src/output/aggregators.rs | 12 ++++++------ src/output/formats.rs | 7 ------- src/output/mod.rs | 14 +++++--------- src/output/pipeline.rs | 6 +++--- 4 files changed, 14 insertions(+), 25 deletions(-) (limited to 'src/output') diff --git a/src/output/aggregators.rs b/src/output/aggregators.rs index 9934fb3..5d0429c 100644 --- a/src/output/aggregators.rs +++ b/src/output/aggregators.rs @@ -8,11 +8,11 @@ //! an Aggregator must make sure that the data is protected by a mutex or similar. use super::{super::LogResult, formats::Format}; -use std::{io::Write, sync::Mutex}; +use std::io::Write; pub trait Aggregator: Sync { - fn push_item(&self, item: &LogResult, format: &Format, stream: &mut Write); - fn finish(self, format: &Format, stream: &mut Write); + fn push_item(&self, item: &LogResult, format: &dyn Format, stream: &mut dyn Write); + fn finish(self, format: &dyn Format, stream: &mut dyn Write); } @@ -22,11 +22,11 @@ pub struct WriteThrough; impl Aggregator for WriteThrough { - fn push_item(&self, item: &LogResult, format: &Format, stream: &mut Write) { + fn push_item(&self, item: &LogResult, format: &dyn Format, stream: &mut dyn Write) { let text = format.format_result(item); - println!("Aggregator::push_item {:?}", text); stream.write_all(text.as_bytes()).unwrap(); + stream.flush().unwrap(); } - fn finish(self, format: &Format, stream: &mut Write) {} + fn finish(self, _: &dyn Format, _: &mut dyn Write) {} } diff --git a/src/output/formats.rs b/src/output/formats.rs index fe6e982..a5171a8 100644 --- a/src/output/formats.rs +++ b/src/output/formats.rs @@ -10,13 +10,6 @@ pub trait Format: Sync + Send { } -impl Format for Box { - fn format_result(&self, item: &LogResult) -> String { - (&*self).format_result(item) - } -} - - /// The human readable, colored format. #[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)] pub struct HumanReadable; diff --git a/src/output/mod.rs b/src/output/mod.rs index dfb3ea8..73af5ab 100644 --- a/src/output/mod.rs +++ b/src/output/mod.rs @@ -1,4 +1,3 @@ -use super::errors::RuntimeError; use super::{FightOutcome, LogResult, Opt}; use std::io; @@ -7,20 +6,17 @@ pub mod formats; pub mod aggregators; pub mod pipeline; -use self::formats::Format; -use self::aggregators::Aggregator; pub use self::pipeline::Pipeline; /// Build an pipeline for the given command line options. pub fn build_pipeline(opt: &Opt) -> Pipeline { let stream = io::stdout(); + let aggregator = aggregators::WriteThrough; - let formatter: Box = if opt.file_name_only { - Box::new(formats::FileOnly) + if opt.file_name_only { + Pipeline::new(stream, formats::FileOnly, aggregator) } else { - Box::new(formats::HumanReadable) - }; - - Pipeline::new(stream, formatter, aggregators::WriteThrough) + Pipeline::new(stream, formats::HumanReadable, aggregator) + } } diff --git a/src/output/pipeline.rs b/src/output/pipeline.rs index 9b3c7e5..fb23a6f 100644 --- a/src/output/pipeline.rs +++ b/src/output/pipeline.rs @@ -7,12 +7,12 @@ use std::{io::Write, sync::Mutex}; pub struct Pipeline { format: Box, aggregator: Box, - writer: Mutex>, + writer: Mutex>, } impl Pipeline { - pub fn new(writer: W, format: F, aggregator: A) -> Pipeline { + pub fn new(writer: W, format: F, aggregator: A) -> Pipeline { Pipeline { format: Box::new(format), aggregator: Box::new(aggregator), @@ -22,6 +22,6 @@ impl Pipeline { pub fn push_item(&self, item: &LogResult) { let mut writer = self.writer.lock().unwrap(); - self.aggregator.push_item(item, &self.format, &mut *writer); + self.aggregator.push_item(item, &*self.format, &mut *writer); } } -- cgit v1.2.3