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):
AUT||504599^^||||0000190447|^||
AUT||504599||||0000190447|^||
But for this module it has two different representations:
- First component of the second field
- 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
decode_components!(components, separators \\ {124, 94, 38, 126}, trim \\ true)
Decode a binary holding one or more HL7 components into its intermediate representation.
decode_field!(field, separators \\ {124, 94, 38, 126}, trim \\ true)
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!("")
""
decode_subcomponents!(component, separators \\ {124, 94, 38, 126}, trim \\ true)
Decode a binary holding one or more HL7 subcomponents into its intermediate representation.
decode_value!(value, type \\ :string)
decode_value!(Type.field(), Type.value_type()) :: Type.value() | nil | no_return()
Checks if a value is empty. A value is considered empty when it is nil
or an empty string.
encode_components!(components, separators \\ {124, 94, 38, 126}, trim \\ true)
encode_field!(field, separators \\ {124, 94, 38, 126}, trim \\ true)
encode_subcomponents!(subcomponents, separators \\ {124, 94, 38, 126}, trim \\ true)
encode_value!(value, type \\ :string)
escape(value, separators \\ {124, 94, 38, 126}, escape_char \\ 92)
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 byHL7.Codec.set_separators/1
. Defaults toHL7.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"
format_date!(date)
format_datetime(datetime)
match_separator(char, separators \\ {124, 94, 38, 126})
match_separator(byte(), Type.separators()) :: {:match, Type.item_type()} | :nomatch
separator(item_type, separators \\ {124, 94, 38, 126})
separator(Type.item_type(), Type.separators()) :: byte()
Return the separator corresponding to an item type.
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.
unescape(value, separators \\ {124, 94, 38, 126}, escape_char \\ 92)
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", ?\\)