Skip to main content

redact_a_message/
redact_a_message.rs

1//! Redact a message with the built-in policy, and see what survived.
2//!
3//! Run with: `cargo run --example redact_a_message`
4
5use er7_redact::{Policy, Redactor};
6
7fn main() -> Result<(), er7_redact::Error> {
8    let text = "MSH|^~\\&|ADT1|MCM|LABADT|MCM|20260815140000||ADT^A08|MSG00001|P|2.5\r\
9                PID|1||PATID1234^^^ADT1^MR||EVERYWOMAN^EVE^E||19610615|F|||\
10                1200 N ELM STREET^^GREENSBORO^NC^27401-1020||(919)379-1212\r\
11                OBX|1|NM|2093-3^Cholesterol^LN||187|mg/dL|100-199|H|||F";
12
13    let mut message = er7::parse(text)?;
14    let report = Redactor::new(Policy::patient_identifiers()).redact(&mut message);
15
16    println!("{}\n", message.to_er7().replace('\r', "\n"));
17    println!("{} positions changed:\n{report}", report.len());
18
19    // The identifiers are gone.
20    assert_eq!(
21        message.query("PID-5")?.as_deref(),
22        Some("REDACTED^REDACTED^REDACTED")
23    );
24    assert_eq!(message.query("PID-7")?.as_deref(), Some("1961"));
25    assert_eq!(message.query("PID-11")?.as_deref(), Some("^^^^"));
26    assert_eq!(message.query("PID-13")?.as_deref(), Some(""));
27    assert_ne!(message.query("PID-3.1")?.as_deref(), Some("PATID1234"));
28
29    // The message is not.
30    assert_eq!(message.control_id().as_deref(), Some("MSG00001"));
31    assert_eq!(message.query("PID-3.4")?.as_deref(), Some("ADT1")); // assigning authority
32    assert_eq!(message.query("PID-8")?.as_deref(), Some("F")); // not an identifier
33    assert_eq!(message.query("OBX-5")?.as_deref(), Some("187")); // the clinical content
34    assert!(er7::parse(&message.to_er7()).is_ok());
35
36    // And the shape did not move: every position that held a value still
37    // exists, so a test that asserted on PID-11.3 still finds it.
38    assert_eq!(message.query("PID-11.3")?.as_deref(), Some(""));
39
40    Ok(())
41}