lc/readers/
mod.rs

1#[cfg(feature = "pdf")]
2pub mod pdf;
3
4use anyhow::{Context, Result};
5use std::io::Read;
6
7/// Trait for reading different file types
8pub trait FileReader {
9    /// Read file content as text from a file path
10    fn read_as_text(&self, file_path: &str) -> Result<String>;
11
12    /// Read file content as text from bytes
13    fn read_as_text_from_bytes(&self, _bytes: &[u8]) -> Result<String> {
14        // Default implementation: not supported
15        Err(anyhow::anyhow!(
16            "Reading from bytes not supported by this file reader"
17        ))
18    }
19
20    /// Read file content as text from a readable stream
21    #[allow(dead_code)]
22    fn read_as_text_from_reader(&self, mut reader: Box<dyn Read>) -> Result<String> {
23        // Default implementation: read all bytes and use bytes method
24        let mut bytes = Vec::new();
25        reader
26            .read_to_end(&mut bytes)
27            .with_context(|| "Failed to read bytes from reader")?;
28        self.read_as_text_from_bytes(&bytes)
29    }
30
31    /// Check if this reader can handle the given file extension
32    #[allow(dead_code)]
33    fn can_handle(&self, extension: &str) -> bool;
34}
35
36/// Get appropriate reader for file extension
37pub fn get_reader_for_extension(extension: &str) -> Option<Box<dyn FileReader>> {
38    match extension.to_lowercase().as_str() {
39        #[cfg(feature = "pdf")]
40        "pdf" => Some(Box::new(pdf::PdfReader::new())),
41        _ => None,
42    }
43}