Skip to main content

parse_blocks/
parse_blocks.rs

1fn main() -> Result<(), Box<dyn std::error::Error>> {
2    let mut path = None;
3    let mut options = rust_docx::ExtractDocumentBlocksOptions::default();
4    let mut include_revisions = false;
5    let mut positional_only = false;
6    for argument in std::env::args_os().skip(1) {
7        if !positional_only && argument == "--" {
8            positional_only = true;
9        } else if !positional_only && argument == "--include-words" {
10            options.include_words = true;
11        } else if !positional_only && argument == "--include-revisions" {
12            include_revisions = true;
13        } else if path.is_none() {
14            path = Some(argument);
15        } else {
16            return Err(
17                "Expected one document path and optional --include-words and --include-revisions"
18                    .into(),
19            );
20        }
21    }
22    let path = path.ok_or("A document path is required")?;
23    let bytes = std::fs::read(path)?;
24    let out = std::io::stdout().lock();
25    if include_revisions {
26        serde_json::to_writer(
27            out,
28            &rust_docx::parse_document_blocks_with_revisions(&bytes, options)?,
29        )?;
30    } else {
31        serde_json::to_writer(
32            out,
33            &rust_docx::parse_document_blocks_with_options(&bytes, options)?,
34        )?;
35    }
36    Ok(())
37}