use lazy_static::lazy_static; use std::ffi::OsStr; use std::path::Path; use std::collections::HashMap; mod txt; mod docx; mod xlsx; mod pptx; mod odt; mod odp; mod pdf; fn empty_extractor(_ : &str) -> String { "".to_string() } macro_rules! ext { ($f:ident) => {(stringify!($f), $f::get_text as ExtFn)} } type ExtFn = fn(&str) -> String; lazy_static! { static ref EXT: HashMap<&'static str, ExtFn> = { HashMap::from([ ext!(txt), ext!(docx), ext!(xlsx), ext!(pptx), ext!(odt), ext!(odp), ext!(pdf), ]) }; } /// Extract text from files if there is an extractor. /// If there is an error like `permission denied` or there /// is no extractor this function returns a empty string. pub fn extract_text(path : &str) -> String { let p = Path::new(&path); let extenstion = p.extension().unwrap_or_else(|| OsStr::new("")).to_str().unwrap(); EXT.get(extenstion).unwrap_or(&(empty_extractor as ExtFn))(path) }