1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
use std::convert::TryFrom;
use std::str::FromStr;
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub struct NumericalLogLevel(pub u8);
impl FromStr for NumericalLogLevel {
type Err = anyhow::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
if let Ok(level) = s.parse::<u8>() {
Ok(NumericalLogLevel(level))
} else {
match s.parse::<NamedLogLevel>() {
Ok(level) => Ok(NumericalLogLevel(level as u8)),
Err(_) => Err(anyhow::anyhow!(format!("Invalid level value: '{}'", s))),
}
}
}
}
#[repr(u8)]
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum NamedLogLevel {
Fatal = 60,
Error = 50,
Warn = 40,
Info = 30,
Debug = 20,
Trace = 10,
}
impl FromStr for NamedLogLevel {
type Err = anyhow::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.to_lowercase().as_str() {
"trace" => Ok(NamedLogLevel::Trace),
"debug" => Ok(NamedLogLevel::Debug),
"info" => Ok(NamedLogLevel::Info),
"warn" => Ok(NamedLogLevel::Warn),
"error" => Ok(NamedLogLevel::Error),
"fatal" => Ok(NamedLogLevel::Fatal),
_ => Err(anyhow::anyhow!(format!("Invalid level value: '{}'", s))),
}
}
}
impl TryFrom<u8> for NamedLogLevel {
type Error = anyhow::Error;
fn try_from(value: u8) -> Result<Self, anyhow::Error> {
match value {
10 => Ok(NamedLogLevel::Trace),
20 => Ok(NamedLogLevel::Debug),
30 => Ok(NamedLogLevel::Info),
40 => Ok(NamedLogLevel::Warn),
50 => Ok(NamedLogLevel::Error),
60 => Ok(NamedLogLevel::Fatal),
v => Err(anyhow::anyhow!(format!(
"{} does not correspond to a valid named log level.",
v
))),
}
}
}