use crate::grep::error::{GrepError, GrepResult};
use gag::Gag;
pub fn extract_text(data: &[u8]) -> GrepResult<String> {
let _gag = Gag::stdout().ok();
pdf_extract::extract_text_from_mem(data).map_err(|e| GrepError::DocumentExtraction {
file_path: std::path::PathBuf::from("<memory>"),
message: format!("PDF extraction failed: {}", e),
})
}
pub fn extract_text_from_file(path: &std::path::Path) -> GrepResult<String> {
let _gag = Gag::stdout().ok();
pdf_extract::extract_text(path).map_err(|e| GrepError::DocumentExtraction {
file_path: path.to_path_buf(),
message: format!("PDF extraction failed: {}", e),
})
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_extract_invalid_pdf() {
let invalid_data = b"This is not a PDF file";
let result = extract_text(invalid_data);
assert!(result.is_err());
}
}