pub mod code;
pub mod docx;
pub mod metadata;
pub mod pdf;
pub mod text;
pub mod walker;
use std::path::Path;
use xxhash_rust::xxh64::xxh64;
use crate::error::{Result, SearchError};
use crate::indexer::schema::StructuralMeta;
use crate::parser::walker::{FileKind, WalkEntry};
#[derive(Debug, Clone)]
pub struct ParsedDoc {
pub text: String,
pub title: Option<String>,
pub snippet: String,
pub metadata: StructuralMeta,
}
pub fn parse(entry: &WalkEntry) -> Result<ParsedDoc> {
let mut doc = match &entry.kind {
FileKind::PlainText => text::parse_text(&entry.path, &entry.kind)?,
FileKind::Markdown => text::parse_text(&entry.path, &entry.kind)?,
FileKind::Pdf => pdf::parse_pdf(&entry.path)?,
FileKind::Docx => docx::parse_docx(&entry.path)?,
FileKind::Code(lang) => code::parse_code(&entry.path, lang)?,
FileKind::Unknown => {
return Err(SearchError::UnsupportedFileType(entry.path.clone()));
}
};
doc.metadata = metadata::extract_metadata(&doc.text, &entry.path);
Ok(doc)
}
pub fn file_hash(path: &Path) -> Result<u64> {
let bytes = std::fs::read(path).map_err(|e| SearchError::Parse {
path: path.to_path_buf(),
reason: e.to_string(),
})?;
Ok(xxh64(&bytes, 0))
}