synaptic_loaders/
file_loader.rs1use std::collections::HashMap;
2use std::path::PathBuf;
3
4use crate::Document;
5use async_trait::async_trait;
6use serde_json::Value;
7use synaptic_core::SynapticError;
8
9use crate::Loader;
10
11pub 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}