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
#![deny(missing_docs)]
#![cfg_attr(not(feature = "std"), no_std)]
#[cfg(feature = "std")]
extern crate std as core;
use core::{
convert::{TryFrom, TryInto},
fmt,
};
#[macro_use]
mod macros;
mod util;
pub mod expr;
pub mod spdx;
#[doc(inline)]
pub use self::{
expr::Expr,
spdx::SpdxLicense,
};
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(untagged))]
pub enum License {
Spdx(SpdxLicense),
#[doc(hidden)]
_NonExhaustive(util::Never),
}
impl From<SpdxLicense> for License {
#[inline]
fn from(l: SpdxLicense) -> Self {
Self::Spdx(l)
}
}
impl fmt::Display for License {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Self::Spdx(l) => l.fmt(f),
Self::_NonExhaustive(never) => never.consume(),
}
}
}
impl<'a> TryFrom<&'a str> for License {
type Error = ParseError<'a>;
fn try_from(s: &'a str) -> Result<Self, Self::Error> {
let s = s.trim();
SpdxLicense::parse(s).map(|l| l.into())
}
}
impl License {
#[inline]
pub fn parse<'a, I>(input: I) -> Result<Self, ParseError<'a>>
where I: TryInto<Self, Error = ParseError<'a>>
{
input.try_into()
}
#[inline]
pub fn id(&self) -> &str {
match self {
License::Spdx(l) => l.id(),
License::_NonExhaustive(never) => never.consume(),
}
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum ParseError<'a> {
Empty,
UnknownLicenseId(&'a str),
}
impl fmt::Display for ParseError<'_> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
ParseError::Empty => {
write!(f, "empty string provided")
},
ParseError::UnknownLicenseId(id) => {
write!(f, "'{}' is not a known license ID", id)
},
}
}
}