use anyhow::Error;
pub struct TxtProcessor;
impl TxtProcessor {
pub fn extract_text<T: AsRef<std::path::Path>>(file_path: &T) -> Result<String, Error> {
let bytes = std::fs::read(file_path)?;
let out = String::from_utf8_lossy(&bytes);
Ok(out.to_string())
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::fs::File;
use tempdir::TempDir;
#[test]
fn test_extract_text() {
let temp_dir = TempDir::new("example").unwrap();
let txt_file = temp_dir.path().join("test.txt");
File::create(&txt_file).unwrap();
let text = TxtProcessor::extract_text(&txt_file).unwrap();
assert_eq!(text, "");
let txt_file = "test_files/test.txt";
let text = TxtProcessor::extract_text(&txt_file).unwrap();
assert_eq!(
text,
"This is a test file to see how txt embedding works !\n"
);
}
#[test]
fn test_extract_text_invalid_file_path() {
let invalid_file_path = "invalid.txt";
let result = TxtProcessor::extract_text(&invalid_file_path);
assert!(result.is_err());
}
}