Skip to main content

kopitiam_document/validation/
mod.rs

1mod report;
2
3pub use report::ConversionReport;
4
5use kopitiam_pdf::Page;
6
7use crate::{Block, Document};
8
9/// Compares what was extracted against what was rendered, and tallies the
10/// block types found, so every conversion produces an auditable report
11/// rather than a silent best-effort guess.
12pub fn validate(pages: &[Page], document: &Document, rendered_markdown: &str) -> ConversionReport {
13    let extracted_words = pages
14        .iter()
15        .flat_map(|page| &page.spans)
16        .map(|span| word_count(&span.text))
17        .sum();
18
19    let mut headings_found = 0;
20    let mut lists_found = 0;
21    let mut tables_found = 0;
22
23    for block in &document.blocks {
24        match block {
25            Block::Heading(_) => headings_found += 1,
26            Block::List(_) => lists_found += 1,
27            Block::Table(_) => tables_found += 1,
28            _ => {}
29        }
30    }
31
32    ConversionReport {
33        pages: pages.len(),
34        extracted_words,
35        rendered_words: word_count(rendered_markdown),
36        headings_found,
37        lists_found,
38        tables_found,
39        citations_found: document.citations.len(),
40    }
41}
42
43fn word_count(text: &str) -> usize {
44    text.split_whitespace().count()
45}