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
use std::fmt;

#[derive(Debug, Clone, PartialEq)]
pub enum License {
    Mit,
    Apache2,
    Gplv3,
    Bsd,
    Other(String),
}

impl From<String> for License {
    fn from(s: String) -> License {
        License::from(s.as_str())
    }
}

impl<'a> From<&'a str> for License {
    fn from(s: &'a str) -> License {
        match s.to_lowercase().as_str() {
            "mit" => License::Mit,
            "apache2" => License::Apache2,
            "gpl" => License::Gplv3,
            "gplv3" => License::Gplv3,
            "bsd" => License::Bsd,
            s => License::Other(s.to_string()),
        }
    }
}

impl fmt::Display for License {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", match self {
            &License::Mit => "MIT",
            &License::Apache2 => "Apache-2.0",
            &License::Gplv3 => "GPLv3",
            &License::Bsd => "BSD",
            &License::Other(ref s) => s,
        })
    }
}