fail2ban_log_parser_core/
lib.rs1#![warn(clippy::pedantic)]
2
3#[cfg(all(feature = "parallel", target_arch = "wasm32"))]
4compile_error!(
5 "The `parallel` feature is not supported on wasm32 targets. Rayon requires OS threads which are not available in WASM."
6);
7
8pub use crate::parser::{Fail2BanEvent, Fail2BanHeaderType, Fail2BanLevel, Fail2BanStructuredLog};
9use std::fmt;
10
11#[cfg(feature = "parallel")]
12use rayon::prelude::*;
13
14mod parser;
15
16#[derive(Debug, Clone)]
18pub struct ParseError {
19 pub line_number: usize,
20 pub line: String,
21}
22
23impl fmt::Display for ParseError {
24 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
25 write!(
26 f,
27 "failed to parse line {}: {}",
28 self.line_number, self.line
29 )
30 }
31}
32
33impl std::error::Error for ParseError {}
34
35#[cfg(not(feature = "parallel"))]
64pub fn parse(
65 input: &str,
66) -> impl Iterator<Item = Result<Fail2BanStructuredLog<'_>, ParseError>> + '_ {
67 input.lines().enumerate().map(|(i, line)| {
68 parser::parse_log_line(&mut &*line).map_err(|_| ParseError {
69 line_number: i + 1,
70 line: line.to_string(),
71 })
72 })
73}
74
75#[cfg(feature = "parallel")]
80pub fn parse(input: &str) -> impl Iterator<Item = Result<Fail2BanStructuredLog<'_>, ParseError>> {
81 let lines: Vec<&str> = input.lines().collect();
82 lines
83 .par_iter()
84 .enumerate()
85 .map(|(i, &line)| {
86 parser::parse_log_line(&mut &*line).map_err(|_| ParseError {
87 line_number: i + 1,
88 line: line.to_string(),
89 })
90 })
91 .collect::<Vec<_>>()
92 .into_iter()
93}