Skip to main content

Crate hl7_mllp

Crate hl7_mllp 

Source
Expand description

§hl7-mllp

Transport-agnostic MLLP (Minimal Lower Layer Protocol) framing for HL7 v2 messages.

MLLP is the standard transport envelope used by HL7 v2 over TCP/IP. This crate provides pure framing logic — encoding and decoding MLLP frames — without coupling to any specific async runtime, I/O library, or transport mechanism.

§What is MLLP?

MLLP (Minimal Lower Layer Protocol) wraps HL7 v2 messages with simple byte delimiters for reliable streaming over TCP. It is defined in HL7 v2.5.1 Appendix C.

§MLLP Frame Format

An MLLP frame wraps an HL7 message with 3 special bytes:

+--------+----------------------------+--------+--------+
|  VT    |      HL7 message bytes     |   FS   |   CR   |
| 0x0B   |        (variable)            | 0x1C   | 0x0D   |
+--------+----------------------------+--------+--------+
   ↑                                    ↑       ↑
   Start of Block                       End of  Line
   (Vertical Tab)                       Block   Terminator
                                        (File   (Carriage
                                        Sep.)   Return)
  • VT (0x0B): Start of block marker. Every frame MUST begin with this byte.
  • FS (0x1C): End of block marker. Every frame MUST end with FS followed by CR.
  • CR (0x0D): Carriage return terminator. Required after FS.

The payload between VT and FS-CR is the raw HL7 message (typically ER7-encoded).

§Design Philosophy

This crate provides three main abstractions:

  • MllpFrame: Stateless encode/decode operations. Use for simple one-shot framing.
  • MllpFramer: Stateful streaming accumulator. Use for network I/O where data arrives in chunks.
  • MllpTransport: Trait for implementing transports (TCP, serial, etc.).

All operations are:

  • Zero-allocation where possible: decode() returns a slice into the original buffer.
  • No async/await: Works with blocking or async code equally well.
  • No I/O opinions: You bring your own sockets/streams.

§Quick Start

§Encode a message for sending

use hl7_mllp::MllpFrame;

let raw_hl7 = b"MSH|^~\\&|SendApp|SendFac|20240101120000||ORU^R01|12345|P|2.5\r";
let framed = MllpFrame::encode(raw_hl7);
// framed now contains: VT + raw_hl7 + FS + CR
// Send `framed` over your TCP socket...

§Decode a received frame

use hl7_mllp::MllpFrame;
use bytes::Bytes;

// Received from TCP socket...
let framed: Bytes = MllpFrame::encode(b"MSH|^~\\&|...");

// decode() returns a slice into the original buffer (zero copy)
let decoded = MllpFrame::decode(&framed).unwrap();
assert_eq!(decoded, b"MSH|^~\\&|...");

§Streaming with MllpFramer

use hl7_mllp::MllpFramer;

let mut framer = MllpFramer::new();

// Data arrives in chunks from TCP...
framer.push(b"\x0BMSH|^~\\&|");
framer.push(b"test message\x1C\x0D");

// Extract complete frame when available
if let Some(frame) = framer.next_frame() {
    // Process the complete frame
    println!("Received {} bytes", frame.len());
}

Structs§

MllpFrame
MLLP frame encoder and decoder.
MllpFramer
Stateful streaming frame accumulator for MLLP protocol.

Enums§

MllpError
Errors produced by MLLP framing operations.

Constants§

CR
MLLP carriage return terminator (CR, 0x0D).
FS
MLLP end-of-block character (FS, 0x1C).
VT
MLLP start-of-block character (VT, 0x0B).

Traits§

MllpTransport
Trait for types that can act as an MLLP byte-stream transport.