Skip to main content

review/
review.rs

1//! `cargo run -p fm-vector --example review -- SCORE.pdf [--count]`: read a PDF for review and
2//! print its questions as JSON (`fm_vector::Review::to_json`, the `.fmn` and MusicXML left out),
3//! or with `--count` one line: the PDF's questions, by kind, its latent bars and the time taken.
4//! `--svg PAGE X0 Y0 X1 Y1` prints that region of a page (from 0) as SVG instead.
5
6fn main() {
7    let a: Vec<String> = std::env::args().collect();
8    if a.len() < 2 {
9        eprintln!("usage: review SCORE.pdf [--count | --svg PAGE X0 Y0 X1 Y1]");
10        std::process::exit(2);
11    }
12    let bytes = std::fs::read(&a[1]).expect("reading the PDF");
13    if a.get(2).map(String::as_str) == Some("--svg") {
14        let n: Vec<f64> = a[3..]
15            .iter()
16            .map(|x| x.parse().expect("a number"))
17            .collect();
18        match fm_vector::page_svg(&bytes, n[0] as usize, [n[1], n[2], n[3], n[4]]) {
19            Ok(svg) => print!("{svg}"),
20            Err(e) => {
21                println!("refused: {}", e.why);
22                std::process::exit(1);
23            }
24        }
25        return;
26    }
27    let t = std::time::Instant::now();
28    let review = match fm_vector::read_pdf_review(&bytes) {
29        Ok(r) => r,
30        Err(e) => {
31            println!("refused: {}", e.why);
32            std::process::exit(1);
33        }
34    };
35    if a.get(2).map(String::as_str) == Some("--count") {
36        let mut kinds = std::collections::BTreeMap::new();
37        for q in &review.questions {
38            *kinds.entry(q.kind.as_str()).or_insert(0) += 1;
39        }
40        let kinds: Vec<String> = kinds.iter().map(|(k, n)| format!("{k}={n}")).collect();
41        let choices: usize = review.questions.iter().map(|q| q.choices.len()).sum();
42        let latent: usize = review.latent.iter().map(|q| q.choices.len()).sum();
43        println!(
44            "questions {}\tunread {}\tlatent {}\tchoices {}\tms {}\t{}\tlatent choices {}",
45            review.questions.len(),
46            review.reading.unread.len(),
47            review.latent.len(),
48            choices,
49            t.elapsed().as_millis(),
50            kinds.join(" "),
51            latent
52        );
53        return;
54    }
55    let qs: Vec<String> = review.questions.iter().map(|q| q.to_json()).collect();
56    let latent: Vec<String> = review.latent.iter().map(|q| q.to_json()).collect();
57    println!(
58        "{{\"questions\":[\n{}\n],\n\"latent\":[\n{}\n]}}",
59        qs.join(",\n"),
60        latent.join(",\n")
61    );
62}