mirror of
https://github.com/msgpack/msgpack-c.git
synced 2025-03-31 07:58:20 +02:00
erlang: external APIs' type/specs.
This commit is contained in:
parent
fd80693420
commit
b1e66256ce
@ -23,17 +23,18 @@
|
||||
%% supported formats. APIs are almost compatible
|
||||
%% for C API (http://msgpack.sourceforge.jp/c:doc)
|
||||
%% except buffering functions (both copying and zero-copying).
|
||||
-export([pack/1, unpack/1, unpack_all/1, test/0]).
|
||||
|
||||
-include_lib("eunit/include/eunit.hrl").
|
||||
-export([pack/1, unpack/1, unpack_all/1]).
|
||||
|
||||
% compile:
|
||||
% erl> c(msgpack).
|
||||
% erl> S = <some term>.
|
||||
% erl> {S, <<>>} = msgpack:unpack( msgpack:pack(S) ).
|
||||
-type reason() :: enomem.
|
||||
-type reason() :: enomem | badarg.
|
||||
-type map() :: any(). % there's no 'dict' type...
|
||||
-type msgpack_term() :: [msgpack_term()] | integer() | float() | {dict, map()}.
|
||||
|
||||
% ===== external APIs ===== %
|
||||
-spec pack(Term::msgpack_term()) -> binary().
|
||||
pack(O) when is_integer(O) andalso O < 0 ->
|
||||
pack_int_(O);
|
||||
pack(O) when is_integer(O) ->
|
||||
@ -57,15 +58,16 @@ pack(_) ->
|
||||
% if failed in decoding and not end, get more data
|
||||
% and feed more Bin into this function.
|
||||
% TODO: error case for imcomplete format when short for any type formats.
|
||||
-spec unpack( binary() )-> {term(), binary()} | {more, non_neg_integer()} | {error, reason()}.
|
||||
-spec unpack( binary() )-> {msgpack_term(), binary()} | {more, non_neg_integer()} | {error, reason()}.
|
||||
unpack(Bin) when not is_binary(Bin)->
|
||||
{error, badard};
|
||||
{error, badarg};
|
||||
unpack(Bin) when bit_size(Bin) >= 8 ->
|
||||
<< Flag:8/unsigned-integer, Payload/binary >> = Bin,
|
||||
unpack_(Flag, Payload);
|
||||
unpack(_)-> % when bit_size(Bin) < 8 ->
|
||||
{more, 8}.
|
||||
|
||||
-spec unpack_all( binary() ) -> [msgpack_term()].
|
||||
unpack_all(Data)->
|
||||
case unpack(Data) of
|
||||
{ Term, Binary } when bit_size(Binary) =:= 0 ->
|
||||
@ -318,6 +320,8 @@ unpack_(Flag, Payload)->
|
||||
{error, no_code_matches}
|
||||
end.
|
||||
|
||||
% ===== test codes ===== %
|
||||
-include_lib("eunit/include/eunit.hrl").
|
||||
-ifdef(EUNIT).
|
||||
|
||||
compare_all([], [])-> ok;
|
||||
|
Loading…
x
Reference in New Issue
Block a user