ex_hl7 v1.0.0 HL7.Codec

Functions that decode and encode HL7 fields, repetitions, components and subcomponents.

Each type of item has a intermediate representation, that will vary depending on whether the trim option was used when decoding or encoding. If we set trim to true, some trailing optional items and separators will be omitted from the decoded or encoded result, as we can see in the following example:

iex> text = "504599^223344&&IIN&^~"
...> decode_field!(text, separators(), trim: true)
{"504599", {"223344", "", "IIN"}}
...> decode_field!(text, separators(), trim: false)
[{"504599", {"223344", "", "IIN", ""}, ""}, ""]

Both representations are correct, given that HL7 allows trailing items that are empty to be omitted. This causes an ambiguity because the same item can be interpreted in several ways when it is the first and only item present.

For example, in the following HL7 segment the item in the third field (504599) might be the same in both cases (i.e. the first component of the second field):

  1. AUT||504599^^||||0000190447|^||
  2. AUT||504599||||0000190447|^||

But for this module it has two different representations:

  1. First component of the second field
  2. Second field

To resolve the ambiguity in the HL7 syntax, the code decoding and encoding HL7 segments using the functions in this module must be aware of this issue and deal with it accordingly when performing lookups or comparisons.

Link to this section Summary

Functions

Decode a binary holding one or more HL7 components into its intermediate representation.

Decode a binary holding an HL7 field into its intermediate representation (IR).

Decode a binary holding one or more HL7 subcomponents into its intermediate representation.

Checks if a value is empty. A value is considered empty when it is nil or an empty string.

Escape a string that may contain separators using the HL7 escaping rules.

Return the separator corresponding to an item type.

Return the default separators used to encode HL7 messages in their compiled format. These are

Convert an escaped string into its original value.

Link to this section Functions

Link to this function

decode_components!(components, separators \\ {124, 94, 38, 126}, trim \\ true)

decode_components!(binary(), Type.separators(), trim :: boolean()) ::
  Type.component() | no_return()

Decode a binary holding one or more HL7 components into its intermediate representation.

Link to this function

decode_field!(field, separators \\ {124, 94, 38, 126}, trim \\ true)

decode_field!(binary(), Type.separators(), trim :: boolean()) ::
  Type.field() | no_return()

Decode a binary holding an HL7 field into its intermediate representation (IR).

Examples

iex> decode_field!("PREPAGA^112233^IIN")
{"PREPAGA", "112233", "IIN"}
...> decode_field!("112233~IIN")
["112233", "IIN"]
...> decode_field!("""")
nil
...> decode_field!("")
""
Link to this function

decode_subcomponents!(component, separators \\ {124, 94, 38, 126}, trim \\ true)

decode_subcomponents!(binary(), Type.separators(), trim :: boolean()) ::
  Type.subcomponent() | no_return()

Decode a binary holding one or more HL7 subcomponents into its intermediate representation.

Link to this function

decode_value!(value, type \\ :string)

decode_value!(Type.field(), Type.value_type()) ::
  Type.value() | nil | no_return()
Link to this macro

empty?(value)

(macro)

Checks if a value is empty. A value is considered empty when it is nil or an empty string.

Link to this function

encode_components!(components, separators \\ {124, 94, 38, 126}, trim \\ true)

encode_components!(Type.component(), Type.separators(), trim :: boolean()) ::
  iodata() | no_return()
Link to this function

encode_field!(field, separators \\ {124, 94, 38, 126}, trim \\ true)

encode_field!(Type.field(), Type.separators(), trim :: boolean()) ::
  iodata() | no_return()
Link to this function

encode_subcomponents!(subcomponents, separators \\ {124, 94, 38, 126}, trim \\ true)

encode_subcomponents!(Type.subcomponent(), Type.separators(), trim :: boolean()) ::
  iodata() | no_return()
Link to this function

encode_value!(value, type \\ :string)

encode_value!(Type.value() | nil, Type.value_type() | nil) ::
  binary() | no_return()
Link to this function

escape(value, separators \\ {124, 94, 38, 126}, escape_char \\ 92)

escape(binary(), Type.separators(), escape_char :: byte()) :: binary()

Escape a string that may contain separators using the HL7 escaping rules.

Arguments

  • value: a string to escape; it may or may not contain separator characters.

  • separators: a tuple containing the item separators to be used when generating the message as returned by HL7.Codec.set_separators/1. Defaults to HL7.Codec.separators.

  • escape_char: character to be used as escape delimiter. Defaults to ?\\.

Examples

iex> escape("ABCDEF")
"ABCDEF"
...> escape("ABC|DEF^GHI", separators: separators(), escape_char: ?\\)
"ABC\\F\\DEF\\S\\GHI"
Link to this function

format_date!(date)

Link to this function

format_datetime(datetime)

Link to this function

match_separator(char, separators \\ {124, 94, 38, 126})

match_separator(byte(), Type.separators()) ::
  {:match, Type.item_type()} | :nomatch
Link to this function

separator(item_type, separators \\ {124, 94, 38, 126})

separator(Type.item_type(), Type.separators()) :: byte()

Return the separator corresponding to an item type.

Link to this function

separators()

separators() :: Type.separators()

Return the default separators used to encode HL7 messages in their compiled format. These are:

  • |: field separator
  • ^: component separator
  • &: subcomponent separator
  • ~: repetition separator

To use custom separators in a message use HL7.Codec.set_separators/1 and pass the returned value as argument to the encoding functions.

Link to this function

set_separators(args)

set_separators(Keyword.t()) :: Type.separators()
Link to this function

unescape(value, separators \\ {124, 94, 38, 126}, escape_char \\ 92)

unescape(binary(), Type.separators(), escape_char :: byte()) :: binary()

Convert an escaped string into its original value.

Arguments

  • value: a string to unescape; it may or may not contain escaped characters.

  • escape_char: character that was used as escape delimiter. Defaults to ?\\.

Examples

iex> "ABCDEF" = HL7.unescape("ABCDEF")
iex> "ABC|DEF|GHI" = HL7.Codec.unescape("ABC\\F\\DEF\\F\\GHI", ?\\)