[go: up one dir, main page]

  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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
use std::str::FromStr;
use std::fmt::{self, Display, Formatter};
use std::ffi::OsStr;

use aho_corasick::{Automaton, AcAutomaton, Dense};

use {Agpl3, Apache2, Cc01, Gpl3, Lgpl3, License, Mit, Mpl2, Unknown, Unlicense};
use conditions::*;
use limitations::*;
use permissions::*;

/// Enum containing all supported licenses.
///
/// # Examples
/// ```
/// use std::str::FromStr;
/// use license::{Apache2, Gpl3, LicenseKind};
///
/// // From text:
/// let apache2 = LicenseKind::from_str(include_str!("files/APACHE-2"));
/// assert_eq!(apache2, Ok(LicenseKind::Apache2(Apache2)));
///
/// // From identity:
/// let gpl3 = LicenseKind::from_str("GPL-3.0");
/// assert_eq!(gpl3, Ok(LicenseKind::Gpl3(Gpl3)));
/// ```
#[derive(Clone, Debug, Eq, PartialEq, Hash, Ord, PartialOrd)]
pub enum LicenseKind {
    Agpl3(Agpl3),
    Apache2(Apache2),
    Cc01(Cc01),
    Gpl3(Gpl3),
    Lgpl3(Lgpl3),
    Mit(Mit),
    Mpl2(Mpl2),
    Unlicense(Unlicense),
}

impl License for LicenseKind {
    #[inline]
    fn name(&self) -> &str {
        match *self {
            LicenseKind::Agpl3(ref agpl3) => agpl3.name(),
            LicenseKind::Apache2(ref apache2) => apache2.name(),
            LicenseKind::Cc01(ref cc01) => cc01.name(),
            LicenseKind::Gpl3(ref gpl3) => gpl3.name(),
            LicenseKind::Lgpl3(ref lgpl3) => lgpl3.name(),
            LicenseKind::Mit(ref mit) => mit.name(),
            LicenseKind::Mpl2(ref mpl2) => mpl2.name(),
            LicenseKind::Unlicense(ref unlicense) => unlicense.name(),
        }
    }

    #[inline]
    fn id(&self) -> &str {
        match *self {
            LicenseKind::Agpl3(ref agpl3) => agpl3.id(),
            LicenseKind::Apache2(ref apache2) => apache2.id(),
            LicenseKind::Cc01(ref cc01) => cc01.id(),
            LicenseKind::Gpl3(ref gpl3) => gpl3.id(),
            LicenseKind::Lgpl3(ref lgpl3) => lgpl3.id(),
            LicenseKind::Mit(ref mit) => mit.id(),
            LicenseKind::Mpl2(ref mpl2) => mpl2.id(),
            LicenseKind::Unlicense(ref unlicense) => unlicense.id(),
        }
    }

    #[inline]
    fn is_osi_approved(&self) -> bool {
        match *self {
            LicenseKind::Agpl3(agpl3) => agpl3.is_osi_approved(),
            LicenseKind::Apache2(apache2) => apache2.is_osi_approved(),
            LicenseKind::Cc01(cc01) => cc01.is_osi_approved(),
            LicenseKind::Gpl3(gpl3) => gpl3.is_osi_approved(),
            LicenseKind::Lgpl3(lgpl3) => lgpl3.is_osi_approved(),
            LicenseKind::Mit(ref mit) => mit.is_osi_approved(),
            LicenseKind::Mpl2(mpl2) => mpl2.is_osi_approved(),
            LicenseKind::Unlicense(unlicense) => unlicense.is_osi_approved(),
        }
    }

    #[inline]
    fn text(&self) -> &str {
        match *self {
            LicenseKind::Agpl3(ref agpl3) => agpl3.text(),
            LicenseKind::Apache2(ref apache2) => apache2.text(),
            LicenseKind::Cc01(ref cc01) => cc01.text(),
            LicenseKind::Gpl3(ref gpl3) => gpl3.text(),
            LicenseKind::Lgpl3(ref lgpl3) => lgpl3.text(),
            LicenseKind::Mit(ref mit) => mit.text(),
            LicenseKind::Mpl2(ref mpl2) => mpl2.text(),
            LicenseKind::Unlicense(ref unlicense) => unlicense.text(),
        }
    }

    #[inline]
    fn permissions(&self) -> Permissions {
        match *self {
            LicenseKind::Agpl3(agpl3) => agpl3.permissions(),
            LicenseKind::Apache2(apache2) => apache2.permissions(),
            LicenseKind::Cc01(cc01) => cc01.permissions(),
            LicenseKind::Gpl3(gpl3) => gpl3.permissions(),
            LicenseKind::Lgpl3(lgpl3) => lgpl3.permissions(),
            LicenseKind::Mit(ref mit) => mit.permissions(),
            LicenseKind::Mpl2(mpl2) => mpl2.permissions(),
            LicenseKind::Unlicense(unlicense) => unlicense.permissions(),
        }
    }

    #[inline]
    fn conditions(&self) -> Conditions {
        match *self {
            LicenseKind::Agpl3(agpl3) => agpl3.conditions(),
            LicenseKind::Apache2(apache2) => apache2.conditions(),
            LicenseKind::Cc01(cc01) => cc01.conditions(),
            LicenseKind::Gpl3(gpl3) => gpl3.conditions(),
            LicenseKind::Lgpl3(lgpl3) => lgpl3.conditions(),
            LicenseKind::Mit(ref mit) => mit.conditions(),
            LicenseKind::Mpl2(mpl2) => mpl2.conditions(),
            LicenseKind::Unlicense(unlicense) => unlicense.conditions(),
        }
    }

    #[inline]
    fn limitations(&self) -> Limitations {
        match *self {
            LicenseKind::Agpl3(agpl3) => agpl3.limitations(),
            LicenseKind::Apache2(apache2) => apache2.limitations(),
            LicenseKind::Cc01(cc01) => cc01.limitations(),
            LicenseKind::Gpl3(gpl3) => gpl3.limitations(),
            LicenseKind::Lgpl3(lgpl3) => lgpl3.limitations(),
            LicenseKind::Mit(ref mit) => mit.limitations(),
            LicenseKind::Mpl2(mpl2) => mpl2.limitations(),
            LicenseKind::Unlicense(unlicense) => unlicense.limitations(),
        }
    }

    #[inline]
    fn homepage(&self) -> Option<&str> {
        match *self {
            LicenseKind::Agpl3(ref agpl3) => agpl3.homepage(),
            LicenseKind::Apache2(ref apache2) => apache2.homepage(),
            LicenseKind::Cc01(ref cc01) => cc01.homepage(),
            LicenseKind::Gpl3(ref gpl3) => gpl3.homepage(),
            LicenseKind::Lgpl3(ref lgpl3) => lgpl3.homepage(),
            LicenseKind::Mit(ref mit) => mit.homepage(),
            LicenseKind::Mpl2(ref mpl2) => mpl2.homepage(),
            LicenseKind::Unlicense(ref unlicense) => unlicense.homepage(),
        }
    }
}

impl FromStr for LicenseKind {
    type Err = Unknown;

    #[inline]
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "AGPL-3.0" => Ok(LicenseKind::Agpl3(Agpl3)),
            "Apache-2.0" => Ok(LicenseKind::Apache2(Apache2)),
            "CC0-1.0" => Ok(LicenseKind::Cc01(Cc01)),
            "GPL-3.0" => Ok(LicenseKind::Gpl3(Gpl3)),
            "LGPL-3.0" => Ok(LicenseKind::Lgpl3(Lgpl3)),
            "MIT" => Ok(LicenseKind::Mit(Mit::default())),
            "MPL-2.0" => Ok(LicenseKind::Mpl2(Mpl2)),
            "Unlicense" => Ok(LicenseKind::Unlicense(Unlicense)),
            s => {
                lazy_static! {
                    static ref AHO: AcAutomaton<&'static &'static str, Dense> = {
                        const PATS: &[&str] = &[
                            "MIT License",                                                              // 0
                            "Apache License",                                                           // 1
                            "Version 2.0",                                                              // 2
                            "GNU GENERAL PUBLIC LICENSE",                                               // 3
                            "Version 3",                                                                // 4
                            "Mozilla Public License",                                                   // 5
                            "This is free and unencumbered software released into the public domain.",  // 6
                            "GNU LESSER GENERAL PUBLIC LICENSE",                                        // 7
                            "GNU AFFERO GENERAL PUBLIC LICENSE",                                        // 8
                            "CC0 1.0 Universal",                                                        // 9
                        ];
                        AcAutomaton::new(PATS)
                    };
                }
                let mut it = AHO.find(s);
                match it.next() {
                    Some(m) => {
                        match m.pati {
                            0 => Ok(LicenseKind::Mit(Mit(s.to_string()))),
                            1 => {
                                match it.next() {
                                    Some(m) if m.pati == 2 => Ok(LicenseKind::Apache2(Apache2)),
                                    _ => Err(Unknown),
                                }
                            }
                            3 => {
                                match it.next() {
                                    Some(m) if m.pati == 4 => Ok(LicenseKind::Gpl3(Gpl3)),
                                    _ => Err(Unknown),
                                }
                            }
                            5 => {
                                match it.next() {
                                    Some(m) if m.pati == 2 => Ok(LicenseKind::Mpl2(Mpl2)),
                                    _ => Err(Unknown),
                                }
                            }
                            6 => Ok(LicenseKind::Unlicense(Unlicense)),
                            7 => {
                                match it.next() {
                                    Some(m) if m.pati == 4 => Ok(LicenseKind::Lgpl3(Lgpl3)),
                                    _ => Err(Unknown),
                                }
                            }
                            8 => {
                                match it.next() {
                                    Some(m) if m.pati == 4 => Ok(LicenseKind::Agpl3(Agpl3)),
                                    _ => Err(Unknown),
                                }
                            }
                            9 => Ok(LicenseKind::Cc01(Cc01)),
                            _ => Err(Unknown),
                        }
                    }
                    None => Err(Unknown),
                }
            }
        }
    }
}

impl Display for LicenseKind {
    #[inline]
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        match *self {
            LicenseKind::Agpl3(agpl3) => write!(f, "{}", agpl3),
            LicenseKind::Apache2(apache2) => write!(f, "{}", apache2),
            LicenseKind::Cc01(cc01) => write!(f, "{}", cc01),
            LicenseKind::Gpl3(gpl3) => write!(f, "{}", gpl3),
            LicenseKind::Lgpl3(lgpl3) => write!(f, "{}", lgpl3),
            LicenseKind::Mit(ref mit) => write!(f, "{}", mit),
            LicenseKind::Mpl2(mpl2) => write!(f, "{}", mpl2),
            LicenseKind::Unlicense(unlicense) => write!(f, "{}", unlicense),
        }
    }
}

impl AsRef<str> for LicenseKind {
    #[inline]
    fn as_ref(&self) -> &str {
        match *self {
            LicenseKind::Agpl3(ref agpl3) => agpl3.as_ref(),
            LicenseKind::Apache2(ref apache2) => apache2.as_ref(),
            LicenseKind::Cc01(ref cc01) => cc01.as_ref(),
            LicenseKind::Gpl3(ref gpl3) => gpl3.as_ref(),
            LicenseKind::Lgpl3(ref lgpl3) => lgpl3.as_ref(),
            LicenseKind::Mit(ref mit) => mit.as_ref(),
            LicenseKind::Mpl2(ref mpl2) => mpl2.as_ref(),
            LicenseKind::Unlicense(ref unlicense) => unlicense.as_ref(),
        }
    }
}

impl AsRef<OsStr> for LicenseKind {
    #[inline]
    fn as_ref(&self) -> &OsStr {
        match *self {
            LicenseKind::Agpl3(ref agpl3) => agpl3.as_ref(),
            LicenseKind::Apache2(ref apache2) => apache2.as_ref(),
            LicenseKind::Cc01(ref cc01) => cc01.as_ref(),
            LicenseKind::Gpl3(ref gpl3) => gpl3.as_ref(),
            LicenseKind::Lgpl3(ref lgpl3) => lgpl3.as_ref(),
            LicenseKind::Mit(ref mit) => mit.as_ref(),
            LicenseKind::Mpl2(ref mpl2) => mpl2.as_ref(),
            LicenseKind::Unlicense(ref unlicense) => unlicense.as_ref(),
        }
    }
}

impl AsRef<[u8]> for LicenseKind {
    #[inline]
    fn as_ref(&self) -> &[u8] {
        match *self {
            LicenseKind::Agpl3(ref agpl3) => agpl3.as_ref(),
            LicenseKind::Apache2(ref apache2) => apache2.as_ref(),
            LicenseKind::Cc01(ref cc01) => cc01.as_ref(),
            LicenseKind::Gpl3(ref gpl3) => gpl3.as_ref(),
            LicenseKind::Lgpl3(ref lgpl3) => lgpl3.as_ref(),
            LicenseKind::Mit(ref mit) => mit.as_ref(),
            LicenseKind::Mpl2(ref mpl2) => mpl2.as_ref(),
            LicenseKind::Unlicense(ref unlicense) => unlicense.as_ref(),
        }
    }
}

impl From<Agpl3> for LicenseKind {
    #[inline]
    fn from(agpl3: Agpl3) -> Self {
        LicenseKind::Agpl3(agpl3)
    }
}

impl From<Apache2> for LicenseKind {
    #[inline]
    fn from(apache2: Apache2) -> Self {
        LicenseKind::Apache2(apache2)
    }
}

impl From<Cc01> for LicenseKind {
    #[inline]
    fn from(cc01: Cc01) -> Self {
        LicenseKind::Cc01(cc01)
    }
}

impl From<Gpl3> for LicenseKind {
    #[inline]
    fn from(gpl3: Gpl3) -> Self {
        LicenseKind::Gpl3(gpl3)
    }
}

impl From<Lgpl3> for LicenseKind {
    #[inline]
    fn from(lgpl3: Lgpl3) -> Self {
        LicenseKind::Lgpl3(lgpl3)
    }
}

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

impl From<Mpl2> for LicenseKind {
    #[inline]
    fn from(mpl2: Mpl2) -> Self {
        LicenseKind::Mpl2(mpl2)
    }
}

impl From<Unlicense> for LicenseKind {
    #[inline]
    fn from(unlicense: Unlicense) -> Self {
        LicenseKind::Unlicense(unlicense)
    }
}

#[cfg(test)]
mod test {
    use std::str::FromStr;
    use ::*;

    #[test]
    fn from_id() {
        let agpl3 = LicenseKind::from_str("AGPL-3.0");
        assert_eq!(agpl3, Ok(LicenseKind::Agpl3(Agpl3)));

        let apache2 = LicenseKind::from_str("Apache-2.0");
        assert_eq!(apache2, Ok(LicenseKind::Apache2(Apache2)));

        let cc01 = LicenseKind::from_str("CC0-1.0");
        assert_eq!(cc01, Ok(LicenseKind::Cc01(Cc01)));

        let gpl3 = LicenseKind::from_str("GPL-3.0");
        assert_eq!(gpl3, Ok(LicenseKind::Gpl3(Gpl3)));

        let lgpl3 = LicenseKind::from_str("LGPL-3.0");
        assert_eq!(lgpl3, Ok(LicenseKind::Lgpl3(Lgpl3)));

        let mit = LicenseKind::from_str("MIT");
        assert_eq!(mit, Ok(LicenseKind::Mit(Mit::default())));

        let mpl2 = LicenseKind::from_str("MPL-2.0");
        assert_eq!(mpl2, Ok(LicenseKind::Mpl2(Mpl2)));

        let unlicense = LicenseKind::from_str("Unlicense");
        assert_eq!(unlicense, Ok(LicenseKind::Unlicense(Unlicense)));
    }

    #[test]
    fn from_text() {
        let agpl3 = LicenseKind::from_str(include_str!("../files/AGPL-3"));
        assert_eq!(agpl3, Ok(LicenseKind::Agpl3(Agpl3)));

        let apache2 = LicenseKind::from_str(include_str!("../files/APACHE-2"));
        assert_eq!(apache2, Ok(LicenseKind::Apache2(Apache2)));

        let cc01 = LicenseKind::from_str(include_str!("../files/CC0-1"));
        assert_eq!(cc01, Ok(LicenseKind::Cc01(Cc01)));

        let gpl3 = LicenseKind::from_str(include_str!("../files/GPL-3"));
        assert_eq!(gpl3, Ok(LicenseKind::Gpl3(Gpl3)));

        let lgpl3 = LicenseKind::from_str(include_str!("../files/LGPL-3"));
        assert_eq!(lgpl3, Ok(LicenseKind::Lgpl3(Lgpl3)));

        let mit =
            LicenseKind::from_str(&format!(include_str!("../files/MIT"), 2017, "Mr. Foo Bar"));
        assert_eq!(mit, Ok(LicenseKind::Mit(Mit::new(2017, "Mr. Foo Bar"))));

        let mpl2 = LicenseKind::from_str(include_str!("../files/MPL-2"));
        assert_eq!(mpl2, Ok(LicenseKind::Mpl2(Mpl2)));

        let unlicense = LicenseKind::from_str(include_str!("../files/UNLICENSE"));
        assert_eq!(unlicense, Ok(LicenseKind::Unlicense(Unlicense)));
    }
}