#![forbid(unsafe_code)]
fn main() -> Result<(), er7::Error> {
let text = "MSH|^~\\&|LAB|ACME|EHR|CLINIC|20260815081500||ADT^A08|MSG1|P|2.5\r\
PID|1||\"\"|4|SMITH^JOHN";
let message = er7::parse(text)?;
let pid = message.segment("PID").expect("the message has a PID");
assert!(pid.field(9).is_none());
let empty = pid.field(2).expect("PID-2 was sent");
assert!(empty.is_empty());
assert!(!empty.is_null());
let null = pid.field(3).expect("PID-3 was sent");
assert!(null.is_null());
assert!(!null.is_empty());
for number in [2, 3, 9] {
let action = match pid.field(number) {
None => "absent — leave the stored value alone",
Some(field) if field.is_null() => "null — CLEAR the stored value",
Some(field) if field.is_empty() => "empty — leave the stored value alone",
Some(_) => "a value — store it",
};
println!("PID-{number}: {action}");
}
assert_eq!(message.query("PID-2")?.as_deref(), Some(""));
assert_eq!(message.query("PID-3")?.as_deref(), Some(""));
assert_eq!(message.query("PID-9")?, None);
let path: er7::Path = "PID-3".parse()?;
assert_eq!(message.query_path_raw(&path), vec![r#""""#]);
assert_eq!(
message.query_path_raw(&"PID-2".parse::<er7::Path>()?),
vec![""]
);
println!("PID-3 as sent: {:?}", message.query_path_raw(&path)[0]);
assert_eq!(message.to_er7(), text);
let mixed = er7::parse("MSH|^~\\&|LAB\rPID|\"\"^X")?;
let field = mixed.segment("PID").unwrap().field(1).unwrap();
assert!(!field.is_null());
assert!(field.repetition(1).unwrap().component(1).unwrap().is_null());
let positions = er7::parse("MSH|^~\\&|LAB\rPID||A~~B|^^C")?;
let pid = positions.segment("PID").unwrap();
assert_eq!(pid.field(1).unwrap().repetitions.len(), 0); assert_eq!(pid.field(2).unwrap().repetitions.len(), 3); assert_eq!(
pid.field(3)
.unwrap()
.repetition(1)
.unwrap()
.components
.len(),
3
);
assert_eq!(positions.to_er7(), "MSH|^~\\&|LAB\rPID||A~~B|^^C");
println!("ok");
Ok(())
}