HL7v2.TypedParser (HL7v2 v3.10.1)

Copy Markdown View Source

Converts a raw-parsed HL7v2 message into typed segment structs.

Each segment in the RawMessage is dispatched to its corresponding segment module via a registry lookup:

  • Known segments (MSH, PID, PV1, ...) are parsed through SegmentModule.parse(raw_fields, separators).
  • Z-segments (names starting with "Z") become HL7v2.Segment.ZXX structs via ZXX.new/2, preserving the original segment name.
  • Unknown segments are kept as raw {name, fields} tuples so nothing is lost.

Summary

Functions

Converts a RawMessage into a TypedMessage with parsed segment structs.

Returns the segment module for a given segment ID, or nil if unknown.

Converts a TypedMessage back into a RawMessage.

Functions

convert(raw_message)

@spec convert(HL7v2.RawMessage.t()) ::
  {:ok, HL7v2.TypedMessage.t()} | {:error, term()}

Converts a RawMessage into a TypedMessage with parsed segment structs.

Returns {:ok, typed_message} on success, or {:error, reason} if conversion fails.

Examples

iex> {:ok, raw} = HL7v2.Parser.parse("MSH|^~\\&|S|F||R|20240101||ADT^A01|1|P|2.5\r")
iex> {:ok, typed} = HL7v2.TypedParser.convert(raw)
iex> %HL7v2.Segment.MSH{} = hd(typed.segments)

segment_module(seg_id)

@spec segment_module(binary()) :: module() | nil

Returns the segment module for a given segment ID, or nil if unknown.

to_raw(typed_message)

Converts a TypedMessage back into a RawMessage.

Each typed segment struct is encoded back to its raw field list via SegmentModule.encode/1. Z-segments use their stored segment_id, and unknown raw tuples pass through unchanged.

Examples

iex> {:ok, raw} = HL7v2.Parser.parse("MSH|^~\\&|S|F||R|20240101||ADT^A01|1|P|2.5\r")
iex> {:ok, typed} = HL7v2.TypedParser.convert(raw)
iex> raw_again = HL7v2.TypedParser.to_raw(typed)
iex> %HL7v2.RawMessage{} = raw_again