Compare commits
2 Commits
main
...
dev/encode
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9a49a02c37 | ||
|
|
0dff65d083 |
166
src/berty_etf_encoder.erl
Normal file
166
src/berty_etf_encoder.erl
Normal file
@@ -0,0 +1,166 @@
|
||||
%%%===================================================================
|
||||
%%% @copyright Erlang Punch
|
||||
%%% @author Mathieu Kerjouan
|
||||
%%% @doc
|
||||
%%% @end
|
||||
%%%===================================================================
|
||||
-module(berty_etf_encoder).
|
||||
-export([default_options/0]).
|
||||
-export([encode/1, encode/2]).
|
||||
-include("berty.hrl").
|
||||
-include_lib("kernel/include/logger.hrl").
|
||||
-include_lib("proper/include/proper.hrl").
|
||||
-include_lib("eunit/include/eunit.hrl").
|
||||
|
||||
-record(?MODULE, { id = make_ref()
|
||||
, module = ?MODULE
|
||||
, started_at = undefined
|
||||
, ended_at = undefined
|
||||
, depth = 0
|
||||
, metric_process = berty_metrics
|
||||
}).
|
||||
|
||||
%%--------------------------------------------------------------------
|
||||
%% @hidden
|
||||
%% @doc
|
||||
%% @end
|
||||
%%--------------------------------------------------------------------
|
||||
?CODE( 70, new_float_ext);
|
||||
?CODE( 77, bit_binary_ext);
|
||||
?CODE( 82, atom_cache_ref);
|
||||
?CODE( 88, new_pid_ext);
|
||||
?CODE( 89, new_port_ext);
|
||||
?CODE( 90, newer_reference_ext);
|
||||
?CODE( 97, small_integer_ext);
|
||||
?CODE( 98, integer_ext);
|
||||
?CODE( 99, float_ext);
|
||||
?CODE(100, atom_ext);
|
||||
?CODE(101, reference_ext);
|
||||
?CODE(102, port_ext);
|
||||
?CODE(103, pid_ext);
|
||||
?CODE(104, small_tuple_ext);
|
||||
?CODE(105, large_tuple_ext);
|
||||
?CODE(106, nil_ext);
|
||||
?CODE(107, string_ext);
|
||||
?CODE(108, list_ext);
|
||||
?CODE(109, binary_ext);
|
||||
?CODE(110, small_big_ext);
|
||||
?CODE(111, large_big_ext);
|
||||
?CODE(112, new_fun_ext);
|
||||
?CODE(113, export_ext);
|
||||
?CODE(114, new_reference_ext);
|
||||
?CODE(115, small_atom_ext);
|
||||
?CODE(116, map_ext);
|
||||
?CODE(117, fun_ext);
|
||||
?CODE(118, atom_utf8_ext);
|
||||
?CODE(119, small_atom_utf8_ext);
|
||||
?CODE(120, v4_port_ext);
|
||||
?CODE(121, local_ext);
|
||||
code(Elsewise) -> Elsewise.
|
||||
|
||||
|
||||
%%--------------------------------------------------------------------
|
||||
%%
|
||||
%%--------------------------------------------------------------------
|
||||
default_options() ->
|
||||
#{}.
|
||||
|
||||
%%--------------------------------------------------------------------
|
||||
%%
|
||||
%%--------------------------------------------------------------------
|
||||
encode(Term) ->
|
||||
encode(Term, default_options()).
|
||||
|
||||
%%--------------------------------------------------------------------
|
||||
%%
|
||||
%%--------------------------------------------------------------------
|
||||
encode(Term, Options) ->
|
||||
encode(Term, Options, #?MODULE{}).
|
||||
|
||||
%%--------------------------------------------------------------------
|
||||
%%
|
||||
%%--------------------------------------------------------------------
|
||||
encode(Term, Options, State)
|
||||
when is_atom(Term) ->
|
||||
encode_atom(Term, Options, State);
|
||||
encode(Term, Options, State) ->
|
||||
ok.
|
||||
|
||||
%%--------------------------------------------------------------------
|
||||
%%
|
||||
%%--------------------------------------------------------------------
|
||||
encode_atom(Term, Options, State)
|
||||
when is_atom(Term) ->
|
||||
Binary = atom_to_binary(Term),
|
||||
Length = byte_size(Binary).
|
||||
|
||||
encode_atom_ext(Term, Options, State)
|
||||
when is_atom(Term) ->
|
||||
Binary = atom_to_binary(Term),
|
||||
Length = byte_size(Binary),
|
||||
if Length>256 ->
|
||||
<<?ATOM_EXT, Length:16, Binary/binary>>;
|
||||
true ->
|
||||
{error, []}
|
||||
end.
|
||||
|
||||
encode_atom_ext({atom, Binary, Length}, Options, State)
|
||||
when is_binary(Binary), is_integer(Length), Length>255 ->
|
||||
<<?ATOM_EXT, Length:16, Binary/binary>>.
|
||||
|
||||
encode_atom_utf8_ext({atom, Binary, Length}, Options, State)
|
||||
when is_binary(Binary), is_integer(Length), Length>255 ->
|
||||
<<?ATOM_UTF8_EXT, Length:16, Binary/binary>>.
|
||||
|
||||
encode_small_atom_ext({atom, Binary, Length}, Options, State)
|
||||
when is_binary(Binary), is_integer(Length), Length=<255 ->
|
||||
<<?SMALL_ATOM_EXT, Length:8, Binary/binary>>.
|
||||
|
||||
%%--------------------------------------------------------------------
|
||||
%% @doc
|
||||
%% @end
|
||||
%%--------------------------------------------------------------------
|
||||
-spec encode_small_atom_ext(Data, Options, State) -> Return when
|
||||
Data :: {atom, binary(), pos_integer()}
|
||||
| {atom, binary()}
|
||||
| {atom, atom()}
|
||||
| {atom, string()}
|
||||
| {atom, integer()},
|
||||
Options :: term(),
|
||||
State :: term(),
|
||||
Return :: binary().
|
||||
|
||||
encode_small_atom_ext({atom, Atom}, Options, State)
|
||||
when is_atom(Atom) ->
|
||||
Binary = atom_to_binary(List),
|
||||
Length = length(List),
|
||||
encode_small_atom_ext({atom, Binary, Length}, Options, State);
|
||||
encode_small_atom_ext({atom, List}, Options, State)
|
||||
when is_list(List) ->
|
||||
Binary = list_to_binary(List),
|
||||
Length = length(List),
|
||||
encode_small_atom_ext({atom, Binary, Length}, Options, State);
|
||||
encode_small_atom_ext({atom, Integer}, Options, State)
|
||||
when is_integer(Integer) ->
|
||||
Binary = integer_to_binary(Integer),
|
||||
encode_small_atom_ext({atom, Binary}, Options, State);
|
||||
encode_small_atom_ext({atom, Binary}, Options, State)
|
||||
when is_binary(Binary) ->
|
||||
Length = byte_size(Binary),
|
||||
encode_small_atom_ext({atom, Binary, Length}, Options, State);
|
||||
|
||||
encode_small_atom_ext({atom, Binary, Length}, Options, State)
|
||||
when is_binary(Binary), is_integer(Length), Length=<255 ->
|
||||
<<?SMALL_ATOM_EXT, Length:8, Binary/binary>>;
|
||||
encode_small_atom_ext(Data, Options, State) ->
|
||||
{error, []}.
|
||||
|
||||
encode_small_atom_ext_test() ->
|
||||
?assertEqual(<<?SMALL_ATOM_EXT, 4, "test">>, encode_small_atom_ext({atom, test}, #{}, #{})),
|
||||
?assertEqual(<<?SMALL_ATOM_EXT, 4, "test">>, encode_small_atom_ext({atom, "test"}, #{}, #{})),
|
||||
?assertEqual(<<?SMALL_ATOM_EXT, 4, "test">>, encode_small_atom_ext({atom, <<"test">>}, #{}, #{})),
|
||||
?assertEqual(<<?SMALL_ATOM_EXT, 4, "test">>, encode_small_atom_ext({atom, <<"test">>, 4}, #{}, #{})).
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user