<commit_before>%%%-------------------------------------------------------------------
%%% @author <NAME> <<EMAIL>>
%%% @copyright (C) 2011 <NAME>
%%% @doc Module that parses and generates HL7 messages.
%%% @end
%%%-------------------------------------------------------------------
-module(ehl7).
-author('<NAME> <<EMAIL>>').

%% API
-export([decode/1, decode/2, encode/1, encode/2]).
-export([segment/2, segment/3, segment_count/2]).
-export([field/2, field/4, repetition/2, repetition/4, component/2, component/4, subcomponent/2, subcomponent/4]).
-export([dump/1]).

-export_type([raw_msg/0, msg/0, raw_segment_id/0, segment_id/0, raw_segment/0, segment/0,
              raw_field/0, field/0, field_index/0, field_data_type/0, field_length/0]).

-on_load(init/0).

-define(APP, ehl7).

-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() ->
    PrivDir = case code:priv_dir(?APP) of
                  {error, bad_name} -> "priv";
                  PrivDir1          -> PrivDir1
              end,
    erlang:load_nif(filename:join(PrivDir, "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 ->
            Error
    end.<commit_msg>Add failsafe when retrieving the "priv" directory of the application<commit_after>%%%-------------------------------------------------------------------
%%% @author <NAME> <<EMAIL>>
%%% @copyright (C) 2011 <NAME>
%%% @doc Module that parses and generates HL7 messages.
%%% @end
%%%-------------------------------------------------------------------
-module(ehl7).
-author('<NAME> <<EMAIL>>').

%% API
-export([decode/1, decode/2, encode/1, encode/2]).
-export([segment/2, segment/3, segment_count/2]).
-export([field/2, field/4, repetition/2, repetition/4, component/2, component/4, subcomponent/2, subcomponent/4]).
-export([dump/1]).

-export_type([raw_msg/0, msg/0, raw_segment_id/0, segment_id/0, raw_segment/0, segment/0,
              raw_field/0, field/0, field_index/0, field_data_type/0, field_length/0]).

-on_load(init/0).

-define(APP, ehl7).

-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() ->
    PrivDir = case code:priv_dir(?APP) of
                  {error, bad_name} ->
                      EbinDir = filename:dirname(code:which(?MODULE)),
                      AppPath = filename:dirname(EbinDir),
                      filename:join(AppPath, "priv");
                  Path ->
                      Path
              end,
    erlang:load_nif(filename:join(PrivDir, "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 ->
            Error
    end.
{'fraction_non_alphanumeric': 0.23462270133164237, 'fraction_numerical': 0.012365250475586557, 'mean_word_length': 1.5899014778325122, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1010471', 'n_tokens_mistral': 1738, 'n_tokens_neox': 1664, 'n_words': 369}