#[cfg(test)]
#[path = "stdin_tests.rs"]
mod stdin_tests;
use crate::traits::{LoaderInfo, LoaderType, LogLoader, Result};
use std::io::BufRead;
#[derive(Debug)]
pub struct StdinLoader {
info: LoaderInfo,
}
impl StdinLoader {
pub fn new() -> Self {
Self {
info: LoaderInfo {
id: "<stdin>".to_string(),
loader_type: LoaderType::TextFile,
multiline_enabled: false,
sample_lines: Vec::new(),
file_mod_year: None,
},
}
}
}
impl Default for StdinLoader {
fn default() -> Self {
Self::new()
}
}
impl LogLoader for StdinLoader {
fn info(&self) -> &LoaderInfo {
&self.info
}
fn load(&mut self) -> Result<Vec<String>> {
let stdin = std::io::stdin();
let reader = stdin.lock();
let lines: Vec<String> = reader.lines().collect::<std::result::Result<Vec<_>, _>>()?;
self.info.sample_lines = lines.iter().take(10).cloned().collect();
Ok(lines)
}
}