Current section

Files

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

src/postgleam@codec@float4.erl

-module(postgleam@codec@float4).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/postgleam/codec/float4.gleam").
-export([decode/1, encode/1, matcher/0]).
-file("src/postgleam/codec/float4.gleam", 43).
-spec decode(bitstring()) -> {ok, postgleam@value:value()} | {error, binary()}.
decode(Data) ->
case Data of
<<16#7F, 16#80, 16#00, 16#00>> ->
{ok, pos_infinity};
<<16#FF, 16#80, 16#00, 16#00>> ->
{ok, neg_infinity};
<<16#7F, Hi, _:16>> when Hi > 16#80 ->
{ok, na_n};
<<16#FF, Hi@1, _:16>> when Hi@1 > 16#80 ->
{ok, na_n};
<<16#7F, 16#80, A, B>> when (A > 0) orelse (B > 0) ->
{ok, na_n};
<<16#FF, 16#80, A@1, B@1>> when (A@1 > 0) orelse (B@1 > 0) ->
{ok, na_n};
_ ->
case postgleam_ffi:decode_float32(Data) of
{ok, F} ->
{ok, {float, F}};
{error, E} ->
{error, E}
end
end.
-file("src/postgleam/codec/float4.gleam", 32).
-spec encode(postgleam@value:value()) -> {ok, bitstring()} | {error, binary()}.
encode(Val) ->
case Val of
{float, F} ->
{ok, postgleam_ffi:encode_float32(F)};
na_n ->
{ok, <<16#7F, 16#C0, 16#00, 16#00>>};
pos_infinity ->
{ok, <<16#7F, 16#80, 16#00, 16#00>>};
neg_infinity ->
{ok, <<16#FF, 16#80, 16#00, 16#00>>};
{integer, N} ->
{ok, postgleam_ffi:encode_float32(erlang:float(N))};
_ ->
{error,
<<"float4 codec: expected Float, NaN, PosInfinity, or NegInfinity"/utf8>>}
end.
-file("src/postgleam/codec/float4.gleam", 22).
-spec build(integer()) -> postgleam@codec:codec().
build(Type_oid) ->
{codec, <<"float4"/utf8>>, Type_oid, binary, fun encode/1, fun decode/1}.
-file("src/postgleam/codec/float4.gleam", 12).
-spec matcher() -> postgleam@codec:codec_matcher().
matcher() ->
{codec_matcher, <<"float4"/utf8>>, [700], none, binary, fun build/1}.