Skip to main content

fail2ban_log_parser_core/parser/
event.rs

1use winnow::{
2    Parser,
3    ascii::Caseless,
4    combinator::{alt, opt},
5    error::StrContext,
6};
7
8#[derive(Debug, Clone, PartialEq)]
9#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
10pub enum Fail2BanEvent {
11    Found,
12    Ban,
13    Unban,
14    Restore,
15    Ignore,
16    AlreadyBanned,
17    Failed,
18    Unknown,
19}
20
21impl std::fmt::Display for Fail2BanEvent {
22    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
23        match self {
24            Self::Found => write!(f, "Found"),
25            Self::Ban => write!(f, "Ban"),
26            Self::Unban => write!(f, "Unban"),
27            Self::Restore => write!(f, "Restore"),
28            Self::Ignore => write!(f, "Ignore"),
29            Self::AlreadyBanned => write!(f, "AlreadyBanned"),
30            Self::Failed => write!(f, "Failed"),
31            Self::Unknown => write!(f, "Unknown"),
32        }
33    }
34}
35
36pub(super) fn parse_event(input: &mut &str) -> winnow::Result<Option<Fail2BanEvent>> {
37    opt(alt((
38        Caseless("found").value(Fail2BanEvent::Found),
39        Caseless("ban").value(Fail2BanEvent::Ban),
40        Caseless("unban").value(Fail2BanEvent::Unban),
41        Caseless("restore").value(Fail2BanEvent::Restore),
42        Caseless("ignore").value(Fail2BanEvent::Ignore),
43        Caseless("alreadybanned").value(Fail2BanEvent::AlreadyBanned),
44        Caseless("failed").value(Fail2BanEvent::Failed),
45        Caseless("unknown").value(Fail2BanEvent::Unknown),
46    ))
47    .context(StrContext::Label(
48        "valid log level (info, notice, warning, error, or debug)",
49    )))
50    .parse_next(input)
51}