processors_rs/
processor.rs

1use std::path::Path;
2
3pub trait DocumentProcessor {
4    fn process_document(&self, content: &str) -> anyhow::Result<Document>;
5}
6
7pub trait FileProcessor {
8    fn process_file(&self, path: impl AsRef<Path>) -> anyhow::Result<Document>;
9}
10
11pub trait UrlProcessor {
12    fn process_url(&self, url: &str) -> anyhow::Result<Document>;
13}
14
15impl<T: DocumentProcessor> FileProcessor for T {
16    fn process_file(&self, path: impl AsRef<Path>) -> anyhow::Result<Document> {
17        let bytes = std::fs::read(path)?;
18        let out = String::from_utf8_lossy(&bytes);
19        self.process_document(&out)
20    }
21}
22
23impl<T: DocumentProcessor> UrlProcessor for T {
24    fn process_url(&self, url: &str) -> anyhow::Result<Document> {
25        let content = reqwest::blocking::get(url)?.text()?;
26        self.process_document(&content)
27    }
28}
29
30pub struct Document {
31    pub chunks: Vec<String>,
32}