hl7-net
A lightweight HL7 v2 message parser and writer for Rust.
hl7-net is an idiomatic Rust port of the
Efferent HL7-V2 .NET library. It keeps
the original library's HL7 semantics — delimiter handling, escaping, "present but
null" values, MLLP framing and the round-trip self-check — while reworking the API
around Rust conventions (snake_case, Result-based error handling, and a pure-data
element tree).
Features
- Parse HL7 v2 messages into a
Segment/Field/Component/SubComponenttree. - Read and write values by path (
PID.5.1,MSH.9.3,PID(2).3.1). - Full HL7 escaping/unescaping (
\F\,\S\,\R\,\E\,\T\,\Xnn\hex, and the<B>/</B>/<BR>formatting sequences). - "Present but null" (
"") values modelled asOption<String>. - Generate ACK / NACK responses.
- MLLP framing helpers (
<VT> … <FS><CR>) for stream transport. - HL7 date/time parsing and formatting via
jiff. - A parse-time round-trip check: a message is only considered valid if re-serializing it reproduces the (hex-normalized) original.
Installation
Add the crate with Cargo:
Or add it to Cargo.toml manually:
[]
= "0.1"
The minimum supported Rust version (MSRV) is 1.88 (Rust 2024 edition).
Usage
Parsing a message and reading values
use Message;
let text = "MSH|^~\\&|App|Fac|App2|Fac2|20200101000000||ADT^A01^ADT_A01|MSGID|P|2.5\r\
PID|1||PATID1234^5^M11||EVERYMAN^ADAM^A^III||19610615|M\r";
let mut message = with_message;
// `parse` returns Ok(true) when the message round-trips cleanly.
assert!;
// Read by path: segment.field.component.subcomponent (all 1-based).
assert_eq!;
assert_eq!;
// Message-level metadata extracted from MSH.
assert_eq!;
assert_eq!;
Updating values
use Message;
let mut message = parse_str.unwrap;
message.set_value.unwrap;
assert_eq!;
// Serialize back to HL7 text.
let out = message.serialize.unwrap;
assert!;
Acknowledgements and MLLP framing
use Message;
let message = parse_str.unwrap;
// Positive acknowledgement (sender/receiver swapped, MSA|AA).
let ack = message.get_ack.unwrap;
assert_eq!;
// Wrap a message in an MLLP frame for transport over a socket.
let framed: = message.get_mllp.unwrap;
assert_eq!; // <VT>
Encoding helpers and date handling
use HL7Encoding;
use helper;
let enc = default;
let encoded = enc.encode;
assert_eq!;
// Parse an HL7 timestamp (with optional fraction and timezone offset).
let dt = parse_date_time.unwrap;
assert_eq!;
API overview
| Type | Role |
|---|---|
Message |
A parsed message; metadata plus the segment tree and value access |
Segment |
A named segment (MSH, PID, …) holding a list of fields |
Field |
A field; either componentized or carrying repetitions |
Component |
A component made of one or more subcomponents |
SubComponent |
The smallest data unit |
HL7Encoding |
Delimiter set and the escape/unescape routines |
Hl7Error |
Error type carrying a message and an optional category code |
helper |
Date/time, message splitting and MLLP framing utilities |
The element tree is pure data: HL7Encoding is threaded into the parse,
serialize and value methods (e.g. field.value(&enc)) rather than stored on each
node, which keeps the tree borrow-friendly and cheap to clone.
Building from source
License
Licensed under the MIT License.
This project is a derivative work of the
Efferent HL7-V2 .NET library, which is
also MIT licensed; the original copyright notice is retained in LICENSE.txt.