Skip to main content

vibe/vocab/
mod.rs

1//! Vocab chunks: bounded N3/Turtle slices with a content hash (P17.1).
2//!
3//! Ontology extends Vibe by compiling **referenced terms**, not by shipping
4//! SNOMED (or any whole ontology) in the binary. Canonical IRIs stay so
5//! SPARQL/N3/SHACL need no rename layer. Versioning (hash-lock vs latest)
6//! is a principal decision (P17.5); this module stores the hash and can
7//! verify a lock, but never fetches "latest".
8
9mod parse;
10mod shake;
11
12pub use parse::{parse_chunk, VocabChunk, VocabError, VocabTerm, MAX_CHUNK_BYTES, MAX_TERMS};
13pub use shake::{project_referenced_iris, tree_shake, unknown_prefixed};
14
15#[cfg(test)]
16mod tests {
17    use super::*;
18    use crate::parse_program;
19
20    const CLINIC: &str = r#"
21# vibe-vocab-0.1
22@prefix snomed: <http://snomed.info/id/> .
23@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
24
25snomed:386661006 rdfs:label "Fever" .
26snomed:386661006 rdfs:subClassOf snomed:64572001 .
27snomed:64572001 rdfs:label "Disease" .
28snomed:404684003 rdfs:label "Clinical finding" .
29snomed:64572001 rdfs:subClassOf snomed:404684003 .
30"#;
31
32    #[test]
33    fn parse_clinic_chunk_hashes_stably() {
34        let a = parse_chunk(CLINIC.as_bytes()).expect("parse");
35        let b = parse_chunk(CLINIC.as_bytes()).expect("parse");
36        assert_eq!(a.content_hash, b.content_hash);
37        assert!(a.terms.len() >= 3);
38        assert!(a.prefix_iri("snomed").unwrap().contains("snomed.info"));
39    }
40
41    #[test]
42    fn tree_shake_keeps_parents() {
43        let chunk = parse_chunk(CLINIC.as_bytes()).expect("parse");
44        let small = tree_shake(&chunk, &["snomed:386661006"]);
45        let iris: Vec<&str> = small.terms.iter().map(|t| t.iri.as_str()).collect();
46        assert!(iris.iter().any(|i| i.ends_with("386661006")));
47        assert!(iris.iter().any(|i| i.ends_with("64572001")));
48        assert!(iris.iter().any(|i| i.ends_with("404684003")));
49        assert!(small.terms.len() < chunk.terms.len() || chunk.terms.len() == 3);
50    }
51
52    #[test]
53    fn oversized_chunk_fails_closed() {
54        let mut src = String::from("@prefix x: <http://example.org/> .\n");
55        while src.len() < MAX_CHUNK_BYTES + 8 {
56            src.push_str("x:a rdfs:label \"pad\" .\n");
57        }
58        assert!(matches!(
59            parse_chunk(src.as_bytes()),
60            Err(VocabError::TooLarge { .. })
61        ));
62    }
63
64    #[test]
65    fn unknown_prefixed_is_reported() {
66        let chunk = parse_chunk(CLINIC.as_bytes()).expect("parse");
67        let prog = parse_program(
68            r#"
69            prefix snomed: <http://snomed.info/id/>;
70            pure fn f() { return snomed:not_a_concept; }
71            "#,
72        )
73        .expect("parse");
74        let unknown = unknown_prefixed(&prog, &[chunk]);
75        assert!(
76            unknown.iter().any(|(_, local, _)| local == "not_a_concept"),
77            "{unknown:?}"
78        );
79    }
80
81    #[test]
82    fn project_iris_from_script() {
83        let chunk = parse_chunk(CLINIC.as_bytes()).expect("parse");
84        let prog = parse_program(
85            r#"
86            prefix snomed: <http://snomed.info/id/>;
87            pure fn f() { return snomed:386661006; }
88            "#,
89        )
90        .expect("parse");
91        let iris = project_referenced_iris(&prog, &chunk);
92        assert!(iris.iter().any(|i| i.ends_with("386661006")));
93    }
94
95    #[test]
96    fn lock_hex_round_trips() {
97        let chunk = parse_chunk(CLINIC.as_bytes()).expect("parse");
98        let hex = chunk.hash_hex();
99        assert_eq!(hex.len(), 64);
100        assert!(chunk.lock_matches(&hex));
101        assert!(!chunk.lock_matches("00"));
102    }
103}