use anyhow::Error;
pub struct MarkdownProcessor;
impl MarkdownProcessor {
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).to_string();
let content = markdown_to_text::convert(&out);
Ok(content)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_extract_text() {
let file_path = "test_files/test.md";
let result = MarkdownProcessor::extract_text(&file_path).unwrap();
assert_eq!(result, "Hello, world!\n\nHow are you\n\nI am good");
}
#[test]
fn test_extract_text_file_not_exist() {
let file_path = "nonexistent_file.md";
let result = MarkdownProcessor::extract_text(&file_path);
assert!(result.is_err());
}
}