Skip to main content

vibe/cbor_ast/cbor_ld/
terms.rs

1//! W3C CBOR-LD Tag Constants & Static/Dynamic Ontology Term Compression.
2
3pub const TAG_CBOR_LD_UNCOMPRESSED: u64 = 0x0500;
4pub const TAG_CBOR_LD_COMPRESSED: u64 = 0x0501;
5pub const TAG_VIBE_QUANTITY: u64 = 4201;
6pub const TAG_VIBE_INSTANT: u64 = 4202;
7pub const TAG_VIBE_DURATION: u64 = 4203;
8pub const TAG_VIBE_QUIN_REF: u64 = 4204;
9pub const TAG_VIBE_DISCLOSURE_DENIED: u64 = 4205;
10pub const TAG_VIBE_IRI: u64 = 32;
11
12pub(crate) const TAG_VIBE_U64: u64 = 4206;
13pub(crate) const TAG_VIBE_PREFIXED: u64 = 4207;
14pub(crate) const TAG_VIBE_RECEIPT: u64 = 4208;
15pub(crate) const TAG_VIBE_OK: u64 = 4209;
16pub(crate) const TAG_VIBE_ERR: u64 = 4210;
17pub(crate) const TAG_VIBE_BLANK: u64 = 4211;
18pub(crate) const TAG_VIBE_VAR: u64 = 4212;
19pub(crate) const TAG_VIBE_TRIPLE: u64 = 4213;
20pub(crate) const TAG_VIBE_REIFIED: u64 = 4214;
21pub(crate) const TAG_VIBE_FRAME: u64 = 4215;
22pub(crate) const TAG_VIBE_POSE: u64 = 4216;
23pub(crate) const TAG_VIBE_TRANSFORM: u64 = 4217;
24pub(crate) const TAG_VIBE_FIELD_REF: u64 = 4218;
25pub(crate) const TAG_VIBE_MATERIAL_REF: u64 = 4219;
26pub(crate) const TAG_VIBE_TENSOR_REF: u64 = 4220;
27pub(crate) const TAG_VIBE_GEOMETRY_REF: u64 = 4221;
28pub(crate) const TAG_VIBE_ASSET_REF: u64 = 4222;
29pub(crate) const TAG_VIBE_WORLDLINE: u64 = 4223;
30pub(crate) const TAG_VIBE_ENUM: u64 = 4224;
31pub(crate) const TAG_VIBE_LAMBDA: u64 = 4225;
32pub(crate) const TAG_VIBE_SPECIES_REF: u64 = 4226;
33pub(crate) const TAG_VIBE_MIXTURE: u64 = 4227;
34pub(crate) const TAG_VIBE_CONSERVATION: u64 = 4228;
35pub(crate) const TAG_VIBE_CAUSAL: u64 = 4229;
36pub(crate) const TAG_VIBE_DISCLOSURE_BOUNDARY: u64 = 4230;
37
38pub const DEFAULT_CONTEXT_IRI: &str = "https://qualia.world/context/ui/v1";
39pub const DYNAMIC_TERM_BIT: u64 = 1u64 << 59;
40pub const TOKEN_60BIT_MASK: u64 = 0x0FFF_FFFF_FFFF_FFFF;
41
42/// Built-in static term table for Qualia/Vibe/UI/Form/QUDT/SHACL/RDF terms (`1..=1023`).
43pub const STATIC_TERMS: &[(u64, &str)] = &[
44    (1, "@context"),
45    (2, "@id"),
46    (3, "@type"),
47    (4, "@value"),
48    (5, "@graph"),
49    (6, "@provider_host"),
50    (7, "@provider_tld"),
51    (10, "https://qualia.world/context/ui/v1"),
52    (11, "https://qualia.world/context/clinical/v1"),
53    (12, "https://qualia.world/context/vibe/v1"),
54    (20, "ui:patch"),
55    (21, "ui:target_id"),
56    (22, "ui:property"),
57    (23, "ui:value"),
58    (24, "ui:items"),
59    (25, "ui:kind"),
60    (26, "ui:text"),
61    (27, "ui:disabled"),
62    (28, "ui:icon"),
63    (29, "ui:color"),
64    (30, "form:id"),
65    (31, "form:fields"),
66    (32, "form:valid"),
67    (33, "form:violations"),
68    (40, "qudt:KiloPascal"),
69    (41, "qudt:Pascal"),
70    (42, "qudt:Bar"),
71    (43, "qudt:Meter"),
72    (44, "qudt:Second"),
73    (45, "qudt:Kilogram"),
74    (46, "qudt:DimensionlessUnit"),
75    (47, "qudt:Kelvin"),
76    (48, "qudt:Mole"),
77    (50, "sh:NodeShape"),
78    (51, "sh:PropertyShape"),
79    (52, "sh:path"),
80    (53, "sh:minCount"),
81    (54, "sh:maxCount"),
82    (55, "sh:datatype"),
83    (60, "rdf:type"),
84    (61, "rdfs:label"),
85    (62, "xsd:string"),
86    (63, "xsd:integer"),
87    (64, "xsd:double"),
88    (65, "xsd:boolean"),
89];
90
91/// Looks up a compact static term ID (`1..=1023`) for a known ontology term or IRI.
92pub fn lookup_static_term_id(iri: &str) -> Option<u64> {
93    for &(id, term) in STATIC_TERMS {
94        if term == iri {
95            return Some(id);
96        }
97    }
98    None
99}
100
101/// Looks up the static IRI or term string for a compact static term ID (`1..=1023`).
102pub fn lookup_static_term_iri(term_id: u64) -> Option<&'static str> {
103    for &(id, term) in STATIC_TERMS {
104        if id == term_id {
105            return Some(term);
106        }
107    }
108    None
109}
110
111/// Compresses an IRI or property term into either a compact static table ID (`1..=1023`)
112/// or a deterministic 60-bit FNV-1a token with bit 59 set (`DYNAMIC_TERM_BIT`).
113pub fn compress_iri(iri: &str, context_iri: &str) -> u64 {
114    if let Some(id) = lookup_static_term_id(iri) {
115        return id;
116    }
117    if let Some(hex_part) = iri.strip_prefix("did:q42:term:") {
118        if let Ok(parsed) = u64::from_str_radix(hex_part, 16) {
119            if (parsed & DYNAMIC_TERM_BIT) != 0 && (parsed & !TOKEN_60BIT_MASK) == 0 {
120                return parsed;
121            }
122        }
123    }
124    let mut hash: u64 = 0xcbf29ce484222325;
125    for &b in context_iri.as_bytes() {
126        hash ^= b as u64;
127        hash = hash.wrapping_mul(0x100000001b3);
128    }
129    hash ^= b'|' as u64;
130    hash = hash.wrapping_mul(0x100000001b3);
131    for &b in iri.as_bytes() {
132        hash ^= b as u64;
133        hash = hash.wrapping_mul(0x100000001b3);
134    }
135    (hash & TOKEN_60BIT_MASK) | DYNAMIC_TERM_BIT
136}
137
138/// Expands a compressed term ID back to a static IRI/term or a deterministic
139/// `did:q42:term:<hex>` topological identifier for dynamic 60-bit tokens.
140pub fn expand_term(term_id: u64, _context_iri: &str) -> Option<String> {
141    if let Some(iri) = lookup_static_term_iri(term_id) {
142        return Some(iri.to_string());
143    }
144    if (term_id & DYNAMIC_TERM_BIT) != 0 && (term_id & !TOKEN_60BIT_MASK) == 0 {
145        return Some(format!("did:q42:term:{term_id:015x}"));
146    }
147    None
148}