1use regex::Regex;
2
3pub fn is_license_eligible_for_data_collection(license: &str) -> bool {
4 // TODO: Include more licenses later (namely, Apache)
5 for pattern in [MIT_LICENSE_REGEX, ISC_LICENSE_REGEX] {
6 let regex = Regex::new(pattern.trim()).unwrap();
7 if regex.is_match(license.trim()) {
8 return true;
9 }
10 }
11 false
12}
13
14const MIT_LICENSE_REGEX: &str = r#"
15^.*MIT License.*
16
17Copyright.*?
18
19Permission is hereby granted, free of charge, to any person obtaining a copy
20of this software and associated documentation files \(the "Software"\), to deal
21in the Software without restriction, including without limitation the rights
22to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
23copies of the Software, and to permit persons to whom the Software is
24furnished to do so, subject to the following conditions:
25
26The above copyright notice and this permission notice shall be included in all
27copies or substantial portions of the Software\.
28
29THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
30IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
31FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT\. IN NO EVENT SHALL THE
32AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
33LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
34OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
35SOFTWARE\.$
36"#;
37
38const ISC_LICENSE_REGEX: &str = r#"
39^ISC License
40
41Copyright.*?
42
43Permission to use, copy, modify, and/or distribute this software for any
44purpose with or without fee is hereby granted, provided that the above
45copyright notice and this permission notice appear in all copies\.
46
47THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
48WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
49MERCHANTABILITY AND FITNESS\. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
50ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
51WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
52ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
53OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE\.$
54"#;
55
56#[cfg(test)]
57mod tests {
58 use unindent::unindent;
59
60 use crate::is_license_eligible_for_data_collection;
61
62 #[test]
63 fn test_mit_positive_detection() {
64 let example_license = unindent(
65 r#"
66 MIT License
67
68 Copyright (c) 2024 John Doe
69
70 Permission is hereby granted, free of charge, to any person obtaining a copy
71 of this software and associated documentation files (the "Software"), to deal
72 in the Software without restriction, including without limitation the rights
73 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
74 copies of the Software, and to permit persons to whom the Software is
75 furnished to do so, subject to the following conditions:
76
77 The above copyright notice and this permission notice shall be included in all
78 copies or substantial portions of the Software.
79
80 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
81 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
82 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
83 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
84 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
85 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
86 SOFTWARE.
87 "#
88 .trim(),
89 );
90
91 assert!(is_license_eligible_for_data_collection(&example_license));
92
93 let example_license = unindent(
94 r#"
95 The MIT License (MIT)
96
97 Copyright (c) 2019 John Doe
98
99 Permission is hereby granted, free of charge, to any person obtaining a copy
100 of this software and associated documentation files (the "Software"), to deal
101 in the Software without restriction, including without limitation the rights
102 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
103 copies of the Software, and to permit persons to whom the Software is
104 furnished to do so, subject to the following conditions:
105
106 The above copyright notice and this permission notice shall be included in all
107 copies or substantial portions of the Software.
108
109 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
110 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
111 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
112 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
113 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
114 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
115 SOFTWARE.
116 "#
117 .trim(),
118 );
119
120 assert!(is_license_eligible_for_data_collection(&example_license));
121 }
122
123 #[test]
124 fn test_mit_negative_detection() {
125 let example_license = unindent(
126 r#"
127 MIT License
128
129 Copyright (c) 2024 John Doe
130
131 Permission is hereby granted, free of charge, to any person obtaining a copy
132 of this software and associated documentation files (the "Software"), to deal
133 in the Software without restriction, including without limitation the rights
134 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
135 copies of the Software, and to permit persons to whom the Software is
136 furnished to do so, subject to the following conditions:
137
138 The above copyright notice and this permission notice shall be included in all
139 copies or substantial portions of the Software.
140
141 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
142 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
143 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
144 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
145 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
146 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
147 SOFTWARE.
148
149 This project is dual licensed under the MIT License and the Apache License, Version 2.0.
150 "#
151 .trim(),
152 );
153
154 assert!(!is_license_eligible_for_data_collection(&example_license));
155 }
156
157 #[test]
158 fn test_isc_positive_detection() {
159 let example_license = unindent(
160 r#"
161 ISC License
162
163 Copyright (c) 2024, John Doe
164
165 Permission to use, copy, modify, and/or distribute this software for any
166 purpose with or without fee is hereby granted, provided that the above
167 copyright notice and this permission notice appear in all copies.
168
169 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
170 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
171 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
172 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
173 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
174 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
175 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
176 "#
177 .trim(),
178 );
179
180 assert!(is_license_eligible_for_data_collection(&example_license));
181 }
182
183 #[test]
184 fn test_isc_negative_detection() {
185 let example_license = unindent(
186 r#"
187 ISC License
188
189 Copyright (c) 2024, John Doe
190
191 Permission to use, copy, modify, and/or distribute this software for any
192 purpose with or without fee is hereby granted, provided that the above
193 copyright notice and this permission notice appear in all copies.
194
195 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
196 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
197 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
198 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
199 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
200 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
201 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
202
203 This project is dual licensed under the ISC License and the MIT License.
204 "#
205 .trim(),
206 );
207
208 assert!(!is_license_eligible_for_data_collection(&example_license));
209 }
210}