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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
use std::ffi::OsStr;
use std::fmt::{self, Display, Formatter};
use url::Url;
use {Conditions, License, Limitations, Permissions};

/// The [MIT License](https://opensource.org/licenses/MIT).
#[derive(Clone, Debug, Hash, Eq, PartialEq, Ord, PartialOrd)]
pub struct Mit(pub(crate) String);

impl Mit {
    /// Returns a MIT license.
    #[inline]
    pub fn new(year: i32, name: &str) -> Mit {
        Mit(format!(include_str!("../files/MIT"), year = year, name = name))
    }
}

impl License for Mit {
    #[inline]
    fn name(&self) -> &'static str {
        "MIT License"
    }

    #[inline]
    fn id(&self) -> &'static str {
        "MIT"
    }

    #[inline]
    fn text(&self) -> &str {
        &self.0
    }

    #[inline]
    fn permissions(&self) -> Permissions {
        Permissions::COMMERCIAL_USE | Permissions::DISTRIBUTION | Permissions::MODIFICATION
            | Permissions::PRIVATE_USE
    }

    #[inline]
    fn conditions(&self) -> Conditions {
        Conditions::LICENSE_AND_COPYRIGHT_NOTICE
    }

    #[inline]
    fn limitations(&self) -> Limitations {
        Limitations::NO_LIABILITY | Limitations::NO_WARRANTY
    }

    #[inline]
    fn is_osi_approved(&self) -> bool {
        true
    }

    #[inline]
    fn is_fsf_free(&self) -> bool {
        true
    }

    #[inline]
    fn url(&self) -> &'static Url {
        lazy_static! {
            static ref URL: Url = match Url::parse("https://opensource.org/licenses/MIT") {
                Ok(url) => url,
                Err(_) => unreachable!(),
            };
        }
        &URL
    }
}

impl Default for Mit {
    #[inline]
    fn default() -> Self {
        Mit(format!(include_str!("../files/MIT"), year = "<year>", name = "<name>"))
    }
}

impl From<Mit> for Box<dyn License> {
    #[inline]
    fn from(mit: Mit) -> Self {
        Box::new(mit)
    }
}

impl Display for Mit {
    #[inline]
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        f.write_str(self.text())
    }
}

impl AsRef<str> for Mit {
    #[inline]
    fn as_ref(&self) -> &str {
        self.text()
    }
}

impl AsRef<OsStr> for Mit {
    #[inline]
    fn as_ref(&self) -> &OsStr {
        self.text().as_ref()
    }
}

impl AsRef<[u8]> for Mit {
    #[inline]
    fn as_ref(&self) -> &[u8] {
        self.text().as_ref()
    }
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn url() {
        let mit = Mit::default();
        assert_eq!(mit.url(), &Url::parse("https://opensource.org/licenses/MIT").unwrap());
    }
}