Current section
Files
Jump to
Current section
Files
src/postgleam@codec@float8.erl
-module(postgleam@codec@float8).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/postgleam/codec/float8.gleam").
-export([decode/1, encode/1, matcher/0]).
-file("src/postgleam/codec/float8.gleam", 43).
-spec decode(bitstring()) -> {ok, postgleam@value:value()} | {error, binary()}.
decode(Data) ->
case Data of
<<16#7F, 16#F0, 16#00, 16#00, 16#00, 16#00, 16#00, 16#00>> ->
{ok, pos_infinity};
<<16#FF, 16#F0, 16#00, 16#00, 16#00, 16#00, 16#00, 16#00>> ->
{ok, neg_infinity};
<<16#7F, 16#F8, _:48>> ->
{ok, na_n};
<<16#FF, 16#F8, _:48>> ->
{ok, na_n};
<<16#7F, 16#F0, A, B, C, D, E, F>> when (((((A > 0) orelse (B > 0)) orelse (C > 0)) orelse (D > 0)) orelse (E > 0)) orelse (F > 0) ->
{ok, na_n};
<<16#FF, 16#F0, A@1, B@1, C@1, D@1, E@1, F@1>> when (((((A@1 > 0) orelse (B@1 > 0)) orelse (C@1 > 0)) orelse (D@1 > 0)) orelse (E@1 > 0)) orelse (F@1 > 0) ->
{ok, na_n};
<<F@2:64/float-big>> ->
{ok, {float, F@2}};
_ ->
{error, <<"float8 codec: expected 8 bytes"/utf8>>}
end.
-file("src/postgleam/codec/float8.gleam", 32).
-spec encode(postgleam@value:value()) -> {ok, bitstring()} | {error, binary()}.
encode(Val) ->
case Val of
{float, F} ->
{ok, <<F:64/float-big>>};
na_n ->
{ok, <<16#7F, 16#F8, 16#00, 16#00, 16#00, 16#00, 16#00, 16#00>>};
pos_infinity ->
{ok, <<16#7F, 16#F0, 16#00, 16#00, 16#00, 16#00, 16#00, 16#00>>};
neg_infinity ->
{ok, <<16#FF, 16#F0, 16#00, 16#00, 16#00, 16#00, 16#00, 16#00>>};
{integer, N} ->
{ok, <<(erlang:float(N)):64/float-big>>};
_ ->
{error,
<<"float8 codec: expected Float, NaN, PosInfinity, or NegInfinity"/utf8>>}
end.
-file("src/postgleam/codec/float8.gleam", 22).
-spec build(integer()) -> postgleam@codec:codec().
build(Type_oid) ->
{codec, <<"float8"/utf8>>, Type_oid, binary, fun encode/1, fun decode/1}.
-file("src/postgleam/codec/float8.gleam", 12).
-spec matcher() -> postgleam@codec:codec_matcher().
matcher() ->
{codec_matcher, <<"float8"/utf8>>, [701], none, binary, fun build/1}.