1use std::{fmt::Display, str::FromStr};
2
3use crate::error::Error;
4
5#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
9pub enum License {
10 MIT,
11 Apache2,
12 GPL3,
13 AGPL3,
14 MPL2,
15 BSD2,
16 BSD3,
17 #[default]
18 None,
19}
20
21impl License {
22 pub fn options() -> Vec<&'static str> {
23 vec![
24 "MIT", "Apache2", "GPL3", "AGPL3", "MPL2", "BSD2", "BSD3", "None",
25 ]
26 }
27}
28
29impl FromStr for License {
30 type Err = Error;
31
32 fn from_str(s: &str) -> Result<Self, Self::Err> {
33 match s {
34 "MIT" => Ok(License::MIT),
35 "Apache2" => Ok(License::Apache2),
36 "GPL3" => Ok(License::GPL3),
37 "AGPL3" => Ok(License::AGPL3),
38 "MPL2" => Ok(License::MPL2),
39 "BSD2" => Ok(License::BSD2),
40 "BSD3" => Ok(License::BSD3),
41 "None" => Ok(License::None),
42 _ => Err(format!("unknown license: {}", s).into()),
43 }
44 }
45}
46
47impl Display for License {
48 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
49 match self {
50 License::MIT => write!(f, "MIT"),
51 License::Apache2 => write!(f, "Apache2"),
52 License::GPL3 => write!(f, "GPL3"),
53 License::AGPL3 => write!(f, "AGPL3"),
54 License::MPL2 => write!(f, "MPL2"),
55 License::BSD2 => write!(f, "BSD2"),
56 License::BSD3 => write!(f, "BSD3"),
57 License::None => write!(f, "None"),
58 }
59 }
60}
61
62impl License {
63 pub fn content(&self, year: i32, holder: &str) -> Option<String> {
69 match self {
70 License::MIT => Some(
71 MIT_LICENSE
72 .replace("<year>", &year.to_string())
73 .replace("<copyright holders>", holder),
74 ),
75 License::Apache2 => Some(
76 APACHE2_LICENSE
77 .replace("[yyyy]", &year.to_string())
78 .replace("[name of copyright owner]", holder),
79 ),
80 License::GPL3 => Some(
81 GPL3_LICENSE
82 .replace("<year>", &year.to_string())
83 .replace("<name of author>", holder),
84 ),
85 License::AGPL3 => Some(
86 AGPL3_LICENSE
87 .replace("<year>", &year.to_string())
88 .replace("<name of author>", holder),
89 ),
90 License::MPL2 => Some(MPL2_LICENSE.to_string()),
91 License::BSD2 => Some(
92 BSD2_LICENSE
93 .replace("<YEAR>", &year.to_string())
94 .replace("<COPYRIGHT HOLDER>", holder),
95 ),
96 License::BSD3 => Some(
97 BSD3_LICENSE
98 .replace("<YEAR>", &year.to_string())
99 .replace("<COPYRIGHT HOLDER>", holder),
100 ),
101 License::None => None,
102 }
103 }
104}
105
106const MIT_LICENSE: &str = r#"
108Copyright (c) <year> <copyright holders>
109
110Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
111
112The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
113
114THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
115"#;
116
117const APACHE2_LICENSE: &str = r#"
119Copyright [yyyy] [name of copyright owner]
120
121Licensed under the Apache License, Version 2.0 (the "License");
122you may not use this file except in compliance with the License.
123You may obtain a copy of the License at
124
125 http://www.apache.org/licenses/LICENSE-2.0
126
127Unless required by applicable law or agreed to in writing, software
128distributed under the License is distributed on an "AS IS" BASIS,
129WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
130See the License for the specific language governing permissions and
131limitations under the License.
132"#;
133
134const GPL3_LICENSE: &str = r#"
136 <one line to give the program's name and a brief idea of what it does.>
137 Copyright (C) <year> <name of author>
138
139 This program is free software: you can redistribute it and/or modify
140 it under the terms of the GNU General Public License as published by
141 the Free Software Foundation, either version 3 of the License, or
142 (at your option) any later version.
143
144 This program is distributed in the hope that it will be useful,
145 but WITHOUT ANY WARRANTY; without even the implied warranty of
146 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
147 GNU General Public License for more details.
148
149 You should have received a copy of the GNU General Public License
150 along with this program. If not, see <https://www.gnu.org/licenses/>.
151"#;
152
153const AGPL3_LICENSE: &str = r#"
155 <one line to give the program's name and a brief idea of what it does.>
156 Copyright (C) <year> <name of author>
157
158 This program is free software: you can redistribute it and/or modify
159 it under the terms of the GNU Affero General Public License as
160 published by the Free Software Foundation, either version 3 of the License, or
161 (at your option) any later version.
162
163 This program is distributed in the hope that it will be useful,
164 but WITHOUT ANY WARRANTY; without even the implied warranty of
165 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
166 GNU Affero General Public License for more details.
167
168 You should have received a copy of the GNU Affero General Public License
169 along with this program. If not, see <https://www.gnu.org/licenses/>.
170
171 Additional permission under GNU AGPL version 3 section 7:
172 If you modify this Program, or any covered work, by linking or combining it with
173 other code, such as the OpenSSL project's OpenSSL library (or a modified version of that
174 library), containing parts covered by the terms of the OpenSSL or SSLeay licenses, the
175 licensors of this Program grant you additional permission to convey the resulting work.
176"#;
177const MPL2_LICENSE: &str = r#"
179Mozilla Public License
180Version 2.0
1811. Definitions
182
1831.1. “Contributor”
184
185 means each individual or legal entity that creates, contributes to the creation of, or owns Covered Software.
1861.2. “Contributor Version”
187
188 means the combination of the Contributions of others (if any) used by a Contributor and that particular Contributor’s Contribution.
1891.3. “Contribution”
190
191 means Covered Software of a particular Contributor.
1921.4. “Covered Software”
193
194 means Source Code Form to which the initial Contributor has attached the notice in Exhibit A, the Executable Form of such Source Code Form, and Modifications of such Source Code Form, in each case including portions thereof.
1951.5. “Incompatible With Secondary Licenses”
196
197 means
198
199 that the initial Contributor has attached the notice described in Exhibit B to the Covered Software; or
200
201 that the Covered Software was made available under the terms of version 1.1 or earlier of the License, but not also under the terms of a Secondary License.
202
2031.6. “Executable Form”
204
205 means any form of the work other than Source Code Form.
2061.7. “Larger Work”
207
208 means a work that combines Covered Software with other material, in a separate file or files, that is not Covered Software.
2091.8. “License”
210
211 means this document.
2121.9. “Licensable”
213
214 means having the right to grant, to the maximum extent possible, whether at the time of the initial grant or subsequently, any and all of the rights conveyed by this License.
2151.10. “Modifications”
216
217 means any of the following:
218
219 any file in Source Code Form that results from an addition to, deletion from, or modification of the contents of Covered Software; or
220
221 any new file in Source Code Form that contains any Covered Software.
222
2231.11. “Patent Claims” of a Contributor
224
225 means any patent claim(s), including without limitation, method, process, and apparatus claims, in any patent Licensable by such Contributor that would be infringed, but for the grant of the License, by the making, using, selling, offering for sale, having made, import, or transfer of either its Contributions or its Contributor Version.
2261.12. “Secondary License”
227
228 means either the GNU General Public License, Version 2.0, the GNU Lesser General Public License, Version 2.1, the GNU Affero General Public License, Version 3.0, or any later versions of those licenses.
2291.13. “Source Code Form”
230
231 means the form of the work preferred for making modifications.
2321.14. “You” (or “Your”)
233
234 means an individual or a legal entity exercising rights under this License. For legal entities, “You” includes any entity that controls, is controlled by, or is under common control with You. For purposes of this definition, “control” means (a) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (b) ownership of more than fifty percent (50%) of the outstanding shares or beneficial ownership of such entity.
235
2362. License Grants and Conditions
2372.1. Grants
238
239Each Contributor hereby grants You a world-wide, royalty-free, non-exclusive license:
240
241 under intellectual property rights (other than patent or trademark) Licensable by such Contributor to use, reproduce, make available, modify, display, perform, distribute, and otherwise exploit its Contributions, either on an unmodified basis, with Modifications, or as part of a Larger Work; and
242
243 under Patent Claims of such Contributor to make, use, sell, offer for sale, have made, import, and otherwise transfer either its Contributions or its Contributor Version.
244
2452.2. Effective Date
246
247The licenses granted in Section 2.1 with respect to any Contribution become effective for each Contribution on the date the Contributor first distributes such Contribution.
2482.3. Limitations on Grant Scope
249
250The licenses granted in this Section 2 are the only rights granted under this License. No additional rights or licenses will be implied from the distribution or licensing of Covered Software under this License. Notwithstanding Section 2.1(b) above, no patent license is granted by a Contributor:
251
252 for any code that a Contributor has removed from Covered Software; or
253
254 for infringements caused by: (i) Your and any other third party’s modifications of Covered Software, or (ii) the combination of its Contributions with other software (except as part of its Contributor Version); or
255
256 under Patent Claims infringed by Covered Software in the absence of its Contributions.
257
258This License does not grant any rights in the trademarks, service marks, or logos of any Contributor (except as may be necessary to comply with the notice requirements in Section 3.4).
2592.4. Subsequent Licenses
260
261No Contributor makes additional grants as a result of Your choice to distribute the Covered Software under a subsequent version of this License (see Section 10.2) or under the terms of a Secondary License (if permitted under the terms of Section 3.3).
2622.5. Representation
263
264Each Contributor represents that the Contributor believes its Contributions are its original creation(s) or it has sufficient rights to grant the rights to its Contributions conveyed by this License.
2652.6. Fair Use
266
267This License is not intended to limit any rights You have under applicable copyright doctrines of fair use, fair dealing, or other equivalents.
2682.7. Conditions
269
270Sections 3.1, 3.2, 3.3, and 3.4 are conditions of the licenses granted in Section 2.1.
2713. Responsibilities
2723.1. Distribution of Source Form
273
274All distribution of Covered Software in Source Code Form, including any Modifications that You create or to which You contribute, must be under the terms of this License. You must inform recipients that the Source Code Form of the Covered Software is governed by the terms of this License, and how they can obtain a copy of this License. You may not attempt to alter or restrict the recipients’ rights in the Source Code Form.
2753.2. Distribution of Executable Form
276
277If You distribute Covered Software in Executable Form then:
278
279 such Covered Software must also be made available in Source Code Form, as described in Section 3.1, and You must inform recipients of the Executable Form how they can obtain a copy of such Source Code Form by reasonable means in a timely manner, at a charge no more than the cost of distribution to the recipient; and
280
281 You may distribute such Executable Form under the terms of this License, or sublicense it under different terms, provided that the license for the Executable Form does not attempt to limit or alter the recipients’ rights in the Source Code Form under this License.
282
2833.3. Distribution of a Larger Work
284
285You may create and distribute a Larger Work under terms of Your choice, provided that You also comply with the requirements of this License for the Covered Software. If the Larger Work is a combination of Covered Software with a work governed by one or more Secondary Licenses, and the Covered Software is not Incompatible With Secondary Licenses, this License permits You to additionally distribute such Covered Software under the terms of such Secondary License(s), so that the recipient of the Larger Work may, at their option, further distribute the Covered Software under the terms of either this License or such Secondary License(s).
2863.4. Notices
287
288You may not remove or alter the substance of any license notices (including copyright notices, patent notices, disclaimers of warranty, or limitations of liability) contained within the Source Code Form of the Covered Software, except that You may alter any license notices to the extent required to remedy known factual inaccuracies.
2893.5. Application of Additional Terms
290
291You may choose to offer, and to charge a fee for, warranty, support, indemnity or liability obligations to one or more recipients of Covered Software. However, You may do so only on Your own behalf, and not on behalf of any Contributor. You must make it absolutely clear that any such warranty, support, indemnity, or liability obligation is offered by You alone, and You hereby agree to indemnify every Contributor for any liability incurred by such Contributor as a result of warranty, support, indemnity or liability terms You offer. You may include additional disclaimers of warranty and limitations of liability specific to any jurisdiction.
2924. Inability to Comply Due to Statute or Regulation
293
294If it is impossible for You to comply with any of the terms of this License with respect to some or all of the Covered Software due to statute, judicial order, or regulation then You must: (a) comply with the terms of this License to the maximum extent possible; and (b) describe the limitations and the code they affect. Such description must be placed in a text file included with all distributions of the Covered Software under this License. Except to the extent prohibited by statute or regulation, such description must be sufficiently detailed for a recipient of ordinary skill to be able to understand it.
2955. Termination
296
2975.1. The rights granted under this License will terminate automatically if You fail to comply with any of its terms. However, if You become compliant, then the rights granted under this License from a particular Contributor are reinstated (a) provisionally, unless and until such Contributor explicitly and finally terminates Your grants, and (b) on an ongoing basis, if such Contributor fails to notify You of the non-compliance by some reasonable means prior to 60 days after You have come back into compliance. Moreover, Your grants from a particular Contributor are reinstated on an ongoing basis if such Contributor notifies You of the non-compliance by some reasonable means, this is the first time You have received notice of non-compliance with this License from such Contributor, and You become compliant prior to 30 days after Your receipt of the notice.
298
2995.2. If You initiate litigation against any entity by asserting a patent infringement claim (excluding declaratory judgment actions, counter-claims, and cross-claims) alleging that a Contributor Version directly or indirectly infringes any patent, then the rights granted to You by any and all Contributors for the Covered Software under Section 2.1 of this License shall terminate.
300
3015.3. In the event of termination under Sections 5.1 or 5.2 above, all end user license agreements (excluding distributors and resellers) which have been validly granted by You or Your distributors under this License prior to termination shall survive termination.
3026. Disclaimer of Warranty
303
304Covered Software is provided under this License on an “as is” basis, without warranty of any kind, either expressed, implied, or statutory, including, without limitation, warranties that the Covered Software is free of defects, merchantable, fit for a particular purpose or non-infringing. The entire risk as to the quality and performance of the Covered Software is with You. Should any Covered Software prove defective in any respect, You (not any Contributor) assume the cost of any necessary servicing, repair, or correction. This disclaimer of warranty constitutes an essential part of this License. No use of any Covered Software is authorized under this License except under this disclaimer.
3057. Limitation of Liability
306
307Under no circumstances and under no legal theory, whether tort (including negligence), contract, or otherwise, shall any Contributor, or anyone who distributes Covered Software as permitted above, be liable to You for any direct, indirect, special, incidental, or consequential damages of any character including, without limitation, damages for lost profits, loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses, even if such party shall have been informed of the possibility of such damages. This limitation of liability shall not apply to liability for death or personal injury resulting from such party’s negligence to the extent applicable law prohibits such limitation. Some jurisdictions do not allow the exclusion or limitation of incidental or consequential damages, so this exclusion and limitation may not apply to You.
3088. Litigation
309
310Any litigation relating to this License may be brought only in the courts of a jurisdiction where the defendant maintains its principal place of business and such litigation shall be governed by laws of that jurisdiction, without reference to its conflict-of-law provisions. Nothing in this Section shall prevent a party’s ability to bring cross-claims or counter-claims.
3119. Miscellaneous
312
313This License represents the complete agreement concerning the subject matter hereof. If any provision of this License is held to be unenforceable, such provision shall be reformed only to the extent necessary to make it enforceable. Any law or regulation which provides that the language of a contract shall be construed against the drafter shall not be used to construe this License against a Contributor.
31410. Versions of the License
31510.1. New Versions
316
317Mozilla Foundation is the license steward. Except as provided in Section 10.3, no one other than the license steward has the right to modify or publish new versions of this License. Each version will be given a distinguishing version number.
31810.2. Effect of New Versions
319
320You may distribute the Covered Software under the terms of the version of the License under which You originally received the Covered Software, or under the terms of any subsequent version published by the license steward.
32110.3. Modified Versions
322
323If you create software not governed by this License, and you want to create a new license for such software, you may create and use a modified version of this License if you rename the license and remove any references to the name of the license steward (except to note that such modified license differs from this License).
32410.4. Distributing Source Code Form that is Incompatible With Secondary Licenses
325
326If You choose to distribute Source Code Form that is Incompatible With Secondary Licenses under the terms of this version of the License, the notice described in Exhibit B of this License must be attached.
327Exhibit A - Source Code Form License Notice
328
329 This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/.
330
331If it is not possible or desirable to put the notice in a particular file, then You may include the notice in a location (such as a LICENSE file in a relevant directory) where a recipient would be likely to look for such a notice.
332
333You may add additional accurate notices of copyright ownership.
334Exhibit B - “Incompatible With Secondary Licenses” Notice
335
336 This Source Code Form is “Incompatible With Secondary Licenses”, as defined by the Mozilla Public License, v. 2.0.
337"#;
338const BSD2_LICENSE: &str = r#"
340Copyright <YEAR> <COPYRIGHT HOLDER>
341
342Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
343
3441. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
345
3462. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
347
348THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
349"#;
350const BSD3_LICENSE: &str = r#"
352Copyright <YEAR> <COPYRIGHT HOLDER>
353
354Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
355
3561. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
357
3582. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
359
3603. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
361
362THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
363"#;