1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
#![allow(clippy::needless_update)]

use crate::*;
use core::fmt::{self, Display, Formatter};

/// Returns an extension license based on the provided id.
///
/// # Examples
/// ```
/// let mit = license::from_id_ext("MIT").unwrap();
/// let perm = mit.permissions();
/// assert!(perm.private_use() && perm.commercial_use());
/// ```
pub fn from_id_ext(id: &str) -> Option<&'static dyn LicenseExt> {
    match id {
        "AFL-3.0" => Some(&AFL_3_0),
        "AGPL-3.0-only" => Some(&AGPL_3_0_only),
        "Apache-2.0" => Some(&Apache_2_0),
        "0BSD" => Some(&BSD_0),
        "BSD-2-Clause" => Some(&BSD_2_Clause),
        "BSD-3-Clause" => Some(&BSD_3_Clause),
        "BSD-3-Clause-Clear" => Some(&BSD_3_Clause_Clear),
        "BSL-1.0" => Some(&BSL_1_0),
        "CC0-1.0" => Some(&CC0_1_0),
        "ECL-2.0" => Some(&ECL_2_0),
        "GPL-3.0-only" => Some(&GPL_3_0_only),
        "LGPL-3.0-only" => Some(&LGPL_3_0_only),
        "MIT" => Some(&MIT),
        "MPL-2.0" => Some(&MPL_2_0),
        "MS-PL" => Some(&MS_PL),
        "OSL-3.0" => Some(&OSL_3_0),
        "Unlicense" => Some(&Unlicense),
        "WTFPL" => Some(&WTFPL),
        "Zlib" => Some(&Zlib),
        _ => None,
    }
}

/// The permissions of the license.
///
/// # Examples
/// ```
/// let mit = license::from_id_ext("MIT").unwrap();
/// let perm = mit.permissions();
/// assert!(perm.private_use() && perm.commercial_use());
/// ```
#[derive(Copy, Clone, Debug, Default, Hash, Ord, PartialOrd, Eq, PartialEq)]
pub struct Permissions {
    commercial_use: bool,
    distribution: bool,
    modification: bool,
    patent_rights: bool,
    private_use: bool,
}

impl Permissions {
    /// May be used for commercial purposes.
    pub const fn commercial_use(self) -> bool {
        self.commercial_use
    }

    /// May be distributed.
    pub const fn distribution(self) -> bool {
        self.distribution
    }

    /// May be modified.
    pub const fn modification(self) -> bool {
        self.modification
    }

    /// Provides an express grant of patent rights from contributors.
    pub const fn patent_rights(self) -> bool {
        self.patent_rights
    }

    /// May be used for private purposes.
    pub const fn private_use(self) -> bool {
        self.private_use
    }
}

impl Display for Permissions {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        if self.commercial_use {
            f.write_str("- May be used for commercial purposes.\n")?;
        }
        if self.distribution {
            f.write_str("- May be distributed.\n")?;
        }
        if self.modification {
            f.write_str("- May be modified.\n")?;
        }
        if self.patent_rights {
            f.write_str("- Provides an express grant of patent rights from contributors.\n")?;
        }
        if self.private_use {
            f.write_str("- May be used for private purposes.\n")?;
        }
        Ok(())
    }
}

/// The conditions of the license.
///
/// # Examples
/// ```
/// let mit = license::from_id_ext("MIT").unwrap();
/// let cond = mit.conditions();
/// assert!(cond.license_and_copyright_notice());
/// ```
#[derive(Copy, Clone, Debug, Default, Hash, Ord, PartialOrd, Eq, PartialEq)]
pub struct Conditions {
    disclose_sources: bool,
    document_changes: bool,
    license_and_copyright_notice: bool,
    network_use_is_distribution: bool,
    same_license: bool,
}

impl Conditions {
    /// Source code must be made available when the software is distributed.
    pub const fn disclose_sources(self) -> bool {
        self.disclose_sources
    }

    /// Changes made to the code must be documented.
    pub const fn document_changes(self) -> bool {
        self.document_changes
    }

    /// The license and copyright notice must be included with the software.
    pub const fn license_and_copyright_notice(self) -> bool {
        self.license_and_copyright_notice
    }

    /// Users who interact with the software via network are
    /// given the right to receive a copy of the source code.
    pub const fn network_use_is_distribution(self) -> bool {
        self.network_use_is_distribution
    }

    /// Modifications must be released under the same license.
    pub const fn same_license(self) -> bool {
        self.same_license
    }
}

impl Display for Conditions {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        if self.disclose_sources {
            f.write_str(
                "- Source code must be made available when the software is distributed.\n",
            )?;
        }
        if self.document_changes {
            f.write_str("- Changes made to the code must be documented.\n")?;
        }
        if self.license_and_copyright_notice {
            f.write_str(
                "- The license and copyright notice must be included with the software.\n",
            )?;
        }
        if self.network_use_is_distribution {
            f.write_str("- Users who interact with the software via network are given the right to receive a copy of the source code.\n")?;
        }
        if self.same_license {
            f.write_str("- Modifications must be released under the same license.\n")?;
        }
        Ok(())
    }
}

/// The limitations of the license.
///
/// # Examples
/// ```
/// let mit = license::from_id_ext("MIT").unwrap();
/// let lim = mit.limitations();
/// assert!(lim.no_liability() && lim.no_warranty());
/// ```
#[derive(Copy, Clone, Debug, Default, Hash, Ord, PartialOrd, Eq, PartialEq)]
pub struct Limitations {
    no_liability: bool,
    no_trademark_rights: bool,
    no_warranty: bool,
    no_patent_rights: bool,
}

impl Limitations {
    /// Includes a limitation of liability.
    pub const fn no_liability(self) -> bool {
        self.no_liability
    }

    /// Does not grant trademark rights.
    pub const fn no_trademark_rights(self) -> bool {
        self.no_trademark_rights
    }

    /// Does not provide any warranty.
    pub const fn no_warranty(self) -> bool {
        self.no_warranty
    }

    /// Does not provide any rights in the patents of contributors.
    pub const fn no_patent_rights(self) -> bool {
        self.no_patent_rights
    }
}

impl Display for Limitations {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        if self.no_liability {
            f.write_str("- Includes a limitation of liability.\n")?;
        }
        if self.no_trademark_rights {
            f.write_str("- Does not grant trademark rights.\n")?;
        }
        if self.no_warranty {
            f.write_str("- Does not provide any warranty.\n")?;
        }
        if self.no_patent_rights {
            f.write_str("- Does not provide any rights in the patents of contributors.\n")?;
        }
        Ok(())
    }
}

macro_rules! impl_ext {
    (
        $(impl $struct:ident {
            permissions: $($permissions:ident)|*;
            conditions: $($conditions:ident)|*;
            limitations: $($limitations:ident)|*;
        })*
    ) => {
        $(impl LicenseExt for $struct {
            fn permissions(&self) -> Permissions {
                Permissions {
                    $($permissions: true,)*
                    ..Default::default()
                }
            }

            fn conditions(&self) -> Conditions {
                Conditions {
                    $($conditions: true,)*
                    ..Default::default()
                }
            }

            fn limitations(&self) -> Limitations {
                Limitations {
                    $($limitations: true,)*
                    ..Default::default()
                }
            }
        })*
    };
}

impl_ext! {
    impl AFL_3_0 {
        permissions: commercial_use | distribution | modification | patent_rights | private_use;
        conditions:  document_changes | license_and_copyright_notice;
        limitations: no_liability | no_trademark_rights | no_warranty;
    }
    impl AGPL_3_0_only {
        permissions: commercial_use | distribution | modification | patent_rights | private_use;
        conditions: disclose_sources | document_changes | license_and_copyright_notice | network_use_is_distribution | same_license;
        limitations: no_liability | no_warranty;
    }
    impl Apache_2_0 {
        permissions: commercial_use | distribution | modification | patent_rights | private_use;
        conditions: document_changes | license_and_copyright_notice;
        limitations: no_liability | no_trademark_rights | no_warranty;
    }
    impl BSD_0 {
        permissions: commercial_use | distribution | modification | private_use;
        conditions: ;
        limitations: no_liability | no_warranty;
    }
    impl BSD_2_Clause {
        permissions: commercial_use | distribution | modification | private_use;
        conditions: license_and_copyright_notice;
        limitations: no_liability | no_warranty;
    }
    impl BSD_3_Clause {
        permissions: commercial_use | distribution | modification | private_use;
        conditions: license_and_copyright_notice;
        limitations: no_liability | no_warranty;
    }
    impl BSD_3_Clause_Clear {
        permissions: commercial_use | distribution | modification | private_use;
        conditions: license_and_copyright_notice;
        limitations: no_liability | no_warranty | no_patent_rights;
    }
    impl BSL_1_0 {
        permissions: commercial_use | distribution | modification | private_use;
        conditions: license_and_copyright_notice;
        limitations: no_liability | no_warranty;
    }
    impl CC0_1_0 {
        permissions: commercial_use | distribution | modification | private_use;
        conditions: ;
        limitations: no_liability | no_trademark_rights | no_warranty | no_patent_rights;
    }
    impl ECL_2_0 {
        permissions: commercial_use | distribution | modification | patent_rights | private_use;
        conditions: document_changes | license_and_copyright_notice;
        limitations: no_liability | no_trademark_rights | no_warranty;
    }
    impl GPL_3_0_only {
        permissions: commercial_use | distribution | modification | patent_rights | private_use;
        conditions: disclose_sources | document_changes | license_and_copyright_notice | same_license;
        limitations: no_liability | no_warranty;
    }
    impl LGPL_3_0_only {
        permissions: commercial_use | distribution | modification | patent_rights | private_use;
        conditions: disclose_sources | document_changes | license_and_copyright_notice | same_license;
        limitations: no_liability | no_warranty;
    }
    impl MIT {
        permissions: commercial_use | distribution | modification | private_use;
        conditions: license_and_copyright_notice;
        limitations: no_liability | no_warranty;
    }
    impl MPL_2_0 {
        permissions: commercial_use | distribution | modification | patent_rights | private_use;
        conditions: disclose_sources | license_and_copyright_notice | same_license;
        limitations: no_liability | no_trademark_rights | no_warranty;
    }
    impl MS_PL {
        permissions: commercial_use | distribution | modification | patent_rights | private_use;
        conditions: license_and_copyright_notice;
        limitations: no_trademark_rights | no_warranty;
    }
    impl OSL_3_0 {
        permissions: commercial_use | distribution | modification | patent_rights | private_use;
        conditions: disclose_sources | document_changes | license_and_copyright_notice | network_use_is_distribution | same_license;
        limitations: no_liability | no_trademark_rights | no_warranty;
    }
    impl Unlicense {
        permissions: commercial_use | distribution | modification | private_use;
        conditions: ;
        limitations: no_liability | no_warranty;
    }
    impl WTFPL {
        permissions: commercial_use | distribution | modification | private_use;
        conditions: ;
        limitations: ;
    }
    impl Zlib {
        permissions: commercial_use | distribution | modification | private_use;
        conditions: document_changes | license_and_copyright_notice;
        limitations: no_liability | no_warranty;
    }
}