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" => Some(Box::new(license::BSD {})),
17 "mit" => Some(Box::new(license::Mit {})),
19 "isc" => Some(Box::new(license::ISC {})),
21 "gpl" => Some(Box::new(license::GPL {})),
23 "gpl3" => Some(Box::new(license::GPL {})),
24 "gpl-3" => Some(Box::new(license::GPL {})),
25 "gpl2" => Some(Box::new(license::GPL2 {})),
27 "gpl-2" => Some(Box::new(license::GPL2 {})),
28 "agpl" => Some(Box::new(license::AGPL {})),
30 "agpl-3" => Some(Box::new(license::AGPL {})),
31 "apache" => Some(Box::new(license::Apache {})),
33 "cc-by" => Some(Box::new(license::CcBy {})),
35 "ccby" => Some(Box::new(license::CcBy {})),
36 "cc-by-nc" => Some(Box::new(license::CcByNc {})),
38 "ccbync" => Some(Box::new(license::CcByNc {})),
39 "cc-by-sa" => Some(Box::new(license::CcBySa {})),
41 "ccbysa" => Some(Box::new(license::CcBySa {})),
42 "cc-by-nc-sa" => Some(Box::new(license::CcByNcSa {})),
44 "ccbyncsa" => Some(Box::new(license::CcByNcSa {})),
45 "cc0" => Some(Box::new(license::CCZero {})),
47 "lgpl" => Some(Box::new(license::LGPL {})),
49 "lgpl-3" => Some(Box::new(license::LGPL {})),
50 "mpl" => Some(Box::new(license::MPL {})),
52 "unlicense" => Some(Box::new(license::UNLICENSE {})),
54 "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}