Crates
hl7-2-derive
#[derive(FromHl7)] and #[derive(ToHl7)] for struct mode
What it is
Map a struct’s fields to HL7 v2 message paths once, in the type definition, instead of writing the same accessor calls at every call site. You do not depend on this crate directly — it arrives through `hl7-2`’s `derive` feature, which is what keeps the default `hl7-2` build to exactly one dependency.
hl7-2 = { version = "0.2", features = ["derive"] }Keeping the macros in a crate of their own is what lets the default build of hl7-2 keep exactly one dependency: syn and quote are
compiled only for callers who ask for the macros.
Usage
use hl7_2::{FromHl7, ToHl7, Raw};
#[derive(FromHl7, ToHl7)]
struct Admission {
#[hl7("PID-1")] sequence: u32,
#[hl7("PID-3")] identifiers: Vec<String>,
#[hl7("PID-5.1.1")] family_name: String,
#[hl7("PID-8")] sex: Option<String>,
#[hl7(nested)] visit: Visit, // its own FromHl7 / ToHl7
#[hl7(raw)] raw: Raw, // the whole message, kept alongside
}
let admission: Admission = hl7_2::parse(text)?.decode()?;
assert_eq!(admission.family_name, "EVERYWOMAN");
// The one vendor field no struct models — same object, no second parse.
assert_eq!(admission.raw.get("ZPD-1")?.as_deref(), Some("local"));The attributes
One attribute per field.
| Attribute | On read | On write |
|---|---|---|
#[hl7("PID-5.1")] | Read the path. | Write the path. |
#[hl7(nested)] | The field's own FromHl7. | The field's own ToHl7. |
#[hl7(raw)] | The whole message, as a Raw. | Skipped. |
| none | Default::default() | Skipped. |
Field types
Field types convert through hl7_2::FromHl7Value and ToHl7Value: String, bool, the integer and floating-point
types, and Option<T> and Vec<T> of those — Option for a value that may be absent, Vec for one that repeats.
A plain type is required, and a path that names nothing is Error::MissingField.
Implement hl7_2::FromHl7Text for a domain type of your own and Option and Vec of it follow.
Writing
Writing needs the segments to exist already. Build the message with hl7_2::Builder — whose encode method takes a ToHl7 — or add
them with Message::append_segment.
This crate has no spec/index.md of its own: the normative description of struct mode
is hl7-2's spec/index.md §6. A fuller walkthrough
is in Struct mode and derive.
Related crates
hl7-2
HL7 v2 itself: parse, navigate, validate, modify, render
hl7-3-derive
#[derive(FromElement)] for HL7 v3 struct mode