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
extern crate license;

use license::License;
use std::fs::File;
use std::io::Write;
use std::io;


pub fn write_license(license: &Box<License>, output_path: &str) -> Result<(), io::Error> {
    let mut file = File::create(output_path)?;
    file.write(license.text().as_bytes())?;
    Ok(())
}

pub fn create_license<'a>(license_type: &'a str, author: &'a str, year: i32) -> Option<Box<License>> {
    match license_type {
        // MIT
        "mit" => Some(Box::new(license::Mit::new(year, &author))),
        "MIT" => Some(Box::new(license::Mit::new(year, &author))),
        // APGL
        "apgl" => Some(Box::new(license::Agpl3)),
        "APGL" => Some(Box::new(license::Agpl3)),
        "apgl-3" => Some(Box::new(license::Agpl3)),
        "APGL-3" => Some(Box::new(license::Agpl3)),
        // Apache
        "apache" => Some(Box::new(license::Apache2)),
        "Apache" => Some(Box::new(license::Apache2)),
        // CC0
        "cc0" => Some(Box::new(license::Cc01)),
        "CC0" => Some(Box::new(license::Cc01)),
        // LGPL
        "lgpl" => Some(Box::new(license::Lgpl3)),
        "LGPL" => Some(Box::new(license::Lgpl3)),
        "lgpl-3" => Some(Box::new(license::Lgpl3)),
        "LGPL-3" => Some(Box::new(license::Lgpl3)),
        // MPL
        "mpl" => Some(Box::new(license::Mpl2)),
        "MPL" => Some(Box::new(license::Mpl2)),
        // Unlicense
        "unlinse" => Some(Box::new(license::Unlicense)),
        "Unlinse" => Some(Box::new(license::Unlicense)),
        "UNLICENSE" => Some(Box::new(license::Unlicense)),
        _ => None
    }
}


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

    #[test]
    fn not_found_license() {
        let license = create_license("NONONONO", "azu", 2018);
        assert!(license.is_none());
    }

    #[test]
    fn create_mit_license() {
        let license = create_license("MIT", "azu", 2018);
        let l = license.unwrap();
        assert_eq!(l.name(), Mit::new(2018, "azu").name());
        assert_eq!(l.id(), Mit::new(2018, "azu").id());
    }

    #[test]
    fn create_other_license() {
        let license = create_license("cc0", "azu", 2018);
        let l = license.unwrap();
        assert_eq!(l.name(), Cc01.name());
        assert_eq!(l.id(), Cc01.id());
    }
}