Current section

Files

Jump to
postgleam src postgleam@codec@int2.erl
Raw

src/postgleam@codec@int2.erl

-module(postgleam@codec@int2).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/postgleam/codec/int2.gleam").
-export([encode/1, decode/1, matcher/0]).
-file("src/postgleam/codec/int2.gleam", 31).
-spec encode(postgleam@value:value()) -> {ok, bitstring()} | {error, binary()}.
encode(Val) ->
case Val of
{integer, N} ->
case (N >= -32768) andalso (N =< 32767) of
true ->
{ok, <<N:16/big>>};
false ->
{error,
<<"int2 codec: value out of range (-32768..32767)"/utf8>>}
end;
_ ->
{error, <<"int2 codec: expected Integer value"/utf8>>}
end.
-file("src/postgleam/codec/int2.gleam", 42).
-spec decode(bitstring()) -> {ok, postgleam@value:value()} | {error, binary()}.
decode(Data) ->
case Data of
<<N:16/big-signed>> ->
{ok, {integer, N}};
_ ->
{error, <<"int2 codec: expected 2 bytes"/utf8>>}
end.
-file("src/postgleam/codec/int2.gleam", 21).
-spec build(integer()) -> postgleam@codec:codec().
build(Type_oid) ->
{codec, <<"int2"/utf8>>, Type_oid, binary, fun encode/1, fun decode/1}.
-file("src/postgleam/codec/int2.gleam", 11).
-spec matcher() -> postgleam@codec:codec_matcher().
matcher() ->
{codec_matcher, <<"int2"/utf8>>, [21], none, binary, fun build/1}.