Skip to main content

synaptic_loaders/
file_loader.rs

1use std::collections::HashMap;
2use std::path::PathBuf;
3
4use async_trait::async_trait;
5use serde_json::Value;
6use synaptic_core::SynapticError;
7use synaptic_retrieval::Document;
8
9use crate::Loader;
10
11/// Loads content from a file on disk.
12///
13/// Reads the file contents via `tokio::fs::read_to_string` and returns a single
14/// Document with the file path as id and the file contents as content.
15/// The `source` metadata key is set to the file path.
16pub struct FileLoader {
17    path: PathBuf,
18}
19
20impl FileLoader {
21    pub fn new(path: impl Into<PathBuf>) -> Self {
22        Self { path: path.into() }
23    }
24}
25
26#[async_trait]
27impl Loader for FileLoader {
28    async fn load(&self) -> Result<Vec<Document>, SynapticError> {
29        let content = tokio::fs::read_to_string(&self.path).await.map_err(|e| {
30            SynapticError::Loader(format!("cannot read {}: {e}", self.path.display()))
31        })?;
32
33        let id = self.path.to_string_lossy().to_string();
34
35        let mut metadata = HashMap::new();
36        metadata.insert(
37            "source".to_string(),
38            Value::String(self.path.to_string_lossy().to_string()),
39        );
40
41        Ok(vec![Document::with_metadata(id, content, metadata)])
42    }
43}