Skip to main content

openvpn_mgmt_codec/
log_level.rs

1use std::fmt;
2
3/// Log severity level from `>LOG:` notifications.
4#[derive(Debug, Clone, PartialEq, Eq)]
5pub enum LogLevel {
6    /// Informational message (`I`).
7    Info,
8
9    /// Debug message (`D`).
10    Debug,
11
12    /// Warning (`W`).
13    Warning,
14
15    /// Non-fatal error (`N`).
16    NonFatal,
17
18    /// Fatal error (`F`).
19    Fatal,
20
21    /// An unrecognized log flag (forward compatibility).
22    Custom(String),
23}
24
25impl LogLevel {
26    /// Parse a wire log-flag string into a typed variant.
27    pub(crate) fn parse(s: &str) -> Self {
28        match s {
29            "I" => Self::Info,
30            "D" => Self::Debug,
31            "W" => Self::Warning,
32            "N" => Self::NonFatal,
33            "F" => Self::Fatal,
34            other => Self::Custom(other.to_owned()),
35        }
36    }
37}
38
39impl fmt::Display for LogLevel {
40    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
41        match self {
42            Self::Info => f.write_str("I"),
43            Self::Debug => f.write_str("D"),
44            Self::Warning => f.write_str("W"),
45            Self::NonFatal => f.write_str("N"),
46            Self::Fatal => f.write_str("F"),
47            Self::Custom(s) => f.write_str(s),
48        }
49    }
50}