//! Different aggregators for output.
//!
//! An aggregator is something that "controls the order" of the output. Aggregators can either save
//! all items that they're and output them once the search is finished, or write them straight
//! to the output stream.
//!
//! Aggregators must be shareable across threads, as the search will be multi-threaded. This is why
//! 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;

pub trait Aggregator: Sync {
    fn push_item(&self, item: &LogResult, format: &dyn Format, stream: &mut dyn Write);
    fn finish(self, format: &dyn Format, stream: &mut dyn Write);
}

/// An aggregator that just pushes through each item to the output stream without any sorting or
/// whatsoever.
pub struct WriteThrough;

impl Aggregator for WriteThrough {
    fn push_item(&self, item: &LogResult, format: &dyn Format, stream: &mut dyn Write) {
        let text = format.format_result(item);
        stream.write_all(text.as_bytes()).unwrap();
        stream.flush().unwrap();
    }

    fn finish(self, _: &dyn Format, _: &mut dyn Write) {}
}