use std::path::Path;
use crate::{load::map_file, repair::with_repair_retry, Result};
pub fn page_count(input: &Path) -> Result<usize> {
page_count_with_password(input, None)
}
pub fn page_count_with_password(input: &Path, password: Option<&str>) -> Result<usize> {
count_impl(input, password, true)
}
pub fn page_count_fast(input: &Path) -> Result<usize> {
page_count_fast_with_password(input, None)
}
pub fn page_count_fast_with_password(input: &Path, password: Option<&str>) -> Result<usize> {
count_impl(input, password, false)
}
fn count_impl(input: &Path, password: Option<&str>, strict: bool) -> Result<usize> {
let timing = std::env::var_os("PDQ_TIMING").is_some();
let start = std::time::Instant::now();
let mmap = map_file(input)?;
with_repair_retry(&mmap, input, password, |source| {
if timing {
eprintln!("phase parse: {:?}", start.elapsed());
}
let count_start = std::time::Instant::now();
let count = if strict {
source.count_pages()
} else {
source.count_pages_fast()
};
if timing {
let phase = if strict { "walk" } else { "count" };
eprintln!("phase {phase}: {:?}", count_start.elapsed());
}
count
})
}