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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
//! Provides license information from [spdx.org](https://spdx.org).
//!
//! The library also extends certain licenses with information about their limitations, conditions, and permission.
//! Additionally, it provides the ability to identify these extended licenses from their license text.

#![doc(html_root_url = "https://docs.rs/license/0.8.1")]
#![no_std]
#![deny(
    bad_style,
    bare_trait_objects,
    missing_debug_implementations,
    missing_docs,
    unused_import_braces,
    unused_qualifications,
    unsafe_code,
    unstable_features
)]

#[allow(bad_style)]
mod licenses;

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

/// Base functionality for all licenses.
pub trait License {
    /// The name of the license.
    ///
    /// Corresponds to the *Full name* column from [spdx.org/licenses](https://spdx.org/licenses/).
    fn name(&self) -> &'static str;

    /// The identifier of the license.
    ///
    /// Corresponds to the *Identifier* column from [spdx.org/licenses](https://spdx.org/licenses/).
    fn id(&self) -> &'static str;

    /// The license text.
    fn text(&self) -> &'static str;

    /// The standard license header.
    fn header(&self) -> Option<&'static str>;

    /// Says if the license is OSI approved.
    ///
    /// Corresponds to the *OSI Approved?* column from [spdx.org/licenses](https://spdx.org/licenses/).
    fn is_osi_approved(&self) -> bool;

    /// Says if the license is FSF Libre.
    ///
    /// Corresponds to the *FSF Free/Libre?* column from [spdx.org/licenses](https://spdx.org/licenses/).
    fn is_fsf_libre(&self) -> bool;

    /// Says if the license is deprecated.
    fn is_deprecated(&self) -> bool;

    /// Relevant sources.
    fn see_also(&self) -> &'static [&'static str];
}

/// Extension trait for supported licenses.
pub trait LicenseExt: License {
    /// The permissions of the license.
    fn permissions(&self) -> Permissions;

    /// The conditions of the license.
    fn conditions(&self) -> Conditions;

    /// The limitations of the license.
    fn limitations(&self) -> Limitations;
}

impl LicenseExt for AGPL_3_0_only {
    #[inline]
    fn permissions(&self) -> Permissions {
        Permissions {
            commercial_use: true,
            distribution: true,
            modification: true,
            patent_rights: true,
            private_use: true,
        }
    }

    #[inline]
    fn conditions(&self) -> Conditions {
        Conditions {
            disclose_sources: true,
            document_changes: true,
            license_and_copyright_notice: true,
            network_use_is_distribution: true,
            same_license: true,
        }
    }

    #[inline]
    fn limitations(&self) -> Limitations {
        Limitations {
            no_liability: true,
            no_trademark_rights: false,
            no_warranty: true,
            no_patent_rights: false,
        }
    }
}

impl LicenseExt for Apache_2_0 {
    #[inline]
    fn permissions(&self) -> Permissions {
        Permissions {
            commercial_use: true,
            distribution: true,
            modification: true,
            patent_rights: true,
            private_use: true,
        }
    }

    #[inline]
    fn conditions(&self) -> Conditions {
        Conditions {
            disclose_sources: false,
            document_changes: true,
            license_and_copyright_notice: true,
            network_use_is_distribution: false,
            same_license: false,
        }
    }

    #[inline]
    fn limitations(&self) -> Limitations {
        Limitations {
            no_liability: true,
            no_trademark_rights: true,
            no_warranty: true,
            no_patent_rights: false,
        }
    }
}

impl LicenseExt for CC0_1_0 {
    #[inline]
    fn permissions(&self) -> Permissions {
        Permissions {
            commercial_use: true,
            distribution: true,
            modification: true,
            patent_rights: false,
            private_use: true,
        }
    }

    #[inline]
    fn conditions(&self) -> Conditions {
        Conditions {
            disclose_sources: false,
            document_changes: false,
            license_and_copyright_notice: false,
            network_use_is_distribution: false,
            same_license: false,
        }
    }

    #[inline]
    fn limitations(&self) -> Limitations {
        Limitations {
            no_liability: true,
            no_trademark_rights: true,
            no_warranty: true,
            no_patent_rights: true,
        }
    }
}

impl LicenseExt for GPL_3_0_only {
    #[inline]
    fn permissions(&self) -> Permissions {
        Permissions {
            commercial_use: true,
            distribution: true,
            modification: true,
            patent_rights: true,
            private_use: true,
        }
    }

    #[inline]
    fn conditions(&self) -> Conditions {
        Conditions {
            disclose_sources: true,
            document_changes: true,
            license_and_copyright_notice: true,
            network_use_is_distribution: false,
            same_license: true,
        }
    }

    #[inline]
    fn limitations(&self) -> Limitations {
        Limitations {
            no_liability: true,
            no_trademark_rights: false,
            no_warranty: true,
            no_patent_rights: false,
        }
    }
}

impl LicenseExt for LGPL_3_0_only {
    #[inline]
    fn permissions(&self) -> Permissions {
        Permissions {
            commercial_use: true,
            distribution: true,
            modification: true,
            patent_rights: true,
            private_use: true,
        }
    }

    #[inline]
    fn conditions(&self) -> Conditions {
        Conditions {
            disclose_sources: true,
            document_changes: true,
            license_and_copyright_notice: true,
            network_use_is_distribution: false,
            same_license: true,
        }
    }

    #[inline]
    fn limitations(&self) -> Limitations {
        Limitations {
            no_liability: true,
            no_trademark_rights: false,
            no_warranty: true,
            no_patent_rights: false,
        }
    }
}

impl LicenseExt for MIT {
    #[inline]
    fn permissions(&self) -> Permissions {
        Permissions {
            commercial_use: true,
            distribution: true,
            modification: true,
            patent_rights: false,
            private_use: true,
        }
    }

    #[inline]
    fn conditions(&self) -> Conditions {
        Conditions {
            disclose_sources: false,
            document_changes: false,
            license_and_copyright_notice: true,
            network_use_is_distribution: false,
            same_license: false,
        }
    }

    #[inline]
    fn limitations(&self) -> Limitations {
        Limitations {
            no_liability: true,
            no_trademark_rights: false,
            no_warranty: true,
            no_patent_rights: false,
        }
    }
}

impl LicenseExt for MPL_2_0 {
    #[inline]
    fn permissions(&self) -> Permissions {
        Permissions {
            commercial_use: true,
            distribution: true,
            modification: true,
            patent_rights: true,
            private_use: true,
        }
    }

    #[inline]
    fn conditions(&self) -> Conditions {
        Conditions {
            disclose_sources: true,
            document_changes: false,
            license_and_copyright_notice: true,
            network_use_is_distribution: false,
            same_license: true,
        }
    }

    #[inline]
    fn limitations(&self) -> Limitations {
        Limitations {
            no_liability: true,
            no_trademark_rights: true,
            no_warranty: true,
            no_patent_rights: false,
        }
    }
}

impl LicenseExt for Unlicense {
    #[inline]
    fn permissions(&self) -> Permissions {
        Permissions {
            commercial_use: true,
            distribution: true,
            modification: true,
            patent_rights: false,
            private_use: true,
        }
    }

    #[inline]
    fn conditions(&self) -> Conditions {
        Conditions {
            disclose_sources: false,
            document_changes: false,
            license_and_copyright_notice: false,
            network_use_is_distribution: false,
            same_license: false,
        }
    }

    #[inline]
    fn limitations(&self) -> Limitations {
        Limitations {
            no_liability: true,
            no_trademark_rights: false,
            no_warranty: true,
            no_patent_rights: false,
        }
    }
}

/// The permissions of the license.
#[derive(Copy, Clone, Debug, Hash, Ord, PartialOrd, Eq, PartialEq)]
pub struct Permissions {
    /// May be used for commercial purposes.
    pub commercial_use: bool,
    /// May be distributed.
    pub distribution: bool,
    /// May be modified.
    pub modification: bool,
    /// Provides an express grant of patent rights from contributors.
    pub patent_rights: bool,
    /// May be used for private purposes.
    pub private_use: bool,
}

impl Display for Permissions {
    #[inline]
    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.
#[derive(Copy, Clone, Debug, Hash, Ord, PartialOrd, Eq, PartialEq)]
pub struct Conditions {
    /// Source code must be made available when the software is distributed.
    pub disclose_sources: bool,
    /// Changes made to the code must be documented.
    pub document_changes: bool,
    /// The license and copyright notice must be included with the software.
    pub license_and_copyright_notice: bool,
    /// Users who interact with the software via network are
    /// given the right to receive a copy of the source code.
    pub network_use_is_distribution: bool,
    /// Modifications must be released under the same license.
    pub same_license: bool,
}

impl Display for Conditions {
    #[inline]
    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.
#[derive(Copy, Clone, Debug, Hash, Ord, PartialOrd, Eq, PartialEq)]
pub struct Limitations {
    /// Includes a limitation of liability.
    pub no_liability: bool,
    /// Does not grant trademark rights.
    pub no_trademark_rights: bool,
    /// Does not provide any warranty.
    pub no_warranty: bool,
    /// Does not provide any rights in the patents of contributors.
    pub no_patent_rights: bool,
}

impl Display for Limitations {
    #[inline]
    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(())
    }
}

/// Returns an extension license based on the provided id.
///
/// # Examples
/// ```
/// # use license::{MIT, License};
/// let mit = license::from_id_ext(MIT.id()).unwrap();
/// assert_eq!(mit.id(), MIT.id());
/// ```
#[inline]
pub fn from_id_ext(id: &str) -> Option<&'static dyn LicenseExt> {
    match id {
        "AGPL-3.0-only" => Some(&AGPL_3_0_only),
        "Apache-2.0" => Some(&Apache_2_0),
        "CC0-1.0" => Some(&CC0_1_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),
        "Unlicense" => Some(&Unlicense),
        _ => None,
    }
}

/// Returns an extension license based on the provided text.
///
/// # Examples
/// ```
/// # use license::{MIT, License};
/// let mit = license::from_text_ext(MIT.text()).unwrap();
/// assert_eq!(mit.id(), MIT.id());
/// ```
#[inline]
pub fn from_text_ext(text: &str) -> Option<&'static dyn LicenseExt> {
    let v2 = text.contains("Version 2.0");
    let v3 = text.contains("Version 3");
    if text.contains("MIT License") {
        Some(&MIT)
    } else if v2 && text.contains("Apache License") {
        Some(&Apache_2_0)
    } else if v3 && text.contains("GNU GENERAL PUBLIC LICENSE") {
        Some(&GPL_3_0_only)
    } else if v2 && text.contains("Mozilla Public License") {
        Some(&MPL_2_0)
    } else if text
        .contains("This is free and unencumbered software released into the public domain.")
    {
        Some(&Unlicense)
    } else if v3 && text.contains("GNU LESSER GENERAL PUBLIC LICENSE") {
        Some(&LGPL_3_0_only)
    } else if v3 && text.contains("GNU AFFERO GENERAL PUBLIC LICENSE") {
        Some(&AGPL_3_0_only)
    } else if text.contains("CC0 1.0 Universal") {
        Some(&CC0_1_0)
    } else {
        None
    }
}

#[cfg(test)]
mod tests {
    use crate::*;

    #[test]
    fn from_text_ext_agpl3() {
        let agpl3 = from_text_ext(AGPL_3_0_only.text()).unwrap();
        assert_eq!(agpl3.id(), AGPL_3_0_only.id());
    }

    #[test]
    fn from_text_ext_apache2() {
        let apache2 = from_text_ext(Apache_2_0.text()).unwrap();
        assert_eq!(apache2.id(), Apache_2_0.id());
    }

    #[test]
    fn from_text_ext_cc01() {
        let cc01 = from_text_ext(CC0_1_0.text()).unwrap();
        assert_eq!(cc01.id(), CC0_1_0.id());
    }

    #[test]
    fn from_text_ext_gpl3() {
        let gpl3 = from_text_ext(GPL_3_0_only.text()).unwrap();
        assert_eq!(gpl3.id(), GPL_3_0_only.id());
    }

    #[test]
    fn from_text_ext_lgpl3() {
        let lgpl3 = from_text_ext(LGPL_3_0_only.text()).unwrap();
        assert_eq!(lgpl3.id(), LGPL_3_0_only.id());
    }

    #[test]
    fn from_text_ext_mit() {
        let mit = from_text_ext(MIT.text()).unwrap();
        assert_eq!(mit.id(), MIT.id());
    }

    #[test]
    fn from_text_ext_mpl2() {
        let mpl2 = from_text_ext(MPL_2_0.text()).unwrap();
        assert_eq!(mpl2.id(), MPL_2_0.id());
    }

    #[test]
    fn from_text_ext_unlicense() {
        let unlicense = from_text_ext(Unlicense.text()).unwrap();
        assert_eq!(unlicense.id(), Unlicense.id());
    }
}