anchor.diff:      end.
   
   
  --spec segment(segment_id(), raw_msg(), Repetition :: non_neg_integer()) -> raw_segment() | undefined.
  +-spec segment(segment_id(), msg() | raw_msg(), Repetition :: non_neg_integer()) -> segment() | raw_segment() | undefined.
   segment(SegmentId, [Segment | Tail], Repetition) ->
       case element(1, Segment) of
           SegmentId when Repetition =:= 1 ->
  
positive.code:     end.
  
  
  -spec encode(Msg :: msg()) -> {ok, iolist()} | {error, Reason :: any()}.
  encode(Msg) ->
      encode(Msg, []).
  
  -spec encode(Msg :: msg(), [encode_option()]) -> {ok, iolist()} | {error, Reason :: any()}.
  encode(Msg, Options) ->
      RawMsg =
          case lists:member(raw, Options) of
              true ->
                  Msg;
              false ->
                  case lists:member(disable_simplify, Options) of
                      true ->
                          encode_msg(Msg, []);
                      false ->
                          encode_and_simplify_msg(Msg, [])
                  end
          end,
      raw_encode(RawMsg).
  
negative.code: 
  -on_load(init/0).
  
  -type field_index()                             :: non_neg_integer() | [non_neg_integer()].
  -type field_data_type()                         :: string | integer | date | float.
  -type field_length()                            :: non_neg_integer().
  
  -type field()                                   :: binary() | integer() | calendar:date() | calendar:datetime() | float() | undefined.
  -type segment_id()                              :: atom().
  -type segment()                                 :: tuple(segment_id() | field()).
  -type msg()                                     :: [segment()].
  
  -type raw_field()                               :: binary() | tuple() | undefined.
  -type raw_segment_id()                          :: binary().
  -type raw_segment()                             :: tuple(raw_segment_id() | raw_field()).
  -type raw_msg()                                 :: [raw_segment()].
  
  -type buffer()                                  :: binary().
  
  -type decode_option()                           :: {format, raw | record}.
  -type encode_option()                           :: {format, raw | record} | {simplify, boolean()}.
  
  -type error()                                   :: {error, Reason :: term()}.
  
  
  %%%===================================================================
  %%% API
  %%%===================================================================
  
  -spec init() -> ok | error().
  init() ->
      erlang:load_nif("priv/ehl7", 0).
  
  
  -spec decode(buffer()) -> msg() | error().
  decode(Buffer) ->
      decode(Buffer, [{format, record}, {simplify, true}]).
  
  -spec decode(buffer(), [decode_option()]) -> msg() | raw_msg() | error().
  decode(Buffer, Options) ->
      case raw_decode(Buffer) of
          [] ->
              {error, {invalid_hl7_message, Buffer}};
          RawMsg when is_list(RawMsg) ->
              case proplists:get_value(format, Options, record) of
                  raw  ->
                      RawMsg;
                  record ->
                      decode_msg(RawMsg, [])
              end;
          {error, _Reason} = Error ->
  