use std::path::Path;
use thiserror::Error;
use crate::logs::asynchronous::log_dir_reader::LogDirReaderError;
use crate::logs::content::LogEvent;
use super::{LogFileReaderError, RawLiveLogDirReader};
#[derive(Debug)]
pub struct LiveLogDirReader {
inner: RawLiveLogDirReader,
}
#[derive(Debug, Error)]
pub enum LiveLogDirReaderError {
#[error(transparent)]
LogDirReaderError(#[from] LogDirReaderError),
#[error(transparent)]
NotifyError(#[from] notify::Error),
}
impl LiveLogDirReader {
pub fn open<P: AsRef<Path>>(dir_path: P) -> Result<LiveLogDirReader, LiveLogDirReaderError> {
Ok(LiveLogDirReader {
inner: RawLiveLogDirReader::open(dir_path)?,
})
}
pub async fn next(&mut self) -> Option<Result<LogEvent, LiveLogDirReaderError>> {
let result = match self.inner.next().await? {
Ok(x) => x,
Err(e) => return Some(Err(e)),
};
Some(serde_json::from_value(result).map_err(|e| {
LiveLogDirReaderError::LogDirReaderError(
LogFileReaderError::FailedToParseLine(e).into(),
)
}))
}
}