alpm_types/license.rs
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
use std::fmt::{Display, Formatter};
use std::str::FromStr;
use spdx::Expression;
use crate::Error;
/// Represents a license expression that can be either a valid SPDX identifier
/// or a non-standard one.
///
/// ## Examples
/// ```
/// use std::str::FromStr;
///
/// use alpm_types::License;
///
/// # fn main() -> Result<(), alpm_types::Error> {
/// // Create License from a valid SPDX identifier
/// let license = License::from_str("MIT")?;
/// assert!(license.is_spdx());
/// assert_eq!(license.to_string(), "MIT");
///
/// // Create License from an invalid/non-SPDX identifier
/// let license = License::from_str("My-Custom-License")?;
/// assert!(!license.is_spdx());
/// assert_eq!(license.to_string(), "My-Custom-License");
/// # Ok(())
/// # }
/// ```
#[derive(Debug, Clone, PartialEq)]
pub enum License {
Spdx(Box<spdx::Expression>), // Boxed to avoid large allocations
Unknown(String),
}
impl License {
/// Creates a new license
///
/// This function accepts both SPDX and non-standard identifiers
/// and it is the same as as calling [`License::from_str`]
pub fn new(license: String) -> Result<Self, Error> {
Self::from_valid_spdx(license.clone()).or(Ok(Self::Unknown(license)))
}
/// Creates a new license from a valid SPDX identifier
///
/// ## Examples
///
/// ```
/// use alpm_types::Error;
/// use alpm_types::License;
///
/// # fn main() -> Result<(), alpm_types::Error> {
/// let license = License::from_valid_spdx("Apache-2.0".to_string())?;
/// assert!(license.is_spdx());
/// assert_eq!(license.to_string(), "Apache-2.0");
///
/// assert!(License::from_valid_spdx("GPL-0.0".to_string()).is_err());
/// assert!(License::from_valid_spdx("Custom-License".to_string()).is_err());
///
/// assert_eq!(
/// License::from_valid_spdx("GPL-2.0".to_string()),
/// Err(Error::DeprecatedLicense("GPL-2.0".to_string()))
/// );
/// # Ok(())
/// # }
/// ```
///
/// ## Errors
///
/// Returns an error if the given input cannot be parsed or is a deprecated license.
pub fn from_valid_spdx(identifier: String) -> Result<Self, Error> {
let expression = Expression::parse(&identifier)?;
if spdx::license_id(&identifier)
.map(|v| v.is_deprecated())
.unwrap_or(false)
{
return Err(Error::DeprecatedLicense(identifier));
}
Ok(Self::Spdx(Box::new(expression)))
}
/// Returns `true` if the license is a valid SPDX identifier
pub fn is_spdx(&self) -> bool {
matches!(self, License::Spdx(_))
}
}
impl FromStr for License {
type Err = Error;
/// Creates a new `License` instance from a string slice.
///
/// If the input is a valid SPDX license expression,
/// it will be marked as such; otherwise, it will be treated as
/// a non-standard license identifier.
///
/// ## Examples
///
/// ```
/// use std::str::FromStr;
///
/// use alpm_types::License;
///
/// # fn main() -> Result<(), alpm_types::Error> {
/// let license = License::from_str("Apache-2.0")?;
/// assert!(license.is_spdx());
/// assert_eq!(license.to_string(), "Apache-2.0");
///
/// let license = License::from_str("NonStandard-License")?;
/// assert!(!license.is_spdx());
/// assert_eq!(license.to_string(), "NonStandard-License");
/// # Ok(())
/// # }
/// ```
///
/// ## Errors
///
/// Returns an error if the given input is a deprecated SPDX license.
fn from_str(s: &str) -> Result<Self, Self::Err> {
Self::new(s.to_string())
}
}
impl Display for License {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match &self {
License::Spdx(expr) => write!(f, "{expr}"),
License::Unknown(s) => write!(f, "{s}"),
}
}
}
#[cfg(test)]
mod tests {
use rstest::rstest;
use super::*;
#[rstest]
#[case("MIT", License::Spdx(Box::new(Expression::parse("MIT").unwrap())))]
#[case("Apache-2.0", License::Spdx(Box::new(Expression::parse("Apache-2.0").unwrap())))]
#[case("Apache-2.0+", License::Spdx(Box::new(Expression::parse("Apache-2.0+").unwrap())))]
#[case(
"Apache-2.0 WITH LLVM-exception",
License::Spdx(Box::new(Expression::parse("Apache-2.0 WITH LLVM-exception").unwrap()))
)]
#[case("GPL-3.0-or-later", License::Spdx(Box::new(Expression::parse("GPL-3.0-or-later").unwrap())))]
#[case("HPND-Fenneberg-Livingston", License::Spdx(Box::new(Expression::parse("HPND-Fenneberg-Livingston").unwrap())))]
#[case(
"NonStandard-License",
License::Unknown(String::from("NonStandard-License"))
)]
fn test_parse_license(
#[case] input: &str,
#[case] expected: License,
) -> testresult::TestResult<()> {
let license = input.parse::<License>()?;
assert_eq!(license, expected);
assert_eq!(license.to_string(), input.to_string());
Ok(())
}
#[rstest]
#[case("Apache-2.0 WITH",
Err(spdx::ParseError {
original: String::from("Apache-2.0 WITH"),
span: 15..15,
reason: spdx::error::Reason::Unexpected(&["<exception>"])
}.into())
)]
#[case("Custom-License",
Err(spdx::ParseError {
original: String::from("Custom-License"),
span: 0..14,
reason: spdx::error::Reason::UnknownTerm
}.into())
)]
fn test_invalid_spdx(#[case] input: &str, #[case] expected: Result<License, Error>) {
let result = License::from_valid_spdx(input.to_string());
assert_eq!(result, expected);
}
#[rstest]
#[case("GPL-2.0")]
#[case("BSD-2-Clause-FreeBSD")]
fn test_deprecated_spdx(#[case] input: &str) {
let result = License::from_valid_spdx(input.to_string());
assert_eq!(result, Err(Error::DeprecatedLicense(input.to_string())));
}
#[rstest]
#[case("MIT", true)]
#[case("Custom-License", false)]
fn test_license_kind(#[case] input: &str, #[case] is_spdx: bool) -> testresult::TestResult<()> {
let spdx_license = License::from_str(input)?;
assert_eq!(spdx_license.is_spdx(), is_spdx);
Ok(())
}
}