Skip to main content

qualia_cli/ingest/
json_mapper.rs

1use qualia_core_db::sparql_library::parsers::json_parser::{
2    parse_json_to_quins_with_provenance, JsonDatatype, JsonFieldMapping, JsonMappingProfile,
3};
4use std::fs::File;
5use std::io::BufReader;
6
7/// Streams JSON to quins with provenance context.
8pub fn stream_json_to_quins_with_provenance(
9    json_path: &str,
10    output_path: &str,
11    profile: &super::mapper::MappingProfile,
12    source_context_hash: u64,
13    sensitivity_class: u8,
14) {
15    let mut writer = super::writer::SuperBlockWriter::new(std::path::Path::new(output_path))
16        .expect("Failed to create SuperBlockWriter");
17    let file = File::open(json_path).expect("Failed to open JSON file");
18    let reader = BufReader::new(file);
19
20    // Convert CLI profile to core-db profile
21    let core_profile = JsonMappingProfile {
22        base_class_hash: profile.base_class_hash,
23        fields: profile
24            .fields
25            .iter()
26            .map(|f| JsonFieldMapping {
27                source_key: f.source_key.clone(),
28                predicate_hash: f.predicate_hash,
29                datatype: match f.datatype {
30                    super::mapper::TargetDatatype::Integer => JsonDatatype::Integer,
31                    super::mapper::TargetDatatype::Float => JsonDatatype::Float,
32                    super::mapper::TargetDatatype::DateTime => JsonDatatype::DateTime,
33                    super::mapper::TargetDatatype::StringRef => JsonDatatype::StringRef,
34                },
35            })
36            .collect(),
37    };
38
39    // Use core-db parser with provenance
40    parse_json_to_quins_with_provenance(
41        reader,
42        &core_profile,
43        source_context_hash,
44        sensitivity_class,
45        |quin| {
46            writer.push(quin).expect("Failed to write to SuperBlock");
47        },
48    )
49    .expect("Failed to parse JSON");
50}
51
52/// Streams JSON to quins (backward-compatible signature).
53pub fn stream_json_to_quins(
54    json_path: &str,
55    output_path: &str,
56    profile: &super::mapper::MappingProfile,
57) {
58    let source_context_hash = profile.base_class_hash & 0x00FF_FFFF_FFFF_FFFF;
59    let sensitivity_class = qualia_core_db::NQuin::SENSITIVITY_PUBLIC;
60    stream_json_to_quins_with_provenance(
61        json_path,
62        output_path,
63        profile,
64        source_context_hash,
65        sensitivity_class,
66    );
67}