Skip to main content

read_the_report/
read_the_report.rs

1//! The report: what a redaction did, in a form that can be pasted into a
2//! ticket without a second thought.
3//!
4//! Run with: `cargo run --example read_the_report`
5
6use er7_redact::{Policy, Redactor};
7
8fn main() -> Result<(), er7_redact::Error> {
9    let text = "MSH|^~\\&|ADT1|MCM||||||ADT^A08|MSG1|P|2.5\r\
10                PID|1||PATID1234^^^ADT1^MR||EVERYWOMAN^EVE^E||19610615|F|||\
11                12 ELM ST^^BOSTON^MA^02101||555-555-1111~555-555-2222\r\
12                NK1|1|EVERYMAN^ADAM|SPO";
13
14    let mut message = er7::parse(text)?;
15    let report = Redactor::new(Policy::patient_identifiers()).redact(&mut message);
16
17    for change in &report.changes {
18        println!("{:<18} {}", change.path.to_string(), change.action);
19    }
20
21    // One row per leaf that actually changed — so a name in three
22    // components is three rows, and a repeated field is one row per
23    // repetition.
24    let paths: Vec<String> = report.changes.iter().map(|c| c.path.to_string()).collect();
25    assert!(paths.contains(&"PID[1]-5[1].1.1".to_string()));
26    assert!(paths.contains(&"PID[1]-5[1].3.1".to_string()));
27    assert!(paths.contains(&"PID[1]-13[2].1.1".to_string()));
28
29    // Every path is fully qualified, which means it is also a valid
30    // `er7 --query` argument: paste one in to see what is there now.
31    for change in &report.changes {
32        assert!(message.query(&change.path.to_string()).is_ok());
33    }
34
35    // A report carries no values — not the old text, and not the new. A
36    // log line quoting the old value puts the patient's name into the log.
37    let printed = report.to_string();
38    for value in ["EVERYWOMAN", "PATID1234", "19610615", "BOSTON"] {
39        assert!(!printed.contains(value));
40    }
41
42    // And nothing that was not there contributes a row: this message has
43    // no GT1 segment, so the guarantor rules matched nothing, which is not
44    // an error.
45    assert!(!paths.iter().any(|p| p.starts_with("GT1")));
46
47    println!("\n{} positions changed", report.len());
48    Ok(())
49}