Skip to main content

qualia_cli/ingest/
csv_mapper.rs

1use qualia_core_db::sparql_library::parsers::csv_parser::{
2    parse_csv_to_quins, CsvColumnMapping, CsvDatatype, CsvMappingProfile,
3};
4use std::fs::File;
5
6pub fn stream_csv_to_quins(
7    csv_path: &str,
8    output_path: &str,
9    profile: &mut super::mapper::MappingProfile,
10) {
11    let mut writer = super::writer::SuperBlockWriter::new(std::path::Path::new(output_path))
12        .expect("Failed to create SuperBlockWriter");
13    let file = File::open(csv_path).expect("Failed to open CSV");
14
15    // Convert CLI profile to core-db profile
16    let mut core_profile = CsvMappingProfile {
17        base_class_hash: profile.base_class_hash,
18        fields: profile
19            .fields
20            .iter()
21            .map(|f| CsvColumnMapping {
22                source_key: f.source_key.clone(),
23                column_index: f.column_index,
24                predicate_hash: f.predicate_hash,
25                datatype: match f.datatype {
26                    super::mapper::TargetDatatype::Integer => CsvDatatype::Integer,
27                    super::mapper::TargetDatatype::Float => CsvDatatype::Float,
28                    super::mapper::TargetDatatype::DateTime => CsvDatatype::DateTime,
29                    super::mapper::TargetDatatype::StringRef => CsvDatatype::StringRef,
30                },
31            })
32            .collect(),
33    };
34
35    // Use core-db parser
36    parse_csv_to_quins(file, &mut core_profile, |quin| {
37        writer.push(quin).expect("Failed to write to SuperBlock");
38    })
39    .expect("Failed to parse CSV");
40
41    // Update column indices back to CLI profile
42    for (i, field) in profile.fields.iter_mut().enumerate() {
43        if let Some(core_field) = core_profile.fields.get(i) {
44            field.column_index = core_field.column_index;
45        }
46    }
47}