Skip to main content

fail2ban_log_parser_core/parser/
header.rs

1use std::fmt;
2
3use winnow::{
4    Parser,
5    ascii::Caseless,
6    combinator::{alt, opt, preceded},
7    error::StrContext,
8};
9
10#[derive(Debug, PartialEq, Clone)]
11#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
12pub enum Fail2BanHeaderType {
13    Filter,
14    Actions,
15    Server,
16}
17
18impl fmt::Display for Fail2BanHeaderType {
19    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
20        match self {
21            Self::Filter => write!(f, "Filter"),
22            Self::Actions => write!(f, "Actions"),
23            Self::Server => write!(f, "Server"),
24        }
25    }
26}
27
28pub(super) fn parse_header(input: &mut &str) -> winnow::Result<Option<Fail2BanHeaderType>> {
29    opt(preceded(Caseless("fail2ban."), parse_header_enum)).parse_next(input)
30}
31
32fn parse_header_enum(input: &mut &str) -> winnow::Result<Fail2BanHeaderType> {
33    alt((
34        Caseless("filter").value(Fail2BanHeaderType::Filter),
35        Caseless("actions").value(Fail2BanHeaderType::Actions),
36        Caseless("server").value(Fail2BanHeaderType::Server),
37    ))
38    .context(StrContext::Label(
39        "valid fail2ban section (filter, actions, or server)",
40    ))
41    .parse_next(input)
42}