license_generator/
lib.rs

1use std::fs::File;
2use std::io;
3use std::io::Write;
4
5pub mod license;
6
7pub fn write_license(license_text: &str, output_path: &str) -> Result<(), io::Error> {
8    let mut file = File::create(output_path)?;
9    file.write_all(license_text.as_bytes())?;
10    Ok(())
11}
12
13pub fn create_license(license_type: &str) -> Option<Box<dyn license::License>> {
14    match license_type.to_lowercase().as_str() {
15        // BSD
16        "bsd" => Some(Box::new(license::BSD {})),
17        // MIT
18        "mit" => Some(Box::new(license::Mit {})),
19        // ISC
20        "isc" => Some(Box::new(license::ISC {})),
21        // GPL
22        "gpl" => Some(Box::new(license::GPL {})),
23        "gpl3" => Some(Box::new(license::GPL {})),
24        "gpl-3" => Some(Box::new(license::GPL {})),
25        // GPLv2
26        "gpl2" => Some(Box::new(license::GPL2 {})),
27        "gpl-2" => Some(Box::new(license::GPL2 {})),
28        // AGPL
29        "agpl" => Some(Box::new(license::AGPL {})),
30        "agpl-3" => Some(Box::new(license::AGPL {})),
31        // Apache
32        "apache" => Some(Box::new(license::Apache {})),
33        // CC_BY
34        "cc-by" => Some(Box::new(license::CcBy {})),
35        "ccby" => Some(Box::new(license::CcBy {})),
36        // CC_BY_NC
37        "cc-by-nc" => Some(Box::new(license::CcByNc {})),
38        "ccbync" => Some(Box::new(license::CcByNc {})),
39        // CC_BY_SA
40        "cc-by-sa" => Some(Box::new(license::CcBySa {})),
41        "ccbysa" => Some(Box::new(license::CcBySa {})),
42        // CC_BY_NC_SA
43        "cc-by-nc-sa" => Some(Box::new(license::CcByNcSa {})),
44        "ccbyncsa" => Some(Box::new(license::CcByNcSa {})),
45        // CC0
46        "cc0" => Some(Box::new(license::CCZero {})),
47        // LGPL
48        "lgpl" => Some(Box::new(license::LGPL {})),
49        "lgpl-3" => Some(Box::new(license::LGPL {})),
50        // MPL
51        "mpl" => Some(Box::new(license::MPL {})),
52        // Unlicense
53        "unlicense" => Some(Box::new(license::UNLICENSE {})),
54        // EUPL
55        "eupl" => Some(Box::new(license::EUPL {})),
56        "eupl12" => Some(Box::new(license::EUPL {})),
57        "eupl-1.2" => Some(Box::new(license::EUPL {})),
58        _ => None,
59    }
60}
61
62#[cfg(test)]
63mod test {
64    use super::*;
65
66    #[test]
67    fn not_found_license() {
68        let license = create_license("_NO_LICENSE_");
69        assert!(license.is_none());
70    }
71
72    #[test]
73    fn create_mit_license() {
74        let mit = create_license("mit");
75        let license_text = mit.unwrap().notice(2018, "TEST_USER", "license-generator");
76        assert!(license_text.contains("Permission is hereby granted"));
77        assert!(license_text.contains("TEST_USER"));
78        assert!(license_text.contains("2018"));
79    }
80
81    #[test]
82    fn create_isc_license() {
83        let isc = create_license("isc");
84        let license_text = isc.unwrap().notice(2018, "TEST_USER", "license-generator");
85        assert!(license_text.contains("Permission to use, copy, modify, and/or distribute"));
86        assert!(license_text.contains("TEST_USER"));
87        assert!(license_text.contains("2018"));
88    }
89
90    #[test]
91    fn create_gpl_license() {
92        let gpl = create_license("gpl");
93        let license_text = gpl.unwrap().notice(2018, "TEST_USER", "license-generator");
94        assert!(license_text.contains("GNU GENERAL PUBLIC LICENSE"));
95        assert!(license_text.contains("TEST_USER"));
96        assert!(license_text.contains("2018"));
97        assert!(license_text.contains("license-generator"));
98    }
99
100    #[test]
101    fn create_ccby_license() {
102        let ccby = create_license("ccby");
103        let license_text = ccby.unwrap().notice(2018, "TEST_USER", "license-generator");
104        assert!(license_text.contains("Attribution 4.0 International"));
105        assert!(license_text.contains("TEST_USER"));
106        assert!(license_text.contains("license-generator"));
107    }
108}