#![warn(clippy::pedantic)]
#[cfg(all(feature = "parallel", target_arch = "wasm32"))]
compile_error!(
"The `parallel` feature is not supported on wasm32 targets. Rayon requires OS threads which are not available in WASM."
);
pub use crate::parser::{Fail2BanEvent, Fail2BanHeaderType, Fail2BanLevel, Fail2BanStructuredLog};
use std::fmt;
#[cfg(feature = "parallel")]
use rayon::prelude::*;
mod parser;
#[derive(Debug, Clone)]
pub struct ParseError {
pub line_number: usize,
pub line: String,
}
impl fmt::Display for ParseError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"failed to parse line {}: {}",
self.line_number, self.line
)
}
}
impl std::error::Error for ParseError {}
#[cfg(not(feature = "parallel"))]
pub fn parse(
input: &str,
) -> impl Iterator<Item = Result<Fail2BanStructuredLog<'_>, ParseError>> + '_ {
input.lines().enumerate().map(|(i, line)| {
parser::parse_log_line(&mut &*line).map_err(|_| ParseError {
line_number: i + 1,
line: line.to_string(),
})
})
}
#[cfg(feature = "parallel")]
pub fn parse(input: &str) -> impl Iterator<Item = Result<Fail2BanStructuredLog<'_>, ParseError>> {
let lines: Vec<&str> = input.lines().collect();
lines
.par_iter()
.enumerate()
.map(|(i, &line)| {
parser::parse_log_line(&mut &*line).map_err(|_| ParseError {
line_number: i + 1,
line: line.to_string(),
})
})
.collect::<Vec<_>>()
.into_iter()
}