use std::collections::BTreeSet;
use datumesh_core::{lexical, LicenceReason};
pub const LOCAL_DERIVATION_LICENCE: &str = "LicenseRef-datumesh-local-derivation";
const OPEN_IDENTIFIERS: &[&str] =
&["CC0-1.0", "PDDL-1.0", "MIT", "Apache-2.0", "CC-BY-4.0", "OGL-UK-3.0", "CC-BY-SA-4.0", "ODbL-1.0"];
pub(crate) fn is_open(licence: &str) -> bool {
OPEN_IDENTIFIERS.contains(&licence)
|| (licence.starts_with("public:") && lexical::check_licence(licence, "$").is_ok())
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) struct Rule {
pub(crate) licence: Option<String>,
pub(crate) reason: LicenceReason,
}
#[derive(Debug, PartialEq, Eq)]
pub(crate) enum Verdict {
Give(String),
Missing(Vec<String>),
}
pub(crate) fn overridable(reason: LicenceReason) -> bool {
matches!(reason, LicenceReason::NotDeclared | LicenceReason::DeclarationNotOpen | LicenceReason::RestrictedConflict)
}
pub(crate) fn decide(licences: &[String], accepted: &BTreeSet<String>) -> Verdict {
let restricted: BTreeSet<&str> = licences.iter().map(String::as_str).filter(|l| !is_open(l)).collect();
let missing: Vec<String> = restricted.iter().filter(|l| !accepted.contains(**l)).map(|l| l.to_string()).collect();
if !missing.is_empty() {
return Verdict::Missing(missing);
}
match restricted.len() {
1 => Verdict::Give(restricted.iter().next().map_or_else(String::new, |l| l.to_string())),
_ => Verdict::Give(LOCAL_DERIVATION_LICENCE.to_string()),
}
}
#[cfg(test)]
mod tests {
use super::*;
use datumesh_core::inherit_licence;
const FN: &str = "rates.example.org/src";
const ISSUED: &str = "2024-03-15T16:00:00.000000Z";
#[test]
fn each_open_identifier_is_open_to_core_alone() {
for id in OPEN_IDENTIFIERS {
assert!(is_open(id), "{id}");
let r = inherit_licence([*id], None, FN, ISSUED, "local.invalid");
assert_eq!(r.licence.as_deref(), Some(*id), "{id}");
assert_eq!(r.reason, LicenceReason::Open, "{id}");
}
}
#[test]
fn a_public_information_licence_is_open_to_core_alone() {
let id = "public:https://www.gov.example/licence";
assert!(is_open(id));
let r = inherit_licence([id], None, FN, ISSUED, "local.invalid");
assert_eq!(r.licence.as_deref(), Some(id));
}
#[test]
fn core_refuses_a_restricted_licence_alone_and_so_do_we_class_it_restricted() {
for id in ["https://rates.example.org/licences/restricted", "cc0-1.0", "BSD-3-Clause"] {
assert!(!is_open(id), "{id}");
let r = inherit_licence([id], None, FN, ISSUED, "local.invalid");
assert_eq!(r.reason, LicenceReason::NotDeclared, "{id}");
}
}
#[test]
fn decide_gives_the_one_licence_or_the_local_derivation() {
let acc: BTreeSet<String> =
["https://a.example/r", "https://b.example/r"].iter().map(|s| s.to_string()).collect();
let one = vec!["CC-BY-4.0".to_string(), "https://a.example/r".to_string()];
assert_eq!(decide(&one, &acc), Verdict::Give("https://a.example/r".into()));
let two = vec!["https://a.example/r".to_string(), "https://b.example/r".to_string()];
assert_eq!(decide(&two, &acc), Verdict::Give(LOCAL_DERIVATION_LICENCE.into()));
let missing = vec!["https://a.example/r".to_string(), "https://c.example/r".to_string()];
assert_eq!(decide(&missing, &acc), Verdict::Missing(vec!["https://c.example/r".into()]));
}
}