Skip to main content

datumesh_engine/
acceptance.rs

1//! The local acceptance policy of licence inheritance (engine 0.6.1, E1; DECISIONS-round-protocol
2//! D4, D4.1): on an unrooted host, a restricted input licence that the operator has accepted may
3//! be computed over, where SPEC 8.5 would refuse it.
4
5use std::collections::BTreeSet;
6
7use datumesh_core::{lexical, LicenceReason};
8
9/// The licence of a local result over two or more distinct accepted restricted licences (E1,
10/// D4.1). Such a result can never be published or exported (the CLI refuses it).
11pub const LOCAL_DERIVATION_LICENCE: &str = "LicenseRef-datumesh-local-derivation";
12
13/// The open identifiers of SPEC 8.5's open tiers (`spec-0.6.2`), as core lists them. Core keeps
14/// them private, so this copy must agree with core: the unit tests check that core gives each of
15/// them a licence when it is the only input.
16const 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
19/// Whether `licence` is open in SPEC 8.5: one of the open identifiers, or a `public:<https URL>`
20/// licence (the public information tier). Every other licence, an https URL included, is
21/// restricted.
22pub(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/// The outcome of the acceptance rule for one request: the licence the result carries, or the
28/// reason it is refused, as core gives it. Engine-internal: core's `Inheritance` is
29/// non-exhaustive and cannot be built here.
30#[derive(Clone, Debug, PartialEq, Eq)]
31pub(crate) struct Rule {
32    /// The result's licence; `None` when the mesh may not compute the cell.
33    pub(crate) licence: Option<String>,
34    /// Why: core's reason. For a result given by acceptance, the reason of the refusal it
35    /// overrides (`not_declared`, `declaration_not_open` or `restricted_conflict`).
36    pub(crate) reason: LicenceReason,
37}
38
39/// What the acceptance rule decides for a refusal of core (E1).
40#[derive(Debug, PartialEq, Eq)]
41pub(crate) enum Verdict {
42    /// Computed under this licence.
43    Give(String),
44    /// Restricted licences not accepted: the sorted list of the missing ones.
45    Missing(Vec<String>),
46}
47
48/// Whether core's refusal `reason` is one the acceptance rule may override: a restricted input
49/// not declared (`not_declared`), declared but not open (`declaration_not_open`), or two
50/// restricted licences in conflict (`restricted_conflict`). A share-alike conflict is never
51/// overridden.
52pub(crate) fn overridable(reason: LicenceReason) -> bool {
53    matches!(reason, LicenceReason::NotDeclared | LicenceReason::DeclarationNotOpen | LicenceReason::RestrictedConflict)
54}
55
56/// The acceptance rule (E1) over the input licences `licences`, for a refusal `reason` of core
57/// that [`overridable`] allows. Every restricted input licence must be in `accepted`: then one
58/// distinct restricted licence is the result's licence, and two or more give
59/// [`LOCAL_DERIVATION_LICENCE`]. Otherwise the missing licences are named.
60pub(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        // Core gives an open licence alone (no declarations needed) with the reason `open`.
83        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}