entrenar/research/artifact/
license.rs1use serde::{Deserialize, Serialize};
4
5#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
7pub enum License {
8 Mit,
10 Apache2,
12 Bsd3,
14 Gpl3,
16 CcBy4,
18 Cc0,
20 Custom(String),
22}
23
24impl std::fmt::Display for License {
25 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
26 match self {
27 Self::Mit => write!(f, "MIT"),
28 Self::Apache2 => write!(f, "Apache-2.0"),
29 Self::Bsd3 => write!(f, "BSD-3-Clause"),
30 Self::Gpl3 => write!(f, "GPL-3.0"),
31 Self::CcBy4 => write!(f, "CC-BY-4.0"),
32 Self::Cc0 => write!(f, "CC0-1.0"),
33 Self::Custom(spdx) => write!(f, "{spdx}"),
34 }
35 }
36}
37
38#[cfg(test)]
39mod tests {
40 use super::*;
41
42 #[test]
43 fn test_license_display() {
44 assert_eq!(License::Mit.to_string(), "MIT");
45 assert_eq!(License::Apache2.to_string(), "Apache-2.0");
46 assert_eq!(License::Bsd3.to_string(), "BSD-3-Clause");
47 assert_eq!(License::Gpl3.to_string(), "GPL-3.0");
48 assert_eq!(License::CcBy4.to_string(), "CC-BY-4.0");
49 assert_eq!(License::Cc0.to_string(), "CC0-1.0");
50 assert_eq!(License::Custom("LGPL-2.1".to_string()).to_string(), "LGPL-2.1");
51 }
52
53 #[test]
54 fn test_license_clone() {
55 let license = License::Mit;
56 let cloned = license.clone();
57 assert_eq!(license, cloned);
58
59 let custom = License::Custom("WTFPL".to_string());
60 let custom_cloned = custom.clone();
61 assert_eq!(custom, custom_cloned);
62 }
63
64 #[test]
65 fn test_license_eq() {
66 assert_eq!(License::Mit, License::Mit);
67 assert_ne!(License::Mit, License::Apache2);
68 assert_eq!(License::Custom("ISC".to_string()), License::Custom("ISC".to_string()));
69 assert_ne!(License::Custom("ISC".to_string()), License::Custom("MPL-2.0".to_string()));
70 }
71
72 #[test]
73 fn test_license_hash() {
74 use std::collections::HashSet;
75 let mut set = HashSet::new();
76 set.insert(License::Mit);
77 set.insert(License::Mit);
78 assert_eq!(set.len(), 1);
79 set.insert(License::Apache2);
80 assert_eq!(set.len(), 2);
81 }
82
83 #[test]
84 fn test_license_serde() {
85 let license = License::Mit;
86 let json = serde_json::to_string(&license).expect("JSON serialization should succeed");
87 let deserialized: License =
88 serde_json::from_str(&json).expect("JSON deserialization should succeed");
89 assert_eq!(license, deserialized);
90
91 let custom = License::Custom("Unlicense".to_string());
92 let custom_json =
93 serde_json::to_string(&custom).expect("JSON serialization should succeed");
94 let custom_deserialized: License =
95 serde_json::from_str(&custom_json).expect("JSON deserialization should succeed");
96 assert_eq!(custom, custom_deserialized);
97 }
98
99 #[test]
100 fn test_license_debug() {
101 assert_eq!(format!("{:?}", License::Mit), "Mit");
102 assert_eq!(format!("{:?}", License::Custom("ISC".to_string())), "Custom(\"ISC\")");
103 }
104}