gmeow-gts 0.9.5

GTS (Graph Transport Substrate) format engine: CBOR-sequence append-only RDF 1.2 log reader, folder, and verifier
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
// SPDX-FileCopyrightText: 2026 Blackcat InformaticsĀ® Inc. <paudley@blackcatinformatics.ca>
// SPDX-License-Identifier: MIT OR Apache-2.0

//! GTS OKF profile exporter.
//!
//! This module projects only the OKF vocabulary to Markdown bundle files.
//! Unsupported RDF remains visible in `_unmapped.nq` instead of being silently
//! dropped.

use std::collections::{BTreeMap, BTreeSet};
use std::fmt;
use std::fs;
use std::io::{self, Write};
use std::path::{Path, PathBuf};
use std::time::{SystemTime, UNIX_EPOCH};

use serde::Serialize;
use serde_json::Value as JsonValue;
use serde_yaml::{Mapping as YamlMapping, Value as YamlValue};

use crate::model::{Graph, TermKind, Triple3};
use crate::nquads::render_term;

pub const OKF_NS: &str = "https://blackcatinformatics.ca/projects/gts/okf#";
pub const DEFAULT_BASE_IRI: &str = "https://blackcatinformatics.ca/projects/gts/okf/doc/";
pub const OKF_PATH: &str = "https://blackcatinformatics.ca/projects/gts/okf#path";
pub const OKF_TYPE: &str = "https://blackcatinformatics.ca/projects/gts/okf#type";
pub const OKF_TITLE: &str = "https://blackcatinformatics.ca/projects/gts/okf#title";
pub const OKF_DESCRIPTION: &str = "https://blackcatinformatics.ca/projects/gts/okf#description";
pub const OKF_RESOURCE: &str = "https://blackcatinformatics.ca/projects/gts/okf#resource";
pub const OKF_TAG: &str = "https://blackcatinformatics.ca/projects/gts/okf#tag";
pub const OKF_TIMESTAMP: &str = "https://blackcatinformatics.ca/projects/gts/okf#timestamp";
pub const OKF_BODY: &str = "https://blackcatinformatics.ca/projects/gts/okf#body";
pub const OKF_LINKS: &str = "https://blackcatinformatics.ca/projects/gts/okf#links";
pub const OKF_LINK_TEXT: &str = "https://blackcatinformatics.ca/projects/gts/okf#linkText";
pub const OKF_LINK_OCCURRENCE: &str =
    "https://blackcatinformatics.ca/projects/gts/okf#linkOccurrence";
pub const OKF_JSON: &str = "https://blackcatinformatics.ca/projects/gts/okf#json";
pub const RDF_REIFIES: &str = "http://www.w3.org/1999/02/22-rdf-syntax-ns#reifies";
pub const XSD_BOOLEAN: &str = "http://www.w3.org/2001/XMLSchema#boolean";
pub const XSD_INTEGER: &str = "http://www.w3.org/2001/XMLSchema#integer";
pub const XSD_DECIMAL: &str = "http://www.w3.org/2001/XMLSchema#decimal";
pub const XSD_DATETIME: &str = "http://www.w3.org/2001/XMLSchema#dateTime";

/// Options for exporting an OKF bundle directory.
#[derive(Clone, Debug)]
pub struct OkfExportOptions {
    /// Base IRI recorded in the bundle manifest.
    pub base_iri: String,
    /// Permit `okf:body` inline string literals as a body source. Digest-backed
    /// body blobs are always supported.
    pub inline_body: bool,
}

impl Default for OkfExportOptions {
    fn default() -> Self {
        Self {
            base_iri: DEFAULT_BASE_IRI.to_string(),
            inline_body: false,
        }
    }
}

/// Summary returned by [`to_okf`].
#[derive(Clone, Debug)]
pub struct OkfExportReport {
    pub directory: PathBuf,
    pub documents: usize,
    pub unmapped_triples: usize,
}

/// Raised when a graph cannot be projected to an OKF bundle without guessing.
#[derive(Debug)]
pub struct OkfExportError {
    detail: String,
}

impl OkfExportError {
    fn new(detail: impl Into<String>) -> Self {
        Self {
            detail: detail.into(),
        }
    }
}

impl fmt::Display for OkfExportError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(&self.detail)
    }
}

impl std::error::Error for OkfExportError {}

impl From<io::Error> for OkfExportError {
    fn from(value: io::Error) -> Self {
        Self::new(value.to_string())
    }
}

#[derive(Default)]
struct ExportDocument {
    subject: usize,
    path: Option<String>,
    fields: BTreeMap<String, YamlValue>,
    tags: BTreeSet<String>,
    body: Option<Vec<u8>>,
}

/// Project an OKF-profile folded graph into a Markdown bundle directory.
pub fn to_okf(
    graph: &Graph,
    directory: &Path,
    options: &OkfExportOptions,
) -> Result<OkfExportReport, OkfExportError> {
    if directory.exists() {
        return Err(OkfExportError::new(format!(
            "destination {} already exists",
            directory.display()
        )));
    }
    let staged = staged_path(directory);
    let _ = fs::remove_dir_all(&staged);
    fs::create_dir_all(&staged)?;

    match write_okf(graph, &staged, options) {
        Ok(mut report) => {
            fs::rename(&staged, directory)?;
            report.directory = directory.to_path_buf();
            Ok(report)
        }
        Err(err) => {
            let _ = fs::remove_dir_all(&staged);
            Err(err)
        }
    }
}

fn write_okf(
    graph: &Graph,
    directory: &Path,
    options: &OkfExportOptions,
) -> Result<OkfExportReport, OkfExportError> {
    let mut docs: BTreeMap<usize, ExportDocument> = BTreeMap::new();
    let mut consumed_quads = BTreeSet::new();
    let mut link_statements = BTreeSet::new();

    for (idx, &(subject, predicate, object, graph_name)) in graph.quads.iter().enumerate() {
        if graph_name.is_some() {
            continue;
        }
        let Some(predicate_iri) = iri_value(graph, predicate) else {
            continue;
        };
        let doc = docs.entry(subject).or_insert_with(|| ExportDocument {
            subject,
            ..ExportDocument::default()
        });
        match predicate_iri {
            OKF_PATH => {
                if let Some(path) = literal_value(graph, object) {
                    safe_relative_path(path)?;
                    doc.path = Some(path.to_string());
                    consumed_quads.insert(idx);
                }
            }
            OKF_TYPE => {
                if let Some(value) = literal_value(graph, object) {
                    doc.fields
                        .insert("type".to_string(), YamlValue::String(value.to_string()));
                    consumed_quads.insert(idx);
                }
            }
            OKF_TITLE => consume_string_field(
                graph,
                object,
                &mut doc.fields,
                "title",
                idx,
                &mut consumed_quads,
            ),
            OKF_DESCRIPTION => consume_string_field(
                graph,
                object,
                &mut doc.fields,
                "description",
                idx,
                &mut consumed_quads,
            ),
            OKF_TIMESTAMP => consume_string_field(
                graph,
                object,
                &mut doc.fields,
                "timestamp",
                idx,
                &mut consumed_quads,
            ),
            OKF_RESOURCE => {
                if let Some(value) = iri_value(graph, object) {
                    doc.fields
                        .insert("resource".to_string(), YamlValue::String(value.to_string()));
                    consumed_quads.insert(idx);
                }
            }
            OKF_TAG => {
                if let Some(value) = literal_value(graph, object) {
                    doc.tags.insert(value.to_string());
                    consumed_quads.insert(idx);
                }
            }
            OKF_BODY => {
                doc.body = Some(body_bytes(graph, object, options)?);
                consumed_quads.insert(idx);
            }
            OKF_LINKS => {
                consumed_quads.insert(idx);
                link_statements.insert((subject, predicate, object));
            }
            _ if predicate_iri.starts_with(OKF_NS) => {
                let local = predicate_iri
                    .strip_prefix(OKF_NS)
                    .expect("predicate matched OKF namespace");
                if known_local(local) {
                    continue;
                }
                if let Some(value) = yaml_value(graph, object)? {
                    doc.fields.insert(local.to_string(), value);
                    consumed_quads.insert(idx);
                }
            }
            _ => {}
        }
    }

    let mut unmapped = Vec::new();
    for (idx, &(subject, predicate, object, graph_name)) in graph.quads.iter().enumerate() {
        if !consumed_quads.contains(&idx) {
            unmapped.push(match graph_name {
                Some(name) => format!(
                    "{} {} {} {} .",
                    render_term(graph, subject),
                    render_term(graph, predicate),
                    render_term(graph, object),
                    render_term(graph, name)
                ),
                None => format!(
                    "{} {} {} .",
                    render_term(graph, subject),
                    render_term(graph, predicate),
                    render_term(graph, object)
                ),
            });
        }
    }

    let consumed_reifiers = consumed_link_reifiers(graph, &link_statements, &mut unmapped);
    for &(reifier, (subject, predicate, object)) in &graph.reifiers {
        if !consumed_reifiers.contains(&reifier) {
            unmapped.push(format!(
                "{} <{RDF_REIFIES}> <<( {} {} {} )>> .",
                render_term(graph, reifier),
                render_term(graph, subject),
                render_term(graph, predicate),
                render_term(graph, object)
            ));
        }
    }

    for &(reifier, predicate, value) in &graph.annotations {
        if !consumed_reifiers.contains(&reifier) {
            unmapped.push(format!(
                "{} {} {} .",
                render_term(graph, reifier),
                render_term(graph, predicate),
                render_term(graph, value)
            ));
        }
    }

    let mut documents = Vec::new();
    for (_, mut doc) in docs {
        let Some(path) = doc.path.clone() else {
            unmapped.push(format!(
                "# subject {} has OKF fields but no okf:path",
                render_term(graph, doc.subject)
            ));
            continue;
        };
        if !doc.fields.contains_key("type") {
            return Err(OkfExportError::new(format!(
                "OKF document {path} is missing okf:type"
            )));
        }
        let body = doc.body.take().unwrap_or_default();
        write_document(directory, &path, &doc.fields, &doc.tags, &body)?;
        documents.push(path);
    }
    documents.sort();

    if !unmapped.is_empty() {
        fs::write(
            directory.join("_unmapped.nq"),
            format!("{}\n", unmapped.join("\n")),
        )?;
    }
    write_manifest(directory, options, &documents, unmapped.len())?;
    Ok(OkfExportReport {
        directory: directory.to_path_buf(),
        documents: documents.len(),
        unmapped_triples: unmapped.len(),
    })
}

fn consume_string_field(
    graph: &Graph,
    object: usize,
    fields: &mut BTreeMap<String, YamlValue>,
    key: &str,
    index: usize,
    consumed: &mut BTreeSet<usize>,
) {
    if let Some(value) = literal_value(graph, object) {
        fields.insert(key.to_string(), YamlValue::String(value.to_string()));
        consumed.insert(index);
    }
}

fn body_bytes(
    graph: &Graph,
    object: usize,
    options: &OkfExportOptions,
) -> Result<Vec<u8>, OkfExportError> {
    let value = literal_value(graph, object)
        .ok_or_else(|| OkfExportError::new("okf:body must be a literal"))?;
    if value.starts_with("blake3:") {
        let entry = graph
            .blob_entry(value)
            .ok_or_else(|| OkfExportError::new(format!("missing OKF body blob {value}")))?;
        return entry
            .decoded_vec()
            .map_err(|err| OkfExportError::new(format!("cannot decode OKF body blob: {err:?}")));
    }
    if options.inline_body {
        Ok(value.as_bytes().to_vec())
    } else {
        Err(OkfExportError::new(
            "inline okf:body literal requires --inline-body",
        ))
    }
}

fn yaml_value(graph: &Graph, object: usize) -> Result<Option<YamlValue>, OkfExportError> {
    let Some(term) = graph.terms.get(object) else {
        return Ok(None);
    };
    if term.kind != TermKind::Literal {
        return Ok(None);
    }
    let value = term.value.as_deref().unwrap_or("");
    let datatype = term.datatype.and_then(|id| iri_value(graph, id));
    Ok(Some(match datatype {
        Some(XSD_BOOLEAN) => YamlValue::Bool(value == "true"),
        Some(XSD_INTEGER) => serde_yaml::from_str(value).unwrap_or(YamlValue::String(value.into())),
        Some(XSD_DECIMAL) => serde_yaml::from_str(value).unwrap_or(YamlValue::String(value.into())),
        Some(OKF_JSON) => {
            let json: JsonValue = serde_json::from_str(value).map_err(|e| {
                OkfExportError::new(format!("invalid okf:json literal {value:?}: {e}"))
            })?;
            serde_yaml::to_value(json)
                .map_err(|e| OkfExportError::new(format!("cannot convert JSON literal: {e}")))?
        }
        _ => YamlValue::String(value.to_string()),
    }))
}

fn consumed_link_reifiers(
    graph: &Graph,
    link_statements: &BTreeSet<Triple3>,
    unmapped: &mut Vec<String>,
) -> BTreeSet<usize> {
    let mut out = BTreeSet::new();
    for &(reifier, statement) in &graph.reifiers {
        if !link_statements.contains(&statement) {
            continue;
        }
        let annotations: Vec<_> = graph
            .annotations
            .iter()
            .filter(|&&(candidate, _, _)| candidate == reifier)
            .collect();
        let ok = annotations.iter().all(|&&(_, predicate, _)| {
            iri_value(graph, predicate)
                .is_some_and(|iri| iri == OKF_LINK_TEXT || iri == OKF_LINK_OCCURRENCE)
        });
        if ok {
            out.insert(reifier);
        } else {
            unmapped.push(format!(
                "# link reifier {} has non-OKF annotations",
                render_term(graph, reifier)
            ));
        }
    }
    out
}

fn write_document(
    root: &Path,
    path: &str,
    fields: &BTreeMap<String, YamlValue>,
    tags: &BTreeSet<String>,
    body: &[u8],
) -> Result<(), OkfExportError> {
    safe_relative_path(path)?;
    let target = root.join(path);
    if let Some(parent) = target.parent() {
        fs::create_dir_all(parent)?;
    }
    let mut all_fields = fields.clone();
    if !tags.is_empty() {
        all_fields.insert(
            "tags".to_string(),
            YamlValue::Sequence(tags.iter().cloned().map(YamlValue::String).collect()),
        );
    }
    let mut mapping = YamlMapping::new();
    for (key, value) in all_fields {
        mapping.insert(YamlValue::String(key.clone()), value.clone());
    }
    let yaml = serde_yaml::to_string(&mapping)
        .map_err(|e| OkfExportError::new(format!("cannot serialize frontmatter: {e}")))?;
    let mut out = fs::File::create(target)?;
    out.write_all(b"---\n")?;
    out.write_all(yaml.as_bytes())?;
    out.write_all(b"---\n")?;
    out.write_all(body)?;
    Ok(())
}

#[derive(Serialize)]
struct Manifest<'a> {
    schema: &'a str,
    base_iri: &'a str,
    doc_count: usize,
    source_paths: &'a [String],
    unmapped_triples: usize,
}

fn write_manifest(
    root: &Path,
    options: &OkfExportOptions,
    paths: &[String],
    unmapped_triples: usize,
) -> Result<(), OkfExportError> {
    let control = root.join(".gts-okf");
    fs::create_dir_all(&control)?;
    let manifest = Manifest {
        schema: "gts-okf-v1",
        base_iri: &options.base_iri,
        doc_count: paths.len(),
        source_paths: paths,
        unmapped_triples,
    };
    let text = serde_json::to_string_pretty(&manifest)
        .map_err(|e| OkfExportError::new(format!("cannot encode OKF manifest: {e}")))?;
    fs::write(control.join("manifest.json"), format!("{text}\n"))?;
    Ok(())
}

fn staged_path(directory: &Path) -> PathBuf {
    let parent = directory
        .parent()
        .filter(|p| !p.as_os_str().is_empty())
        .unwrap_or_else(|| Path::new("."));
    let name = directory
        .file_name()
        .map(|n| n.to_string_lossy())
        .unwrap_or_else(|| "okf".into());
    let nanos = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map(|d| d.as_nanos())
        .unwrap_or(0);
    parent.join(format!(
        ".{name}.gts-okf.{}.{}.tmp",
        std::process::id(),
        nanos
    ))
}

fn safe_relative_path(path: &str) -> Result<(), OkfExportError> {
    if path.is_empty()
        || path.starts_with('/')
        || path.contains('\\')
        || path
            .split('/')
            .any(|part| part.is_empty() || part == "." || part == "..")
    {
        return Err(OkfExportError::new(format!(
            "unsafe OKF relative path: {path}"
        )));
    }
    Ok(())
}

fn iri_value(graph: &Graph, term: usize) -> Option<&str> {
    graph
        .terms
        .get(term)
        .and_then(|term| match (term.kind, term.value.as_deref()) {
            (TermKind::Iri, Some(value)) => Some(value),
            _ => None,
        })
}

fn literal_value(graph: &Graph, term: usize) -> Option<&str> {
    graph
        .terms
        .get(term)
        .and_then(|term| match (term.kind, term.value.as_deref()) {
            (TermKind::Literal, Some(value)) => Some(value),
            _ => None,
        })
}

fn known_local(local: &str) -> bool {
    matches!(
        local,
        "path"
            | "type"
            | "title"
            | "description"
            | "resource"
            | "tag"
            | "timestamp"
            | "body"
            | "links"
            | "linkText"
            | "linkOccurrence"
            | "json"
    )
}