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
use failure::Fail;
use serde::{Deserialize, Serialize};
use std::fmt;

#[derive(Debug, Copy, Clone, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
#[serde(rename_all = "lowercase")]
pub enum Level {
    None,
    Low,
    Medium,
    High,
}

impl Default for Level {
    fn default() -> Self {
        Level::Medium
    }
}

impl fmt::Display for Level {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        use self::Level::*;
        f.write_str(match self {
            None => "none",
            Low => "low",
            Medium => "medium",
            High => "high",
        })
    }
}

#[derive(Fail, Debug)]
#[fail(display = "Can't convert string to Level")]
pub struct FromStrErr;

impl std::str::FromStr for Level {
    type Err = FromStrErr;

    fn from_str(s: &str) -> std::result::Result<Level, FromStrErr> {
        Ok(match s {
            "none" => Level::None,
            "low" => Level::Low,
            "medium" => Level::Medium,
            "high" => Level::High,
            _ => return Err(FromStrErr),
        })
    }
}