datumesh_engine/
acceptance.rs1use std::collections::BTreeSet;
6
7use datumesh_core::{lexical, LicenceReason};
8
9pub const LOCAL_DERIVATION_LICENCE: &str = "LicenseRef-datumesh-local-derivation";
12
13const OPEN_IDENTIFIERS: &[&str] =
17 &["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"];
18
19pub(crate) fn is_open(licence: &str) -> bool {
23 OPEN_IDENTIFIERS.contains(&licence)
24 || (licence.starts_with("public:") && lexical::check_licence(licence, "$").is_ok())
25}
26
27#[derive(Clone, Debug, PartialEq, Eq)]
31pub(crate) struct Rule {
32 pub(crate) licence: Option<String>,
34 pub(crate) reason: LicenceReason,
37}
38
39#[derive(Debug, PartialEq, Eq)]
41pub(crate) enum Verdict {
42 Give(String),
44 Missing(Vec<String>),
46}
47
48pub(crate) fn overridable(reason: LicenceReason) -> bool {
53 matches!(reason, LicenceReason::NotDeclared | LicenceReason::DeclarationNotOpen | LicenceReason::RestrictedConflict)
54}
55
56pub(crate) fn decide(licences: &[String], accepted: &BTreeSet<String>) -> Verdict {
61 let restricted: BTreeSet<&str> = licences.iter().map(String::as_str).filter(|l| !is_open(l)).collect();
62 let missing: Vec<String> = restricted.iter().filter(|l| !accepted.contains(**l)).map(|l| l.to_string()).collect();
63 if !missing.is_empty() {
64 return Verdict::Missing(missing);
65 }
66 match restricted.len() {
67 1 => Verdict::Give(restricted.iter().next().map_or_else(String::new, |l| l.to_string())),
68 _ => Verdict::Give(LOCAL_DERIVATION_LICENCE.to_string()),
69 }
70}
71
72#[cfg(test)]
73mod tests {
74 use super::*;
75 use datumesh_core::inherit_licence;
76
77 const FN: &str = "rates.example.org/src";
78 const ISSUED: &str = "2024-03-15T16:00:00.000000Z";
79
80 #[test]
81 fn each_open_identifier_is_open_to_core_alone() {
82 for id in OPEN_IDENTIFIERS {
84 assert!(is_open(id), "{id}");
85 let r = inherit_licence([*id], None, FN, ISSUED, "local.invalid");
86 assert_eq!(r.licence.as_deref(), Some(*id), "{id}");
87 assert_eq!(r.reason, LicenceReason::Open, "{id}");
88 }
89 }
90
91 #[test]
92 fn a_public_information_licence_is_open_to_core_alone() {
93 let id = "public:https://www.gov.example/licence";
94 assert!(is_open(id));
95 let r = inherit_licence([id], None, FN, ISSUED, "local.invalid");
96 assert_eq!(r.licence.as_deref(), Some(id));
97 }
98
99 #[test]
100 fn core_refuses_a_restricted_licence_alone_and_so_do_we_class_it_restricted() {
101 for id in ["https://rates.example.org/licences/restricted", "cc0-1.0", "BSD-3-Clause"] {
102 assert!(!is_open(id), "{id}");
103 let r = inherit_licence([id], None, FN, ISSUED, "local.invalid");
104 assert_eq!(r.reason, LicenceReason::NotDeclared, "{id}");
105 }
106 }
107
108 #[test]
109 fn decide_gives_the_one_licence_or_the_local_derivation() {
110 let acc: BTreeSet<String> =
111 ["https://a.example/r", "https://b.example/r"].iter().map(|s| s.to_string()).collect();
112 let one = vec!["CC-BY-4.0".to_string(), "https://a.example/r".to_string()];
113 assert_eq!(decide(&one, &acc), Verdict::Give("https://a.example/r".into()));
114 let two = vec!["https://a.example/r".to_string(), "https://b.example/r".to_string()];
115 assert_eq!(decide(&two, &acc), Verdict::Give(LOCAL_DERIVATION_LICENCE.into()));
116 let missing = vec!["https://a.example/r".to_string(), "https://c.example/r".to_string()];
117 assert_eq!(decide(&missing, &acc), Verdict::Missing(vec!["https://c.example/r".into()]));
118 }
119}