cargo_toml_builder/types/
license.rs1use std::fmt;
2
3#[derive(Debug, Clone, PartialEq)]
5pub enum License {
6 Mit,
8 Apache2,
10 Gplv3,
12 Bsd,
14 Mpl,
16 Agplv3,
18 Other(String),
20}
21
22impl From<String> for License {
23 fn from(s: String) -> License {
24 License::from(s.as_str())
25 }
26}
27
28impl<'a> From<&'a str> for License {
29 fn from(s: &'a str) -> License {
30 match s.to_lowercase().as_str() {
31 "mit" => License::Mit,
32 "apache2" => License::Apache2,
33 "apache-2.0" => License::Apache2,
34 "gpl" => License::Gplv3,
35 "gplv3" => License::Gplv3,
36 "gpl-3.0" => License::Gplv3,
37 "bsd" => License::Bsd,
38 "mpl" => License::Mpl,
39 "agpl" => License::Agplv3,
40 "agplv3" => License::Agplv3,
41 "agpl-3.0" => License::Agplv3,
42 _ => License::Other(s.to_string()),
43 }
44 }
45}
46
47impl fmt::Display for License {
48 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
49 write!(f, "{}", match self {
50 &License::Mit => "MIT",
51 &License::Apache2 => "Apache-2.0",
52 &License::Gplv3 => "GPL-3.0",
53 &License::Bsd => "BSD",
54 &License::Mpl => "MPL",
55 &License::Agplv3 => "AGPL-3.0",
56 &License::Other(ref s) => s,
57 })
58 }
59}
60
61#[cfg(test)]
62mod tests {
63 use super::*;
64
65 #[test]
66 fn test_from_string() {
67 assert_eq!(License::from("mit"), License::Mit);
68 assert_eq!(License::from("MIT"), License::Mit);
69 assert_eq!(License::from("apache2"), License::Apache2);
70 assert_eq!(License::from("Apache2"), License::Apache2);
71 assert_eq!(License::from("Apache-2.0"), License::Apache2);
72 assert_eq!(License::from("GPL"), License::Gplv3);
73 assert_eq!(License::from("gpl"), License::Gplv3);
74 assert_eq!(License::from("GPL-3.0"), License::Gplv3);
75 assert_eq!(License::from("GPLv3"), License::Gplv3);
76 assert_eq!(License::from("gplv3"), License::Gplv3);
77 assert_eq!(License::from("bsd"), License::Bsd);
78 assert_eq!(License::from("BSD"), License::Bsd);
79 assert_eq!(License::from("mpl"), License::Mpl);
80 assert_eq!(License::from("MPL"), License::Mpl);
81 assert_eq!(License::from("agpl"), License::Agplv3);
82 assert_eq!(License::from("AGPL"), License::Agplv3);
83 assert_eq!(License::from("agplv3"), License::Agplv3);
84 assert_eq!(License::from("AGPLv3"), License::Agplv3);
85 assert_eq!(License::from("AGPL-3.0"), License::Agplv3);
86 assert_eq!(License::from("CUSTOM"), License::Other("CUSTOM".to_string()));
87 }
88
89 #[test]
90 fn test_to_string() {
91 assert_eq!(License::Mit.to_string().as_str(), "MIT");
92 assert_eq!(License::Apache2.to_string().as_str(), "Apache-2.0");
93 assert_eq!(License::Gplv3.to_string().as_str(), "GPL-3.0");
94 assert_eq!(License::Bsd.to_string().as_str(), "BSD");
95 assert_eq!(License::Mpl.to_string().as_str(), "MPL");
96 assert_eq!(License::Agplv3.to_string().as_str(), "AGPL-3.0");
97 assert_eq!(License::Other("CUSTOM".to_string()).to_string().as_str(), "CUSTOM");
98 }
99}