Skip to main content

qualia_solid_bridge/
consumer.rs

1//! Solid **consumer** (relying party / personal agent side).
2//!
3//! Fetches LDP resources from a remote (or local) Solid pod and converts Turtle
4//! into NQuins for Qualia import. Also PUTs Turtle to a pod (egress deposit /
5//! sync-to-pod).
6
7use qualia_core_db::{q_hash, NQuin};
8use serde::{Deserialize, Serialize};
9
10#[derive(Debug, Clone, Serialize, Deserialize)]
11pub struct FetchResult {
12    pub url: String,
13    pub status: u16,
14    pub content_type: String,
15    pub body: String,
16    pub quin_count: usize,
17}
18
19#[derive(Debug)]
20pub enum ConsumerError {
21    Http(String),
22    Status(u16, String),
23    Empty,
24}
25
26impl std::fmt::Display for ConsumerError {
27    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
28        match self {
29            ConsumerError::Http(e) => write!(f, "http: {e}"),
30            ConsumerError::Status(c, b) => write!(f, "status {c}: {b}"),
31            ConsumerError::Empty => write!(f, "empty body"),
32        }
33    }
34}
35
36impl std::error::Error for ConsumerError {}
37
38/// GET a Solid resource (Turtle preferred). Optional Bearer access token.
39pub async fn fetch_resource(url: &str, bearer: Option<&str>) -> Result<FetchResult, ConsumerError> {
40    let client = reqwest::Client::builder()
41        .user_agent("QualiaSolidBridge/0.0.23 (Webizen consumer agent)")
42        .build()
43        .map_err(|e| ConsumerError::Http(e.to_string()))?;
44
45    let mut req = client.get(url).header(
46        "Accept",
47        "text/turtle, application/ld+json;q=0.9, application/rdf+xml;q=0.8, */*;q=0.1",
48    );
49    if let Some(tok) = bearer {
50        req = req.bearer_auth(tok);
51    }
52
53    let resp = req
54        .send()
55        .await
56        .map_err(|e| ConsumerError::Http(e.to_string()))?;
57    let status = resp.status().as_u16();
58    let content_type = resp
59        .headers()
60        .get(reqwest::header::CONTENT_TYPE)
61        .and_then(|v| v.to_str().ok())
62        .unwrap_or("application/octet-stream")
63        .to_string();
64    let body = resp
65        .text()
66        .await
67        .map_err(|e| ConsumerError::Http(e.to_string()))?;
68
69    if !(200..300).contains(&status) {
70        return Err(ConsumerError::Status(
71            status,
72            body.chars().take(400).collect(),
73        ));
74    }
75    if body.trim().is_empty() {
76        return Err(ConsumerError::Empty);
77    }
78
79    let quins = turtle_to_quins(&body);
80    Ok(FetchResult {
81        url: url.to_string(),
82        status,
83        content_type,
84        body,
85        quin_count: quins.len(),
86    })
87}
88
89/// PUT Turtle (or other RDF) to a Solid resource URL.
90pub async fn put_resource(
91    url: &str,
92    body: &[u8],
93    content_type: &str,
94    bearer: Option<&str>,
95) -> Result<u16, ConsumerError> {
96    let client = reqwest::Client::builder()
97        .user_agent("QualiaSolidBridge/0.0.23 (Webizen consumer agent)")
98        .build()
99        .map_err(|e| ConsumerError::Http(e.to_string()))?;
100
101    let mut req = client
102        .put(url)
103        .header("Content-Type", content_type)
104        .header("Link", r#"<http://www.w3.org/ns/ldp#Resource>; rel="type""#)
105        .body(body.to_vec());
106    if let Some(tok) = bearer {
107        req = req.bearer_auth(tok);
108    }
109
110    let resp = req
111        .send()
112        .await
113        .map_err(|e| ConsumerError::Http(e.to_string()))?;
114    let status = resp.status().as_u16();
115    if !(200..300).contains(&status) {
116        let t = resp.text().await.unwrap_or_default();
117        return Err(ConsumerError::Status(status, t.chars().take(400).collect()));
118    }
119    Ok(status)
120}
121
122/// POST into an LDP container (create resource). Returns Location if present.
123pub async fn post_to_container(
124    container_url: &str,
125    body: &[u8],
126    content_type: &str,
127    slug: Option<&str>,
128    bearer: Option<&str>,
129) -> Result<(u16, Option<String>), ConsumerError> {
130    let client = reqwest::Client::builder()
131        .user_agent("QualiaSolidBridge/0.0.23 (Webizen consumer agent)")
132        .build()
133        .map_err(|e| ConsumerError::Http(e.to_string()))?;
134
135    let mut req = client
136        .post(container_url)
137        .header("Content-Type", content_type)
138        .header("Link", r#"<http://www.w3.org/ns/ldp#Resource>; rel="type""#)
139        .body(body.to_vec());
140    if let Some(s) = slug {
141        req = req.header("Slug", s);
142    }
143    if let Some(tok) = bearer {
144        req = req.bearer_auth(tok);
145    }
146
147    let resp = req
148        .send()
149        .await
150        .map_err(|e| ConsumerError::Http(e.to_string()))?;
151    let status = resp.status().as_u16();
152    let location = resp
153        .headers()
154        .get(reqwest::header::LOCATION)
155        .and_then(|v| v.to_str().ok())
156        .map(|s| s.to_string());
157    if !(200..300).contains(&status) {
158        let t = resp.text().await.unwrap_or_default();
159        return Err(ConsumerError::Status(status, t.chars().take(400).collect()));
160    }
161    Ok((status, location))
162}
163
164/// Best-effort Turtle statements → NQuins (allocation firewall boundary).
165///
166/// Accumulates multi-line statements until `.`, then parses terms of the form
167/// `<s> <p> <o>` / `s p o` (ignores `@prefix`). Full RDF parsers remain cold-path;
168/// this covers Qualia export round-trip and simple VC-in-Turtle deposits.
169pub fn turtle_to_quins(turtle: &str) -> Vec<NQuin> {
170    let mut out = Vec::new();
171    let mut stmt = String::new();
172    for line in turtle.lines() {
173        let t = line.trim();
174        if t.is_empty() || t.starts_with('@') || t.starts_with('#') {
175            continue;
176        }
177        // Strip trailing comment outside strings (best-effort)
178        let t = t.split('#').next().unwrap_or(t).trim();
179        if !stmt.is_empty() {
180            stmt.push(' ');
181        }
182        stmt.push_str(t);
183        if !stmt.ends_with('.') {
184            continue;
185        }
186        let body = stmt.trim_end_matches('.').trim().to_string();
187        stmt.clear();
188        // Expand `;` predicate lists: subject p1 o1 ; p2 o2
189        let subject_and_rest = split_turtle_terms(&body);
190        if subject_and_rest.len() < 3 {
191            continue;
192        }
193        // Re-scan body for `;` separated predicate-object pairs after first subject
194        for triple in expand_turtle_statement(&body) {
195            if triple.len() < 3 {
196                continue;
197            }
198            let subject = hash_term(&triple[0]);
199            let predicate = hash_term(&triple[1]);
200            let object = hash_term(&triple[2]);
201            let context = q_hash("solid:import");
202            let metadata = 0x4000_0000_0000_0000u64;
203            let mut q = NQuin {
204                subject,
205                predicate,
206                object,
207                context,
208                metadata,
209                parity: 0,
210            };
211            q.recalculate_parity();
212            out.push(q);
213        }
214    }
215    out
216}
217
218/// Expand one Turtle statement body (no trailing `.`) into SPO triples.
219fn expand_turtle_statement(body: &str) -> Vec<Vec<String>> {
220    let terms = split_turtle_terms(body);
221    if terms.len() < 3 {
222        return Vec::new();
223    }
224    let mut triples = Vec::new();
225    let subject = terms[0].clone();
226    // Walk: p o [; p o]*
227    let mut i = 1;
228    while i + 1 < terms.len() {
229        if terms[i] == ";" {
230            i += 1;
231            continue;
232        }
233        let pred = terms[i].clone();
234        let obj = terms[i + 1].clone();
235        triples.push(vec![subject.clone(), pred, obj]);
236        i += 2;
237        if i < terms.len() && terms[i] == ";" {
238            i += 1;
239        }
240    }
241    if triples.is_empty() && terms.len() >= 3 {
242        triples.push(vec![terms[0].clone(), terms[1].clone(), terms[2].clone()]);
243    }
244    triples
245}
246
247fn hash_term(term: &str) -> u64 {
248    let t = term.trim();
249    if t.starts_with('<') && t.ends_with('>') {
250        let iri = &t[1..t.len() - 1];
251        return q_hash(iri);
252    }
253    if t.starts_with('"') {
254        // literal — hash full token
255        return q_hash(t);
256    }
257    // prefixed name or bare
258    q_hash(t)
259}
260
261/// Split a triple line into terms, respecting angle brackets and quotes.
262fn split_turtle_terms(line: &str) -> Vec<String> {
263    let mut terms = Vec::new();
264    let mut cur = String::new();
265    let mut chars = line.chars().peekable();
266    while let Some(c) = chars.next() {
267        match c {
268            ' ' | '\t' | '\n' | '\r'
269                if !cur.is_empty() && !cur.starts_with('<') && !cur.starts_with('"') =>
270            {
271                terms.push(std::mem::take(&mut cur));
272            }
273            ' ' | '\t' | '\n' | '\r' if cur.is_empty() => {}
274            ';' if cur.is_empty() => terms.push(";".into()),
275            '<' => {
276                if !cur.is_empty() {
277                    terms.push(std::mem::take(&mut cur));
278                }
279                cur.push('<');
280                for c2 in chars.by_ref() {
281                    cur.push(c2);
282                    if c2 == '>' {
283                        break;
284                    }
285                }
286                terms.push(std::mem::take(&mut cur));
287            }
288            '"' => {
289                if !cur.is_empty() {
290                    terms.push(std::mem::take(&mut cur));
291                }
292                cur.push('"');
293                for c2 in chars.by_ref() {
294                    cur.push(c2);
295                    if c2 == '"' {
296                        break;
297                    }
298                }
299                terms.push(std::mem::take(&mut cur));
300            }
301            _ => cur.push(c),
302        }
303    }
304    if !cur.is_empty() {
305        terms.push(cur);
306    }
307    terms
308}
309
310/// Import fetch result into a caller-owned quin buffer (cold path; returns count).
311pub fn fetch_to_quin_buffer(result: &FetchResult, out: &mut [NQuin]) -> usize {
312    let quins = turtle_to_quins(&result.body);
313    let n = quins.len().min(out.len());
314    out[..n].copy_from_slice(&quins[..n]);
315    n
316}
317
318#[cfg(test)]
319mod tests {
320    use super::*;
321
322    #[test]
323    fn parses_qualia_export_style_triples() {
324        let ttl = r#"
325@prefix qualia: <urn:qualia:schema:> .
326<urn:qualia:node:1> <urn:qualia:pred:2> <urn:qualia:node:3> . # Context: 9
327<urn:qualia:node:4> <urn:qualia:pred:5> <urn:qualia:node:6> .
328"#;
329        let quins = turtle_to_quins(ttl);
330        assert_eq!(quins.len(), 2);
331        assert!(quins[0].verify_ecc_parity());
332        assert_eq!(quins[0].subject, q_hash("urn:qualia:node:1"));
333    }
334
335    #[test]
336    fn parses_multiline_semicolon_statement() {
337        let ttl = r#"
338<#deposit> a <https://www.w3.org/2018/credentials#VerifiableCredential> ;
339  <http://purl.org/dc/terms/title> "Hackathon smoke deposit" ;
340  <http://purl.org/dc/terms/creator> <urn:institution:demo> .
341"#;
342        let quins = turtle_to_quins(ttl);
343        assert!(quins.len() >= 2, "got {}", quins.len());
344        assert!(quins.iter().all(|q| q.verify_ecc_parity()));
345    }
346
347    #[test]
348    fn ignores_prefix_only_lines() {
349        let quins = turtle_to_quins("@prefix foaf: <http://xmlns.com/foaf/0.1/> .\n");
350        assert!(quins.is_empty());
351    }
352}