use std::fs;
fn main() {
let dir = std::env::args().nth(1).expect("dir");
let mut ok = 0usize;
let mut failed: Vec<(String, String)> = Vec::new();
let mut empty: Vec<String> = Vec::new();
let mut entries: Vec<_> = fs::read_dir(&dir)
.expect("read dir")
.filter_map(Result::ok)
.map(|entry| entry.path())
.filter(|path| path.extension().map(|ext| ext == "doc").unwrap_or(false))
.collect();
entries.sort();
for path in entries {
let name = path.file_name().unwrap().to_string_lossy().to_string();
let bytes = match fs::read(&path) {
Ok(bytes) => bytes,
Err(error) => {
failed.push((name, format!("io: {error}")));
continue;
}
};
let result = std::panic::catch_unwind(|| {
let pkg = rust_docx::parse_document_bytes(&bytes)?;
let model = rust_docx::build_doc_model(&pkg);
let mut text_len = 0usize;
let mut tables = 0usize;
for node in &model.nodes {
match node {
rust_docx::DocNode::Table(_) => tables += 1,
rust_docx::DocNode::Paragraph(p) => {
for c in &p.children {
if let rust_docx::ParagraphChildNode::Text(r) = c {
text_len += r.text.len();
}
}
}
}
}
Ok::<(usize, usize, usize), String>((model.nodes.len(), text_len, tables))
});
match result {
Ok(Ok((nodes, text_len, tables))) => {
ok += 1;
if text_len == 0 && tables == 0 {
empty.push(name.clone());
}
println!("OK {name}: nodes={nodes} text={text_len} tables={tables}");
}
Ok(Err(error)) => failed.push((name, error)),
Err(_) => failed.push((name, "PANIC".to_string())),
}
}
println!("\n=== {ok} ok, {} failed, {} empty ===", failed.len(), empty.len());
for (name, error) in &failed {
println!("FAIL {name}: {error}");
}
for name in &empty {
println!("EMPTY {name}");
}
}