Skip to main content

vibe/vocab/
parse.rs

1//! Bounded vocab-chunk parser. Not a full N3 engine.
2
3use std::collections::BTreeMap;
4
5pub const MAX_CHUNK_BYTES: usize = 256 * 1024;
6pub const MAX_TERMS: usize = 4096;
7
8#[derive(Debug, Clone, PartialEq, Eq)]
9pub struct VocabTerm {
10    pub iri: String,
11    pub label: Option<String>,
12    pub parents: Vec<String>,
13}
14
15#[derive(Debug, Clone, PartialEq, Eq)]
16pub struct VocabChunk {
17    pub prefixes: BTreeMap<String, String>,
18    pub terms: Vec<VocabTerm>,
19    pub content_hash: [u8; 32],
20}
21
22#[derive(Debug, Clone, PartialEq, Eq)]
23pub enum VocabError {
24    TooLarge { bytes: usize },
25    TooManyTerms { count: usize },
26    Empty,
27}
28
29impl std::fmt::Display for VocabError {
30    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
31        match self {
32            Self::TooLarge { bytes } => {
33                write!(f, "vocab chunk {bytes} bytes exceeds {MAX_CHUNK_BYTES}")
34            }
35            Self::TooManyTerms { count } => {
36                write!(f, "vocab chunk {count} terms exceeds {MAX_TERMS}")
37            }
38            Self::Empty => write!(f, "vocab chunk has no terms"),
39        }
40    }
41}
42
43impl VocabChunk {
44    /// Prefix IRI.
45    pub fn prefix_iri(&self, prefix: &str) -> Option<&str> {
46        self.prefixes.get(prefix).map(String::as_str)
47    }
48
49    /// Expands a prefixed name.
50    pub fn expand(&self, prefixed: &str) -> Option<String> {
51        let (p, local) = prefixed.split_once(':')?;
52        let base = self.prefix_iri(p)?;
53        Some(format!("{base}{local}"))
54    }
55
56    /// `true` when it has local.
57    pub fn has_local(&self, prefix: &str, local: &str) -> bool {
58        let Some(iri) = self.expand(&format!("{prefix}:{local}")) else {
59            return false;
60        };
61        self.terms.iter().any(|t| t.iri == iri)
62    }
63
64    /// Hash as hex.
65    pub fn hash_hex(&self) -> String {
66        self.content_hash
67            .iter()
68            .map(|b| format!("{b:02x}"))
69            .collect()
70    }
71
72    /// P17.5 lock check. Does not fetch latest.
73    pub fn lock_matches(&self, expected_hex: &str) -> bool {
74        expected_hex.eq_ignore_ascii_case(&self.hash_hex())
75    }
76}
77
78/// Parses chunk.
79pub fn parse_chunk(bytes: &[u8]) -> Result<VocabChunk, VocabError> {
80    if bytes.len() > MAX_CHUNK_BYTES {
81        return Err(VocabError::TooLarge { bytes: bytes.len() });
82    }
83    let src = std::str::from_utf8(bytes).unwrap_or("");
84    let hash = content_hash(bytes);
85    let mut prefixes = BTreeMap::new();
86    let mut by_iri: BTreeMap<String, VocabTerm> = BTreeMap::new();
87
88    for raw in src.lines() {
89        let line = raw.trim();
90        if line.is_empty() || line.starts_with('#') {
91            continue;
92        }
93        if let Some(rest) = line
94            .strip_prefix("@prefix ")
95            .or_else(|| line.strip_prefix("prefix "))
96        {
97            if let Some((name, iri)) = parse_prefix(rest) {
98                prefixes.insert(name, iri);
99            }
100            continue;
101        }
102        if let Some((s, p, o)) = parse_triple(line) {
103            let s_iri = expand(&prefixes, &s);
104            let p_iri = expand(&prefixes, &p);
105            let entry = by_iri.entry(s_iri.clone()).or_insert_with(|| VocabTerm {
106                iri: s_iri,
107                label: None,
108                parents: Vec::new(),
109            });
110            if p_iri.ends_with("label") || p == "rdfs:label" {
111                entry.label = Some(unquote(&o));
112            } else if p_iri.ends_with("subClassOf") || p == "rdfs:subClassOf" {
113                entry.parents.push(expand(&prefixes, &o));
114            }
115        }
116    }
117
118    if by_iri.len() > MAX_TERMS {
119        return Err(VocabError::TooManyTerms {
120            count: by_iri.len(),
121        });
122    }
123    let terms: Vec<VocabTerm> = by_iri.into_values().collect();
124    if terms.is_empty() {
125        return Err(VocabError::Empty);
126    }
127    Ok(VocabChunk {
128        prefixes,
129        terms,
130        content_hash: hash,
131    })
132}
133
134fn parse_prefix(rest: &str) -> Option<(String, String)> {
135    let rest = rest.trim().trim_end_matches('.').trim();
136    let (name, iri_part) = rest.split_once(':')?;
137    let iri = iri_part
138        .trim()
139        .trim_start_matches('<')
140        .trim_end_matches('>')
141        .trim()
142        .to_string();
143    if name.trim().is_empty() || iri.is_empty() {
144        return None;
145    }
146    Some((name.trim().to_string(), iri))
147}
148
149fn parse_triple(line: &str) -> Option<(String, String, String)> {
150    let line = line.trim().trim_end_matches('.').trim();
151    let mut parts = Vec::new();
152    if line.starts_with('"') {
153        return None;
154    }
155    let mut rest = line;
156    for _ in 0..3 {
157        rest = rest.trim();
158        if rest.is_empty() {
159            return None;
160        }
161        if rest.starts_with('"') {
162            let end = rest[1..].find('"')? + 1;
163            parts.push(rest[..=end].to_string());
164            rest = &rest[end + 1..];
165        } else if rest.starts_with('<') {
166            let end = rest.find('>')?;
167            parts.push(rest[..=end].to_string());
168            rest = &rest[end + 1..];
169        } else {
170            let end = rest.find(char::is_whitespace).unwrap_or(rest.len());
171            parts.push(rest[..end].to_string());
172            rest = &rest[end..];
173        }
174    }
175    if parts.len() != 3 {
176        return None;
177    }
178    Some((parts[0].clone(), parts[1].clone(), parts[2].clone()))
179}
180
181fn expand(prefixes: &BTreeMap<String, String>, tok: &str) -> String {
182    if let Some(inner) = tok.strip_prefix('<').and_then(|s| s.strip_suffix('>')) {
183        return inner.to_string();
184    }
185    if tok.starts_with('"') {
186        return unquote(tok);
187    }
188    if let Some((p, local)) = tok.split_once(':') {
189        if let Some(base) = prefixes.get(p) {
190            return format!("{base}{local}");
191        }
192    }
193    tok.to_string()
194}
195
196fn unquote(tok: &str) -> String {
197    tok.trim()
198        .trim_start_matches('"')
199        .trim_end_matches('"')
200        .to_string()
201}
202
203/// Deterministic 32-byte identity (four FNV-1a 64 streams). Not SHA-256.
204pub fn content_hash(bytes: &[u8]) -> [u8; 32] {
205    const OFFSETS: [u64; 4] = [
206        0xcbf29ce484222325,
207        0x6c62272e07bb0142,
208        0x9e3779b97f4a7c15,
209        0x243f6a8885a308d3,
210    ];
211    let mut out = [0u8; 32];
212    for (lane, seed) in OFFSETS.iter().enumerate() {
213        let mut h = *seed;
214        for (i, b) in bytes.iter().enumerate() {
215            h ^= *b as u64;
216            h = h.wrapping_mul(0x100000001b3);
217            h ^= (i as u64).rotate_left((lane as u32 * 7) % 63);
218        }
219        out[lane * 8..(lane + 1) * 8].copy_from_slice(&h.to_le_bytes());
220    }
221    out
222}