Skip to main content

fail2ban_log_parser_core/parser/
level.rs

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