Current section

Files

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

src/postgleam@codec@path.erl

-module(postgleam@codec@path).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/postgleam/codec/path.gleam").
-export([encode/1, decode/1, matcher/0]).
-file("src/postgleam/codec/path.gleam", 41).
-spec encode_points(list({float(), float()}), bitstring()) -> bitstring().
encode_points(Points, Acc) ->
case Points of
[] ->
Acc;
[{X, Y} | Rest] ->
encode_points(
Rest,
<<Acc/bitstring, X:64/float-big, Y:64/float-big>>
)
end.
-file("src/postgleam/codec/path.gleam", 25).
-spec encode(postgleam@value:value()) -> {ok, bitstring()} | {error, binary()}.
encode(Val) ->
case Val of
{path, Closed, Points} ->
Closed_byte = case Closed of
true ->
1;
false ->
0
end,
Npts = erlang:length(Points),
Header = <<Closed_byte, Npts:32/big>>,
Body = encode_points(Points, <<>>),
{ok, <<Header/bitstring, Body/bitstring>>};
_ ->
{error, <<"path codec: expected Path value"/utf8>>}
end.
-file("src/postgleam/codec/path.gleam", 65).
-spec decode_points(bitstring(), integer(), list({float(), float()})) -> {ok,
list({float(), float()})} |
{error, binary()}.
decode_points(Data, Count, Acc) ->
case Count of
0 ->
{ok, lists:reverse(Acc)};
_ ->
case Data of
<<X:64/float-big, Y:64/float-big, Rest/bitstring>> ->
decode_points(Rest, Count - 1, [{X, Y} | Acc]);
_ ->
{error, <<"path codec: insufficient point data"/utf8>>}
end
end.
-file("src/postgleam/codec/path.gleam", 49).
-spec decode(bitstring()) -> {ok, postgleam@value:value()} | {error, binary()}.
decode(Data) ->
case Data of
<<Closed_byte, Npts:32/big, Rest/bitstring>> ->
Closed = case Closed_byte of
1 ->
true;
_ ->
false
end,
case decode_points(Rest, Npts, []) of
{ok, Points} ->
{ok, {path, Closed, Points}};
{error, E} ->
{error, E}
end;
_ ->
{error, <<"path codec: invalid header"/utf8>>}
end.
-file("src/postgleam/codec/path.gleam", 21).
-spec build(integer()) -> postgleam@codec:codec().
build(Type_oid) ->
{codec, <<"path"/utf8>>, Type_oid, binary, fun encode/1, fun decode/1}.
-file("src/postgleam/codec/path.gleam", 11).
-spec matcher() -> postgleam@codec:codec_matcher().
matcher() ->
{codec_matcher, <<"path"/utf8>>, [602], none, binary, fun build/1}.