-module(lz4_nif).
-on_load(init/0).
-export([
compress/1,
compress/2,
uncompress/2
]).
-type option() :: term(). % reserved for future use
-type error_reason() :: alloc_failed
| badarg
| compress_failed
| decompress_failed.
-export_type([option/0, error_reason/0]).
-spec init() -> ok.
init() ->
SoName = filename:join(code:priv_dir(lz4_nif), "lz4_nif"),
erlang:load_nif(SoName, 0).
%% @doc Compress `Binary' using the LZ4 block format.
%%
%% Returns `{ok, Compressed}' on success. `Compressed' does *not*
%% include the original size; callers are expected to track it
%% separately and pass it to `uncompress/2'.
-spec compress(binary()) -> {ok, binary()} | {error, error_reason()}.
compress(_Binary) ->
erlang:nif_error(lz4_nif_not_loaded).
%% @doc Same as `compress/1' but accepts an options list.
%%
%% No options are currently interpreted. The two-arity form exists
%% for API parity with `szktty/erlang-lz4' so existing callers can
%% migrate without rewriting.
-spec compress(binary(), [option()]) -> {ok, binary()} | {error, error_reason()}.
compress(_Binary, _Options) ->
erlang:nif_error(lz4_nif_not_loaded).
%% @doc Decompress an LZ4 block-format `Binary' whose original
%% (uncompressed) size was `OrigSize' bytes.
%%
%% Returns `{ok, Decompressed}' on success. The result is an exact
%% byte-match of the original input to `compress/1,2'. If the input
%% is corrupt or `OrigSize' doesn't match, returns
%% `{error, decompress_failed}'.
-spec uncompress(binary(), non_neg_integer()) -> {ok, binary()} | {error, error_reason()}.
uncompress(_Binary, _OrigSize) ->
erlang:nif_error(lz4_nif_not_loaded).