use super::{Chunker, EventLimiter, OutputWriter, WindowManager};
use crate::event::Event;
use std::collections::VecDeque;
pub const DEFAULT_MULTILINE_FLUSH_TIMEOUT_MS: u64 = 400;
pub struct SimpleChunker;
impl Chunker for SimpleChunker {
fn feed_line(&mut self, line: String) -> Option<String> {
Some(line)
}
fn flush(&mut self) -> Option<String> {
None
}
fn has_pending(&self) -> bool {
false
}
}
pub struct SimpleWindowManager {
current: Option<Event>,
}
impl Default for SimpleWindowManager {
fn default() -> Self {
Self::new()
}
}
impl SimpleWindowManager {
pub fn new() -> Self {
Self { current: None }
}
}
impl WindowManager for SimpleWindowManager {
fn get_window(&self) -> Vec<Event> {
if let Some(ref event) = self.current {
vec![event.clone()]
} else {
Vec::new()
}
}
fn update(&mut self, current: &Event) {
self.current = Some(current.clone());
}
}
pub struct StdoutWriter;
impl OutputWriter for StdoutWriter {
fn write(&mut self, line: &str) -> std::io::Result<()> {
println!("{}", line);
Ok(())
}
fn flush(&mut self) -> std::io::Result<()> {
use std::io::Write;
std::io::stdout().flush()
}
}
pub struct TakeNLimiter {
remaining: usize,
}
impl TakeNLimiter {
pub fn new(limit: usize) -> Self {
Self { remaining: limit }
}
}
impl EventLimiter for TakeNLimiter {
fn allow(&mut self) -> bool {
if self.remaining > 0 {
self.remaining -= 1;
true
} else {
false
}
}
fn is_exhausted(&self) -> bool {
self.remaining == 0
}
}
pub struct SlidingWindowManager {
window_size: usize,
buffer: VecDeque<Event>,
}
impl SlidingWindowManager {
pub fn new(window_size: usize) -> Self {
Self {
window_size,
buffer: VecDeque::with_capacity(window_size + 1),
}
}
}
impl WindowManager for SlidingWindowManager {
fn get_window(&self) -> Vec<Event> {
self.buffer.iter().cloned().collect()
}
fn update(&mut self, current: &Event) {
self.buffer.push_front(current.clone());
while self.buffer.len() > self.window_size + 1 {
self.buffer.pop_back();
}
}
}