# `HL7v2.Separator`
[🔗](https://github.com/Balneario-de-Cofrentes/hl7v2/blob/v3.10.1/lib/hl7v2/separator.ex#L1)

Detects and manages HL7v2 message delimiters from MSH-1/MSH-2.

Every HL7v2 message declares its delimiter set in the first 9 characters of the
MSH segment: `MSH` (3 chars) + field separator (MSH-1, 1 char) + encoding
characters (MSH-2, 4 or 5 chars). The defaults are `|^~\&` but the standard
allows any single-byte characters.

HL7 v2.7+ allows an optional 5th encoding character: the **truncation character**.
When present (e.g., `^~\&#`), it indicates that field values ending with this
character were truncated. It is a display hint, not a delimiter.

## Examples

    iex> HL7v2.Separator.default()
    %HL7v2.Separator{field: ?|, component: ?^, repetition: ?~, escape: ?\\, sub_component: ?&, truncation: nil, segment: ?\r}

    iex> {:ok, sep} = HL7v2.Separator.from_msh("MSH|^~\\&|SENDING_APP|")
    iex> sep.field
    ?|

    iex> {:ok, sep} = HL7v2.Separator.from_msh("MSH|^~\\&#|SENDING_APP|")
    iex> sep.truncation
    ?#

# `t`

```elixir
@type t() :: %HL7v2.Separator{
  component: non_neg_integer(),
  escape: non_neg_integer(),
  field: non_neg_integer(),
  repetition: non_neg_integer(),
  segment: non_neg_integer(),
  sub_component: non_neg_integer(),
  truncation: non_neg_integer() | nil
}
```

# `default`

```elixir
@spec default() :: t()
```

Returns the default HL7v2 delimiter set (`|^~\&` with CR segment terminator, no truncation character).

# `encoding_characters`

```elixir
@spec encoding_characters(t()) :: binary()
```

Returns the encoding characters string (MSH-2 value) for this separator set.

When a truncation character is present (v2.7+), the returned string is 5 characters.

## Examples

    iex> HL7v2.Separator.encoding_characters(HL7v2.Separator.default())
    "^~\\&"

# `from_msh`

```elixir
@spec from_msh(binary()) :: {:ok, t()} | {:error, term()}
```

Extracts delimiters from an MSH header binary.

MSH-1 is the single character immediately after `"MSH"` (the field separator).
MSH-2 is the next 4 characters (component, repetition, escape, sub-component),
written as a literal string and NOT delimited. In HL7 v2.7+, a 5th character
(truncation) may follow. The truncation character is distinguished from the
start of the next field by checking whether the 5th byte equals the field
separator: if it does not, it is the truncation character.

Returns `{:ok, separator}` or `{:error, reason}`.

## Examples

    iex> {:ok, sep} = HL7v2.Separator.from_msh("MSH|^~\\&|SendApp|")
    iex> sep.field
    ?|
    iex> sep.component
    ?^

    iex> {:ok, sep} = HL7v2.Separator.from_msh("MSH|^~\\&#|SendApp|")
    iex> sep.truncation
    ?#

---

*Consult [api-reference.md](api-reference.md) for complete listing*
