rete-cli 0.4.2

CLI to build and inspect Rete graph files.
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
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
//! RDF projections of the Dataset Card — JSON-LD (VoID + schema.org + PROV-O)
//! and a Croissant subset.
//!
//! The card is **plain JSON at rest** and stays that way: the metadata section
//! is fetched by every reader on every open, and a measurement on the two
//! reference cards showed the faithful JSON-LD form costs materially more (the
//! VoID partitions need one object per row where the card stores a two-element
//! array — see `docs/dataset-cards.md` for the numbers). So the RDF view is a
//! **pure projection** — `rete card --format jsonld` reshapes bytes already
//! fetched, no extra network, no index read — and drift is impossible because
//! the projection has no second artefact: it is derived from the stored card on
//! every call.
//!
//! Vocabulary policy (issue #153): map onto **VoID** before inventing terms
//! (`void:triples`, `void:vocabulary`, `void:propertyPartition`,
//! `void:classPartition`, `void:sparqlEndpoint`, `void:dataDump`), **PROV-O**
//! for origin (`prov:wasDerivedFrom`, `prov:wasGeneratedBy`), **schema.org**
//! for the descriptive header Croissant shares, and a small `rete:` namespace
//! only for what no standard covers (counts of terms/named graphs, the content
//! hash, profile caps).
//!
//! Croissant is projected **honestly**: the descriptive header, licence,
//! creators and distribution map faithfully; `recordSet` does not — Croissant
//! models tables (`recordSet` → `field` → `dataType`) and an RDF graph has no
//! records — so the Croissant projection carries **no recordSet** rather than a
//! fabricated one, and no `sha256` (the format's integrity hash is blake3-16,
//! which Croissant has no slot for; it is published as `rete:contentHash`).

use serde_json::{json, Map, Value};

use super::buildinfo::BuildInfo;
use super::card::DatasetCard;

/// The `rete:` vocabulary IRI for card terms no standard vocabulary covers.
pub(crate) const RETE_NS: &str = "https://w3id.org/rete/card#";

/// Strip the N-Triples brackets from a stored IRI term (`<http://x>` → `http://x`).
fn iri(term: &str) -> &str {
    term.strip_prefix('<')
        .and_then(|t| t.strip_suffix('>'))
        .unwrap_or(term)
}

fn is_url(s: &str) -> bool {
    s.starts_with("http://") || s.starts_with("https://")
}

/// Percent-encode a custom-field key so `rete:extra/<key>` always expands to a
/// **valid IRI**. `RETE_NS` ends in `#`, so the key lands in the fragment;
/// RFC 3987 fragment characters (alphanumerics, `-._~!$&'()*+,;=:@/?`) pass
/// through, everything else — spaces, quotes, `#`, `%`, non-ASCII — is
/// percent-encoded byte-wise. Injective, so distinct keys keep distinct IRIs.
fn fragment_safe(key: &str) -> String {
    let mut out = String::with_capacity(key.len());
    for b in key.bytes() {
        let ok = b.is_ascii_alphanumeric()
            || matches!(
                b,
                b'-' | b'.'
                    | b'_'
                    | b'~'
                    | b'!'
                    | b'$'
                    | b'&'
                    | b'\''
                    | b'('
                    | b')'
                    | b'*'
                    | b'+'
                    | b','
                    | b';'
                    | b'='
                    | b':'
                    | b'@'
                    | b'/'
                    | b'?'
            );
        if ok {
            out.push(b as char);
        } else {
            out.push_str(&format!("%{b:02X}"));
        }
    }
    out
}

/// Insert `key: value` unless the value is `Null` / empty array / empty string.
fn put(obj: &mut Map<String, Value>, key: &str, value: Value) {
    let empty = match &value {
        Value::Null => true,
        Value::Array(a) => a.is_empty(),
        Value::String(s) => s.is_empty(),
        _ => false,
    };
    if !empty {
        obj.insert(key.to_string(), value);
    }
}

fn opt(v: &Option<String>) -> Value {
    v.as_ref().map(|s| json!(s)).unwrap_or(Value::Null)
}

/// A value that the context types as `@id` when it is a URL, else a literal.
fn id_or_text(s: &str) -> Value {
    if is_url(s) {
        json!({ "@id": s })
    } else {
        json!(s)
    }
}

/// The dataset's IRI: the curated canonical URL wins; else the source the
/// command was pointed at, when that is itself a URL (`card-url`); else none
/// (the dataset is a blank node — still valid JSON-LD).
fn dataset_id(card: &DatasetCard, source: &str) -> Option<String> {
    card.canonical_url
        .clone()
        .or_else(|| is_url(source).then(|| source.to_string()))
}

/// Project a card (+ optional build info) to one JSON-LD document typed both
/// `schema:Dataset` and `void:Dataset` — one dataset described in two
/// vocabularies, not two objects.
pub(crate) fn to_jsonld(
    card: &DatasetCard,
    build: Option<&BuildInfo>,
    checksum: &str,
    source: &str,
) -> Value {
    let mut d = Map::new();
    d.insert(
        "@context".into(),
        json!({
            "@vocab": "https://schema.org/",
            "void": "http://rdfs.org/ns/void#",
            "dcat": "http://www.w3.org/ns/dcat#",
            "dct": "http://purl.org/dc/terms/",
            "prov": "http://www.w3.org/ns/prov#",
            "xsd": "http://www.w3.org/2001/XMLSchema#",
            "rete": RETE_NS,
            "void:vocabulary": { "@type": "@id" },
            "dcat:theme": { "@type": "@id" },
            "void:sparqlEndpoint": { "@type": "@id" },
            "void:dataDump": { "@type": "@id" },
            "void:property": { "@type": "@id" },
            "void:class": { "@type": "@id" },
            "prov:wasDerivedFrom": { "@type": "@id" },
            "prov:endedAtTime": { "@type": "xsd:dateTime" }
        }),
    );
    if let Some(id) = dataset_id(card, source) {
        d.insert("@id".into(), json!(id));
    }
    d.insert("@type".into(), json!(["Dataset", "void:Dataset"]));

    // --- Descriptive header (schema.org — the part Croissant shares). ---
    put(&mut d, "name", opt(&card.title));
    put(&mut d, "description", opt(&card.description));
    if let Some(l) = &card.license {
        d.insert("license".into(), id_or_text(l));
    }
    put(&mut d, "version", opt(&card.version));
    put(&mut d, "dateCreated", opt(&card.created));
    put(&mut d, "citation", opt(&card.cite_as));
    // Keywords under BOTH standard terms — the same dual-vocabulary stance as
    // the schema:Dataset + void:Dataset typing: `schema:keywords` for
    // dataset-search harvesters, `dcat:keyword` for DCAT catalogs. One list,
    // two agreed names; a keyword in the `extra` bag would have had neither.
    let kw: Vec<Value> = card.keywords.iter().map(|k| json!(k)).collect();
    put(&mut d, "keywords", Value::Array(kw.clone()));
    put(&mut d, "dcat:keyword", Value::Array(kw));
    // Themes are IRIs into a controlled vocabulary, typed @id in the context —
    // exactly one standard term (`dcat:theme`), so no schema.org double.
    put(
        &mut d,
        "dcat:theme",
        Value::Array(card.theme.iter().map(|t| json!(t)).collect()),
    );
    // No curated language projects here: in RDF the language rides on each
    // literal, so the card DERIVES it — `signals.default_lang` fills
    // `schema:inLanguage` below, measured, never declared.
    if let Some(doi) = &card.doi {
        d.insert("identifier".into(), json!({ "@id": doi }));
    }
    let creators: Vec<Value> = card
        .creators
        .iter()
        .map(|c| {
            let mut p = Map::new();
            p.insert("@type".into(), json!("Person"));
            p.insert("name".into(), json!(c.name));
            if let Some(orcid) = &c.orcid {
                p.insert("@id".into(), json!(orcid));
            }
            Value::Object(p)
        })
        .collect();
    put(&mut d, "creator", Value::Array(creators));
    if let Some(p) = &card.publisher {
        let mut o = Map::new();
        o.insert("@type".into(), json!("Organization"));
        o.insert("name".into(), json!(p.name));
        if let Some(ror) = &p.ror {
            o.insert("@id".into(), json!(ror));
        }
        d.insert("publisher".into(), Value::Object(o));
    }
    if let Some(lang) = &card.signals.default_lang {
        d.insert("inLanguage".into(), json!(lang));
    }
    if let Some((from, to)) = &card.signals.temporal_extent {
        d.insert("temporalCoverage".into(), json!(format!("{from}/{to}")));
    }
    if let Some([min_lon, min_lat, max_lon, max_lat]) = card.signals.spatial_bbox {
        d.insert(
            "spatialCoverage".into(),
            json!({
                "@type": "Place",
                "geo": {
                    "@type": "GeoShape",
                    // schema:box is "minLat,minLon maxLat,maxLon".
                    "box": format!("{min_lat},{min_lon} {max_lat},{max_lon}")
                }
            }),
        );
    }
    if let Some(url) = &card.canonical_url {
        d.insert(
            "distribution".into(),
            json!([{
                "@type": "DataDownload",
                "contentUrl": url,
                "encodingFormat": "application/x-rete",
                "rete:contentHash": format!("blake3-16:{checksum}")
            }]),
        );
    } else {
        d.insert(
            "rete:contentHash".into(),
            json!(format!("blake3-16:{checksum}")),
        );
    }

    // --- The graph, in VoID: the native fit. ---
    // void:triples is the WHOLE dataset (default graph + named graphs).
    d.insert("void:triples".into(), json!(card.quad_count));
    put(&mut d, "void:sparqlEndpoint", opt(&card.sparql_endpoint));
    put(&mut d, "void:dataDump", opt(&card.canonical_url));
    put(
        &mut d,
        "void:vocabulary",
        Value::Array(card.vocabularies.iter().map(|v| json!(v)).collect()),
    );
    put(
        &mut d,
        "void:propertyPartition",
        Value::Array(
            card.predicates
                .iter()
                .map(|(p, n)| json!({ "void:property": iri(p), "void:triples": n }))
                .collect(),
        ),
    );
    put(
        &mut d,
        "void:classPartition",
        Value::Array(
            card.classes
                .iter()
                .map(|(c, n)| json!({ "void:class": iri(c), "void:entities": n }))
                .collect(),
        ),
    );

    // --- What no standard vocabulary covers (kept in rete:, not bent terms). ---
    d.insert("rete:defaultGraphTriples".into(), json!(card.triple_count));
    if card.named_graph_count > 0 {
        d.insert("rete:namedGraphs".into(), json!(card.named_graph_count));
    }
    d.insert("rete:terms".into(), json!(card.term_count));
    // Whether this dataset carries quoted triples, and what a consumer gets if
    // they export it. The boolean is written **whenever the signal was
    // measured** — a consumer branching on "does this graph have triple terms"
    // needs `false` stated, not inferred from an absent key. It is absent only
    // for a card read out of a saved JSON document, where nobody measured
    // anything and `false` would be a guess. The two surface terms follow only
    // when there is a choice to make.
    if let Some(q) = &card.signals.quoted_triples {
        d.insert("rete:quotedTriples".into(), json!(q.present));
        if !q.export_surfaces.is_empty() {
            d.insert(
                "rete:quotedTripleExportSurfaces".into(),
                Value::Array(q.export_surfaces.iter().map(|s| json!(s)).collect()),
            );
        }
        if let Some(def) = &q.export_default {
            d.insert("rete:quotedTripleExportDefault".into(), json!(def));
        }
    }
    if card.truncated {
        d.insert("rete:truncated".into(), json!(true));
        if card.top_n > 0 {
            d.insert("rete:partitionTopN".into(), json!(card.top_n));
        }
    }
    // Publisher-defined custom fields, each under `rete:extra/<key>` — kept in
    // the projection (omitting them would make it unfaithful: a consumer
    // diffing card against projection would find fields missing with no
    // explanation), but as **opaque values, not vocabulary**: the IRI means
    // "the publisher-defined field named <key>", whose semantics are private
    // to this card's publisher. Scalars project as plain literals; container
    // values (objects/arrays) are typed `@json` in the context so they become
    // JSON-LD 1.1 JSON literals (`rdf:JSON`) instead of blank-node structures
    // pretending to be modelled data. The consumer gets the values — never
    // their meaning.
    for (key, value) in &card.extra {
        let term = format!("rete:extra/{}", fragment_safe(key));
        if value.is_object() || value.is_array() {
            d.get_mut("@context")
                .expect("context inserted above")
                .as_object_mut()
                .expect("context is an object")
                .insert(term.clone(), json!({ "@type": "@json" }));
        }
        d.insert(term, value.clone());
    }

    // --- Provenance (PROV-O). ---
    let mut derived: Vec<Value> = Vec::new();
    if let Some(src) = &card.source {
        if is_url(src) {
            derived.push(json!(src));
        } else {
            d.insert("dct:source".into(), json!(src));
        }
    }
    for s in &card.derived_from {
        if is_url(s) {
            derived.push(json!(s));
        } else {
            d.entry("dct:source".to_string())
                .and_modify(|v| {
                    let prev = v.take();
                    *v = match prev {
                        Value::Array(mut a) => {
                            a.push(json!(s));
                            Value::Array(a)
                        }
                        other => json!([other, s]),
                    };
                })
                .or_insert_with(|| json!(s));
        }
    }
    put(&mut d, "prov:wasDerivedFrom", Value::Array(derived));
    put(&mut d, "rete:sourceDate", opt(&card.source_date));
    if let Some(b) = build {
        let activity = build_activity(b, &mut d);
        d.insert("prov:wasGeneratedBy".into(), activity);
    }

    Value::Object(d)
}

/// The IANA media type of an input format the build record names.
fn media_type(format: &str) -> Option<&'static str> {
    Some(match format {
        "nt" => "application/n-triples",
        "nq" => "application/n-quads",
        "ttl" => "text/turtle",
        "trig" => "application/trig",
        "rdfxml" => "application/rdf+xml",
        "rete" => "application/x-rete",
        _ => return None,
    })
}

/// The build record as a PROV-O activity — only where PROV-O means what the
/// record says: `prov:startedAtTime` / `prov:endedAtTime` for the run,
/// `prov:used` for each input (a `prov:Entity`, by file name and digest — a
/// file has no IRI of its own to point at), and `prov:wasAssociatedWith` for
/// the engine (a `schema:SoftwareApplication`). The rest of the record — the
/// flags, the counts, the timings, the spill, a re-card — has no standard term,
/// so it travels under `rete:` as JSON literals (`@json`, like the `extra`
/// bag): the values are all there, and no meaning is borrowed for them.
fn build_activity(b: &BuildInfo, doc: &mut Map<String, Value>) -> Value {
    let ctx = doc
        .get_mut("@context")
        .and_then(Value::as_object_mut)
        .expect("context inserted first");
    ctx.insert(
        "prov:startedAtTime".into(),
        json!({ "@type": "xsd:dateTime" }),
    );
    for term in [
        "rete:params",
        "rete:counts",
        "rete:run",
        "rete:spill",
        "rete:recarded",
        "rete:host",
    ] {
        ctx.insert(term.into(), json!({ "@type": "@json" }));
    }

    let mut act = Map::new();
    act.insert("@type".into(), json!("prov:Activity"));
    if let Some(t) = &b.started_at {
        act.insert("prov:startedAtTime".into(), json!(t));
    }
    if let Some(t) = &b.built_at {
        act.insert("prov:endedAtTime".into(), json!(t));
    }
    let mut agent = Map::new();
    agent.insert("@type".into(), json!("SoftwareApplication"));
    match (&b.engine, &b.builder) {
        (Some(e), _) => {
            agent.insert("name".into(), json!(e.name));
            agent.insert("softwareVersion".into(), json!(e.version));
            agent.insert("rete:gitCommit".into(), json!(e.commit));
            agent.insert("rete:target".into(), json!(e.target));
            agent.insert("rete:profile".into(), json!(e.profile));
            put(
                &mut agent,
                "rete:features",
                Value::Array(e.features.iter().map(|f| json!(f)).collect()),
            );
        }
        (None, Some(builder)) => {
            let (name, version) = builder.split_once(' ').unwrap_or((builder.as_str(), ""));
            agent.insert("name".into(), json!(name));
            if !version.is_empty() {
                agent.insert("softwareVersion".into(), json!(version));
            }
        }
        (None, None) => {}
    }
    if agent.len() > 1 {
        act.insert("prov:wasAssociatedWith".into(), Value::Object(agent));
    }
    let mut used: Vec<Value> = b
        .inputs
        .iter()
        .map(|i| {
            let mut e = Map::new();
            e.insert("@type".into(), json!("prov:Entity"));
            e.insert("name".into(), json!(i.name));
            if let Some(m) = media_type(&i.format) {
                e.insert("encodingFormat".into(), json!(m));
            }
            if let Some(c) = &i.compression {
                e.insert("rete:compression".into(), json!(c));
            }
            if let Some(n) = i.bytes {
                e.insert("rete:bytes".into(), json!(n));
            }
            if let Some(h) = &i.blake3 {
                e.insert("rete:blake3".into(), json!(h));
            }
            Value::Object(e)
        })
        .collect();
    if let Some(c) = &b.params.card_file {
        used.push(json!({
            "@type": "prov:Entity",
            "name": c.name,
            "encodingFormat": "application/json",
            "rete:role": "card-file",
            "rete:bytes": c.bytes,
            "rete:blake3": c.blake3,
        }));
    }
    put(&mut act, "prov:used", Value::Array(used));
    if b.inputs_omitted > 0 {
        act.insert("rete:inputsOmitted".into(), json!(b.inputs_omitted));
    }
    if let Some(h) = &b.host {
        act.insert("rete:host".into(), as_json(h));
    }
    act.insert("rete:params".into(), as_json(&b.params));
    if let Some(c) = &b.counts {
        act.insert("rete:counts".into(), as_json(c));
    }
    if let Some(r) = &b.run {
        act.insert("rete:run".into(), as_json(r));
    }
    if let Some(s) = &b.spill {
        act.insert("rete:spill".into(), as_json(s));
    }
    if let Some(r) = &b.recarded {
        act.insert("rete:recarded".into(), as_json(r));
    }
    Value::Object(act)
}

/// A build-record part as a JSON value, for an `@json` literal.
fn as_json<T: serde::Serialize>(v: &T) -> Value {
    serde_json::to_value(v).expect("build record serializes")
}

/// Project the **honestly-mappable Croissant subset**: descriptive header,
/// licence, creators/publisher, provenance, and the `.rete` file as a
/// `cr:FileObject` distribution. Deliberately absent: `recordSet` (Croissant
/// models tables; an RDF graph has no records — an empty or fabricated record
/// set would validate and mislead). The graph-shaped facts (partitions,
/// counts) live in the VoID projection, where they have standard terms.
///
/// `sha256`: Croissant requires an md5/sha256 on every `FileObject`, and a
/// self-contained file **cannot carry its own whole-file sha256** (the hash
/// would change the bytes being hashed). The format's integrity hash
/// (blake3-16 over the payload sections) is published as `rete:contentHash`;
/// pass the file's sha256 — known only *outside* the file — via
/// `--sha256` to emit a fully validator-clean document. Without it,
/// `mlcroissant` reports exactly that one missing property.
pub(crate) fn to_croissant(
    card: &DatasetCard,
    checksum: &str,
    source: &str,
    sha256: Option<&str>,
) -> Value {
    let mut d = Map::new();
    // The standard Croissant 1.0 context, verbatim from the spec.
    d.insert(
        "@context".into(),
        json!({
            "@language": "en",
            "@vocab": "https://schema.org/",
            "citeAs": "cr:citeAs",
            "column": "cr:column",
            "conformsTo": "dct:conformsTo",
            "cr": "http://mlcommons.org/croissant/",
            "rai": "http://mlcommons.org/croissant/RAI/",
            "data": { "@id": "cr:data", "@type": "@json" },
            "dataType": { "@id": "cr:dataType", "@type": "@vocab" },
            "dct": "http://purl.org/dc/terms/",
            "equivalentProperty": "cr:equivalentProperty",
            "examples": { "@id": "cr:examples", "@type": "@json" },
            "extract": "cr:extract",
            "field": "cr:field",
            "fileProperty": "cr:fileProperty",
            "fileObject": "cr:fileObject",
            "fileSet": "cr:fileSet",
            "format": "cr:format",
            "includes": "cr:includes",
            "isLiveDataset": "cr:isLiveDataset",
            "jsonPath": "cr:jsonPath",
            "key": "cr:key",
            "md5": "cr:md5",
            "parentField": "cr:parentField",
            "path": "cr:path",
            "recordSet": "cr:recordSet",
            "references": "cr:references",
            "regex": "cr:regex",
            "repeated": "cr:repeated",
            "replace": "cr:replace",
            "samplingRate": "cr:samplingRate",
            "sc": "https://schema.org/",
            "separator": "cr:separator",
            "source": "cr:source",
            "subField": "cr:subField",
            "transform": "cr:transform",
            "rete": RETE_NS
        }),
    );
    d.insert("@type".into(), json!("sc:Dataset"));
    d.insert(
        "conformsTo".into(),
        json!("http://mlcommons.org/croissant/1.0"),
    );
    // Croissant requires a name; fall back to the file/URL basename.
    let fallback_name = source
        .rsplit(['/', '\\'])
        .next()
        .unwrap_or("dataset")
        .to_string();
    d.insert(
        "name".into(),
        json!(card.title.clone().unwrap_or(fallback_name)),
    );
    put(&mut d, "description", opt(&card.description));
    if let Some(l) = &card.license {
        // mlcroissant wants a plain Text/URL value here, not an @id node.
        d.insert("license".into(), json!(l));
    }
    put(&mut d, "version", opt(&card.version));
    // Keywords have a home here: Croissant's descriptive header is
    // schema.org, and `keywords` is part of it (unlike the `extra` bag,
    // which is omitted — and unlike `theme`, whose only term is `dcat:theme`:
    // it stays in the JSON-LD projection rather than being bent into a
    // schema.org shape here).
    put(
        &mut d,
        "keywords",
        Value::Array(card.keywords.iter().map(|k| json!(k)).collect()),
    );
    // datePublished is recommended by the validator; the curated creation date
    // is the closest truthful value the card holds.
    put(&mut d, "datePublished", opt(&card.created));
    put(&mut d, "citeAs", opt(&card.cite_as));
    if let Some(u) = dataset_id(card, source) {
        d.insert("url".into(), json!(u));
    }
    let creators: Vec<Value> = card
        .creators
        .iter()
        .map(|c| {
            let mut p = Map::new();
            p.insert("@type".into(), json!("sc:Person"));
            p.insert("name".into(), json!(c.name));
            if let Some(orcid) = &c.orcid {
                p.insert("@id".into(), json!(orcid));
            }
            Value::Object(p)
        })
        .collect();
    put(&mut d, "creator", Value::Array(creators));
    if let Some(p) = &card.publisher {
        let mut o = Map::new();
        o.insert("@type".into(), json!("sc:Organization"));
        o.insert("name".into(), json!(p.name));
        if let Some(ror) = &p.ror {
            o.insert("@id".into(), json!(ror));
        }
        d.insert("publisher".into(), Value::Object(o));
    }
    if let Some(url) = &card.canonical_url {
        let mut file = Map::new();
        file.insert("@type".into(), json!("cr:FileObject"));
        file.insert("@id".into(), json!("rete-file"));
        file.insert("name".into(), json!("rete-file"));
        file.insert(
            "description".into(),
            json!("The single-file range-queryable RDF graph (.rete)."),
        );
        file.insert("contentUrl".into(), json!(url));
        file.insert("encodingFormat".into(), json!("application/x-rete"));
        if let Some(h) = sha256 {
            file.insert("sha256".into(), json!(h));
        }
        file.insert(
            "rete:contentHash".into(),
            json!(format!("blake3-16:{checksum}")),
        );
        d.insert("distribution".into(), json!([Value::Object(file)]));
    }
    // NO recordSet: an RDF graph is not a table. Tabular companions (Parquet),
    // where they exist, are the honest recordSet material — they are published
    // beside the file, not inside it, and carry their own Croissant documents.
    // And NO custom fields (`card.extra`): Croissant is the honestly-mappable
    // subset for ML loaders, and publisher-defined keys have no Croissant
    // terms. They stay in `--json` (verbatim) and `--format jsonld`
    // (`rete:extra/<key>` opaque values).
    Value::Object(d)
}

#[cfg(test)]
mod tests {
    use super::*;
    use rete_core::card::{Creator, Publisher};

    fn sample_card() -> DatasetCard {
        DatasetCard {
            title: Some("Demo".into()),
            description: Some("A demo graph".into()),
            license: Some("https://creativecommons.org/publicdomain/zero/1.0/".into()),
            source: Some("https://example.org/dump.nt".into()),
            version: Some("2026-08-04".into()),
            creators: vec![Creator {
                name: "Ada".into(),
                orcid: Some("https://orcid.org/0000-0002-1825-0097".into()),
            }],
            publisher: Some(Publisher {
                name: "EPFL".into(),
                ror: Some("https://ror.org/02s376052".into()),
            }),
            canonical_url: Some("https://data.example.org/demo.rete".into()),
            sparql_endpoint: Some("https://example.org/sparql".into()),
            derived_from: vec!["https://example.org/raw.csv".into()],
            triple_count: 10,
            quad_count: 12,
            named_graph_count: 1,
            term_count: 9,
            predicates: vec![("<http://ex/p>".into(), 7)],
            classes: vec![("<http://ex/C>".into(), 3)],
            vocabularies: vec!["http://ex/".into()],
            format_version: 5,
            ..Default::default()
        }
    }

    #[test]
    fn jsonld_maps_onto_void_schema_and_prov() {
        let card = sample_card();
        let build = crate::commands::buildinfo::BuildInfo {
            schema: 1,
            built_at: Some("2026-08-04T00:00:00Z".into()),
            builder: Some("rete-cli 0.3.2".into()),
            ..Default::default()
        };
        let v = to_jsonld(&card, Some(&build), "aa".repeat(16).as_str(), "demo.rete");

        assert_eq!(v["@id"], "https://data.example.org/demo.rete");
        assert_eq!(v["@type"], json!(["Dataset", "void:Dataset"]));
        assert_eq!(v["void:triples"], 12, "void:triples is the WHOLE dataset");
        assert_eq!(v["rete:defaultGraphTriples"], 10);
        assert_eq!(v["void:sparqlEndpoint"], "https://example.org/sparql");
        assert_eq!(v["void:dataDump"], "https://data.example.org/demo.rete");
        // Partitions carry unbracketed IRIs.
        assert_eq!(
            v["void:propertyPartition"][0]["void:property"],
            "http://ex/p"
        );
        assert_eq!(v["void:classPartition"][0]["void:entities"], 3);
        // Provenance: source + derived_from as prov:wasDerivedFrom @ids.
        let derived = v["prov:wasDerivedFrom"].as_array().unwrap();
        assert!(derived.contains(&json!("https://example.org/dump.nt")));
        assert!(derived.contains(&json!("https://example.org/raw.csv")));
        // Creator ORCID / publisher ROR are the node @ids — joinable IRIs.
        assert_eq!(
            v["creator"][0]["@id"],
            "https://orcid.org/0000-0002-1825-0097"
        );
        assert_eq!(v["publisher"]["@id"], "https://ror.org/02s376052");
        // The build activity, from the unhashed build-info section.
        assert_eq!(
            v["prov:wasGeneratedBy"]["prov:endedAtTime"],
            "2026-08-04T00:00:00Z"
        );
        assert_eq!(
            v["prov:wasGeneratedBy"]["prov:wasAssociatedWith"]["softwareVersion"],
            "0.3.2"
        );
        // Deterministic output for identical input.
        assert_eq!(
            serde_json::to_string(&v).unwrap(),
            serde_json::to_string(&to_jsonld(
                &card,
                Some(&build),
                "aa".repeat(16).as_str(),
                "demo.rete"
            ))
            .unwrap()
        );
    }

    #[test]
    fn croissant_carries_no_recordset_and_no_fabricated_sha256() {
        let card = sample_card();
        let v = to_croissant(&card, "bb", "demo.rete", None);
        assert_eq!(v["conformsTo"], "http://mlcommons.org/croissant/1.0");
        assert_eq!(v["distribution"][0]["@type"], "cr:FileObject");
        assert_eq!(v["distribution"][0]["encodingFormat"], "application/x-rete");
        // The honesty line: no fabricated tables, no fake checksum slot — a
        // file cannot carry its own whole-file sha256.
        assert!(v.get("recordSet").is_none());
        assert!(v["distribution"][0].get("sha256").is_none());
        assert_eq!(v["distribution"][0]["rete:contentHash"], "blake3-16:bb");
        // The validator wants a plain license value, not an @id node.
        assert!(v["license"].is_string());

        // With the publisher-supplied hash (known only outside the file), the
        // FileObject is complete and the document validator-clean.
        let with = to_croissant(&card, "bb", "demo.rete", Some("ab".repeat(32).as_str()));
        assert_eq!(with["distribution"][0]["sha256"], "ab".repeat(32));
    }

    /// The curated discovery fields — fields WITH agreed meaning, unlike the
    /// bag — project under their standard terms: keywords under BOTH
    /// `schema:keywords` and `dcat:keyword`, themes as `@id`-typed
    /// `dcat:theme` IRIs. Keywords carry into Croissant's schema.org header;
    /// `theme` (whose only term is DCAT's) does not. Absent fields emit
    /// nothing anywhere, and `schema:inLanguage` stays what it always was:
    /// the MEASURED dominant literal tag (there is deliberately no curated
    /// language — the data itself carries it, per-literal).
    #[test]
    fn discovery_fields_project_under_standard_terms_and_into_croissant() {
        let mut card = sample_card();
        card.keywords = vec!["catalog".into(), "open data".into()];
        card.theme =
            vec!["http://publications.europa.eu/resource/authority/data-theme/GOVE".into()];
        card.signals.default_lang = Some("cs".into());

        let v = to_jsonld(&card, None, "aa", "demo.rete");
        assert_eq!(v["keywords"], json!(["catalog", "open data"]));
        assert_eq!(v["dcat:keyword"], json!(["catalog", "open data"]));
        assert_eq!(v["@context"]["dcat"], "http://www.w3.org/ns/dcat#");
        assert_eq!(
            v["dcat:theme"],
            json!(["http://publications.europa.eu/resource/authority/data-theme/GOVE"])
        );
        assert_eq!(v["@context"]["dcat:theme"]["@type"], "@id");
        assert_eq!(v["inLanguage"], "cs", "measured, never curated");

        let c = to_croissant(&card, "aa", "demo.rete", None);
        assert_eq!(c["keywords"], json!(["catalog", "open data"]));
        assert!(c.get("dcat:theme").is_none(), "no DCAT term in Croissant");

        // No fields, no keys — in either projection.
        let plain = to_jsonld(&sample_card(), None, "aa", "demo.rete");
        assert!(plain.get("keywords").is_none());
        assert!(plain.get("dcat:keyword").is_none());
        assert!(plain.get("dcat:theme").is_none());
        let plain_cr = to_croissant(&sample_card(), "aa", "demo.rete", None);
        assert!(plain_cr.get("keywords").is_none());
    }

    /// Custom fields project per key under `rete:extra/<key>` — values, not
    /// vocabulary: scalars as plain literals, containers as `@json`-typed
    /// JSON literals, keys percent-encoded so the IRI is always valid.
    /// Croissant omits them entirely; a card without a bag emits nothing.
    #[test]
    fn extra_projects_per_key_as_opaque_values_and_croissant_omits_it() {
        let mut card = sample_card();
        card.extra = [
            ("atlas:layer".to_string(), json!(84)),
            ("review".to_string(), json!({"by": "dg", "ok": true})),
            ("my field".to_string(), json!("spaced")),
        ]
        .into_iter()
        .collect();

        let v = to_jsonld(&card, None, "aa", "demo.rete");
        // Scalar: a plain literal under its own rete:extra/ term (`:` is
        // IRI-fragment-legal, so the key passes through unencoded).
        assert_eq!(v["rete:extra/atlas:layer"], 84);
        // Container: emitted verbatim AND typed @json in the context, so it
        // expands to one rdf:JSON literal, not a blank-node structure.
        assert_eq!(v["rete:extra/review"]["by"], "dg");
        assert_eq!(v["@context"]["rete:extra/review"]["@type"], "@json");
        assert!(
            v["@context"].get("rete:extra/atlas:layer").is_none(),
            "scalars need no @json typing"
        );
        // An IRI-hostile key is percent-encoded (space → %20), keeping the
        // expanded IRI valid.
        assert_eq!(v["rete:extra/my%20field"], "spaced");
        // No bare top-level property was minted.
        assert!(v.get("atlas:layer").is_none());
        assert!(v.get("review").is_none());

        let c = to_croissant(&card, "aa", "demo.rete", None);
        assert!(c.get("extra").is_none(), "Croissant omits the bag");
        assert!(c.get("rete:extra/review").is_none());

        let plain = to_jsonld(&sample_card(), None, "aa", "demo.rete");
        assert!(
            !plain
                .as_object()
                .unwrap()
                .keys()
                .any(|k| k.starts_with("rete:extra/")),
            "no bag, no keys"
        );
    }

    #[test]
    fn cardless_iri_falls_back_to_url_source_only() {
        let mut card = sample_card();
        card.canonical_url = None;
        let remote = to_jsonld(&card, None, "cc", "https://host/x.rete");
        assert_eq!(remote["@id"], "https://host/x.rete");
        let local = to_jsonld(&card, None, "cc", "D:/data/x.rete");
        assert!(local.get("@id").is_none(), "a local path is not an IRI");
        // Without a distribution the hash still surfaces on the dataset node.
        assert_eq!(local["rete:contentHash"], "blake3-16:cc");
    }
}