Skip to main content

openvpn_mgmt_codec/
log_level.rs

1use std::str::FromStr;
2
3/// Error returned when a string is not a recognized log level.
4#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
5#[error("unrecognized log level: {0:?}")]
6pub struct ParseLogLevelError(pub String);
7
8/// Log severity level from `>LOG:` notifications.
9#[derive(Debug, Clone, PartialEq, Eq, strum::Display)]
10pub enum LogLevel {
11    /// Informational message (`I`).
12    #[strum(to_string = "I")]
13    Info,
14
15    /// Debug message (`D`).
16    #[strum(to_string = "D")]
17    Debug,
18
19    /// Warning (`W`).
20    #[strum(to_string = "W")]
21    Warning,
22
23    /// Non-fatal error (`N`).
24    #[strum(to_string = "N")]
25    NonFatal,
26
27    /// Fatal error (`F`).
28    #[strum(to_string = "F")]
29    Fatal,
30
31    /// An unrecognized log flag (forward compatibility).
32    #[strum(default)]
33    Unknown(String),
34}
35
36impl FromStr for LogLevel {
37    type Err = ParseLogLevelError;
38
39    /// Parse a recognized log-flag string: `I`, `D`, `W`, `N`, `F`.
40    fn from_str(s: &str) -> Result<Self, Self::Err> {
41        match s {
42            "I" => Ok(Self::Info),
43            "D" => Ok(Self::Debug),
44            "W" => Ok(Self::Warning),
45            "N" => Ok(Self::NonFatal),
46            "F" => Ok(Self::Fatal),
47            other => Err(ParseLogLevelError(other.to_string())),
48        }
49    }
50}