Crate hl7_parser
source ·Expand description
HL7v2 message parsing in Rust.
Parses the structure of HL7v2 messages, but does not validate the correctness of the messages.
Parsing is centered around the ParsedMessage type.
Examples
Parsing a ParsedMessage
use hl7_parser::ParsedMessage;
use std::num::NonZeroUsize;
let message = r#"
MSH|^~\&|AccMgr|1|||20050110045504||ADT^A01|599102|P|2.3|||
PID|1||10006579^^^1^MRN^1||DUCK^DONALD^D||19241010|M||1|111 DUCK ST^^FOWL^CA^999990000^^M|1|8885551212|8885551212|1|2||40007716^^^AccMgr^VN^1|123121234|||||||||||NO NK1|1|DUCK^HUEY|SO|3583 DUCK RD^^FOWL^CA^999990000|8885552222||Y||||||||||||||
PV1|1|I|PREOP^101^1^1^^^S|3|||37^DISNEY^WALT^^^^^^AccMgr^^^^CI|||01||||1|||37^DISNEY^WALT^^^^^^AccMgr^^^^CI|2|40007716^^^AccMgr^VN|4|||||||||||||||||||1||G|||20050110045253||||||
"#;
let message = ParsedMessage::parse(message.trim(), true).expect("can parse message");
let message_type = message.get_field_source(("MSH", 0), NonZeroUsize::new(9).unwrap());
assert_eq!(message_type.unwrap(), "ADT^A01");
Querying a ParsedMessage
use hl7_parser::ParsedMessage;
let message = include_str!("../test_assets/sample_adt_a01.hl7");
let message = ParsedMessage::parse(&message, true).expect("can parse message");
let trigger_event = message.query_value("MSH.9.2").expect("can parse location query");
assert_eq!(trigger_event, Some("A01"));
Locating the Cursor Within A ParsedMessage
(The cursor being the character index of some point within the buffer)
use hl7_parser::ParsedMessage;
use std::num::NonZeroUsize;
let message = r#"
MSH|^~\&|AccMgr|1|||20050110045504||ADT^A01|599102|P|2.3|||
PID|1||10006579^^^1^MRN^1||DUCK^DONALD^D||19241010|M||1|111 DUCK ST^^FOWL^CA^999990000^^M|1|8885551212|8885551212|1|2||40007716^^^AccMgr^VN^1|123121234|||||||||||NO NK1|1|DUCK^HUEY|SO|3583 DUCK RD^^FOWL^CA^999990000|8885552222||Y||||||||||||||
PV1|1|I|PREOP^101^1^1^^^S|3|||37^DISNEY^WALT^^^^^^AccMgr^^^^CI|||01||||1|||37^DISNEY^WALT^^^^^^AccMgr^^^^CI|2|40007716^^^AccMgr^VN|4|||||||||||||||||||1||G|||20050110045253||||||
"#;
let message = ParsedMessage::parse(message.trim(), true).expect("can parse message");
let location = message.locate_cursor(25);
assert_eq!(location.segment.unwrap().0, "MSH");
assert_eq!(location.field.unwrap().0.get(), 7);
assert_eq!(location.field.unwrap().1.source(message.source), "20050110045504");
Parsing ParsedMessage Timestamps
#[cfg(feature = "chrono")]
let ts = "20230312195905-0700";
#[cfg(feature = "chrono")]
let ts = parse_timestamp_chrono(ts)
.expect("can parse timestamp")
.earliest()
.expect("can convert to datetime");
#[cfg(feature = "time")]
let ts = "20230312195905-0700";
#[cfg(feature = "time")]
let ts = parse_timestamp_time(ts).expect("can parse timestamp");
assert_eq!(ts.year(), 2023);
#[cfg(all(feature = "chrono", not(feature = "time")))]
assert_eq!(ts.month(), 3);
#[cfg(feature = "time")]
assert_eq!(ts.month(), time::Month::March);
assert_eq!(ts.day(), 12);
assert_eq!(ts.hour(), 19);
assert_eq!(ts.minute(), 59);
assert_eq!(ts.second(), 05);
Decoding Encoded Strings
use hl7_parser::Separators;
let separators = Separators::default();
assert_eq!(
separators.decode(r#"Pierre DuRho\S\ne \T\ Cie"#).as_str(),
r#"Pierre DuRho^ne & Cie"#
);
Structs
- Represents an HL7v2 sub-component
- Represents an HL7v2 field
- Results from locating a cursor within a message
- A query for a particular piece of a message, to be used in ParsedMessage::query (or ParsedMessageOwned::query)
- A parsed message. The message structure is valid, but the contents may or may not be.
- A parsed message that owns its string slice. The message structure is valid, but the contents may or may not be.
- Represents an HL7v2 repeat of a field
- Represents an HL7v2 segment
- Wrapper around segments; HL7 messages can contain multiple segments of the same type (ex: ORU messages often contain multiple OBX segments)
- The separators for the message
- Represents an HL7v2 sub-component
Enums
- Errors that can occur during parsing
- Errors that can occur when parsing timestamps
Traits
- A trait for accessing components on fields, to extend Option<&Repeat> with short-circuit access
- A trait for accessing components on fields, to extend Option<&mut Repeat> with short-circuit access
- A trait for accessing fields on segments, to extend Option<&Segment> with short-circuit access
- A trait for accessing fields on segments, to extend Option<&mut Segment> with short-circuit access
- A trait for accessing repeat on fields, to extend Option<&Field> with short-circuit access
- A trait for accessing repeat on fields, to extend Option<&mut Field> with short-circuit access
- A trait for accessing sub-components on fields, to extend Option<&Component> with short-circuit access
- A trait for accessing sub-components on fields, to extend Option<&mut Component> with short-circuit access
Functions
- Parse an HL7 timestamp
- Parse an HL7 timestamp