qualia_core_db/domain_licence/
registry.rs1use serde::{Deserialize, Serialize};
10
11use super::verify::normalize_domain;
12
13pub const COMPILED_IN_LICENSED_DOMAINS: &[&str] = &[];
17
18#[repr(u8)]
20#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
21pub enum RegistrySource {
22 InstalledFile = 1,
24 WellKnown = 2,
26 DnsTxt = 3,
28 ChainAnchor = 4,
30}
31
32#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
34pub struct RegistryEntry {
35 pub domain: String,
37 pub licence_id: String,
39 #[serde(default)]
41 pub digest: String,
42 pub source: RegistrySource,
44}
45
46#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
48pub struct LicenceRegistry {
49 #[serde(default)]
50 pub entries: Vec<RegistryEntry>,
51}
52
53impl LicenceRegistry {
54 pub fn lookup(&self, domain: &str) -> Option<&RegistryEntry> {
56 let d = normalize_domain(domain);
57 self.entries.iter().find(|e| e.domain == d)
58 }
59
60 pub fn upsert(&mut self, entry: RegistryEntry) {
62 let d = normalize_domain(&entry.domain);
63 self.entries.retain(|e| e.domain != d);
64 self.entries.push(RegistryEntry { domain: d, ..entry });
65 }
66
67 pub fn remove(&mut self, domain: &str) -> bool {
69 let d = normalize_domain(domain);
70 let before = self.entries.len();
71 self.entries.retain(|e| e.domain != d);
72 self.entries.len() != before
73 }
74
75 pub fn is_declared(&self, domain: &str) -> bool {
77 let d = normalize_domain(domain);
78 COMPILED_IN_LICENSED_DOMAINS
79 .iter()
80 .any(|c| normalize_domain(c) == d)
81 || self.lookup(&d).is_some()
82 }
83
84 pub fn declared_domains(&self) -> Vec<String> {
86 let mut out: Vec<String> = self.entries.iter().map(|e| e.domain.clone()).collect();
87 for c in COMPILED_IN_LICENSED_DOMAINS {
88 let n = normalize_domain(c);
89 if !out.contains(&n) {
90 out.push(n);
91 }
92 }
93 out
94 }
95
96 pub fn to_json(&self) -> Result<String, serde_json::Error> {
98 serde_json::to_string_pretty(self)
99 }
100
101 pub fn from_json(text: &str) -> Result<Self, serde_json::Error> {
103 serde_json::from_str(text)
104 }
105}