Skip to main content

synapse_core/ingest/
mod.rs

1use crate::store::{SynapseStore, IngestTriple};
2use anyhow::{Context, Result};
3use std::fs;
4use std::path::Path;
5use std::sync::Arc;
6
7pub mod extractor;
8pub mod processor;
9
10use extractor::{CsvExtractor, Extractor, MarkdownExtractor};
11
12pub struct IngestionEngine {
13    store: Arc<SynapseStore>,
14}
15
16impl IngestionEngine {
17    pub fn new(store: Arc<SynapseStore>) -> Self {
18        Self { store }
19    }
20
21    pub async fn ingest_file(&self, path: &Path, _namespace: &str) -> Result<u32> {
22        let content =
23            fs::read_to_string(path).with_context(|| format!("Failed to read file: {:?}", path))?;
24
25        let extension = path.extension().and_then(|s| s.to_str()).unwrap_or("");
26
27        let result = match extension {
28            "csv" => CsvExtractor::new().extract(&content)?,
29            "md" | "markdown" => MarkdownExtractor.extract(&content)?,
30            _ => anyhow::bail!("Unsupported file type: {}", extension),
31        };
32
33        // Convert (s, p, o) tuples to IngestTriple
34        let ingest_triples = result.triples.into_iter().map(|(s, p, o)| {
35             IngestTriple {
36                 subject: s,
37                 predicate: p,
38                 object: o,
39                 provenance: None, // Or we could add file provenance here
40             }
41        }).collect();
42
43        let (added, _) = self.store.ingest_triples(ingest_triples).await?;
44        Ok(added)
45    }
46}