Skip to main content

Module builder

Module builder 

Source
Expand description

Building messages.

The builder is structural: you set fields, components and subcomponents as plain text and the builder inserts the delimiters and escapes any delimiter characters that appear inside values. It never lets you write a delimiter by accident, and the output always parses back with Message::parse to the same values.

use hl7kit::builder::{Builder, Value};
use hl7kit::Message;

let mut b = Builder::new();
b.segment("MSH")
    .set(3, "RIS")
    .set(4, "HOSP")
    .set(9, Value::components(["ORM", "O01", "ORM_O01"]))
    .set(10, "MSG0001")
    .set(11, "P")
    .set(12, "2.5.1");
b.segment("PID")
    .set(1, "1")
    .set(3, Value::components(["4MR1", "", "", "HOSP", "MR"]))
    .set(5, Value::components(["Doe", "Jane & John"]));
b.segment("ZDS")
    .set(1, Value::components(["1.2.3", "", "Application", "DICOM"]));

let text = b.build();
assert!(text.starts_with("MSH|^~\\&|RIS|HOSP|||||ORM^O01^ORM_O01|MSG0001|P|2.5.1\r"));
let msg = Message::parse(&text).unwrap();
assert_eq!(msg.get("PID-3.4"), Some("HOSP"));
assert_eq!(msg.get_decoded("PID-5.2").unwrap(), "Jane & John");   // `&` was escaped
assert_eq!(msg.get("ZDS-1.1"), Some("1.2.3"));

Structs§

Builder
Builds a message segment by segment.
SegmentBuilder
One segment under construction.
Value
The value of one field: repetitions of components of subcomponents.

Functions§

escape
Escape the delimiter characters of enc in text so it can be placed in a field as a literal. Line breaks become \.br\.