-module(postgleam@value). -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]). -define(FILEPATH, "src/postgleam/value.gleam"). -export([uuid_from_string/1, uuid_to_string/1]). -export_type([value/0]). -if(?OTP_RELEASE >= 27). -define(MODULEDOC(Str), -moduledoc(Str)). -define(DOC(Str), -doc(Str)). -else. -define(MODULEDOC(Str), -compile([])). -define(DOC(Str), -compile([])). -endif. -type value() :: null | {boolean, boolean()} | {integer, integer()} | {float, float()} | pos_infinity | neg_infinity | na_n | {text, binary()} | {bytea, bitstring()} | {uuid, bitstring()} | {oid, integer()} | void | {array, list(gleam@option:option(value()))} | {date, integer()} | {time, integer()} | {time_tz, integer(), integer()} | {timestamp, integer()} | {timestamptz, integer()} | {interval, integer(), integer(), integer()} | {json, binary()} | {jsonb, binary()} | {numeric, binary()} | {point, float(), float()} | {inet, integer(), bitstring(), integer()} | {macaddr, bitstring()}. -file("src/postgleam/value.gleam", 143). -spec hex_byte(integer()) -> {ok, integer()} | {error, nil}. hex_byte(B) -> case B of _ when (B >= 16#30) andalso (B =< 16#39) -> {ok, B - 16#30}; _ when (B >= 16#61) andalso (B =< 16#66) -> {ok, (B - 16#61) + 10}; _ when (B >= 16#41) andalso (B =< 16#46) -> {ok, (B - 16#41) + 10}; _ -> {error, nil} end. -file("src/postgleam/value.gleam", 124). -spec decode_hex_bytes(bitstring(), bitstring()) -> {ok, value()} | {error, nil}. decode_hex_bytes(Input, Acc) -> case Input of <<>> -> case erlang:byte_size(Acc) of 16 -> {ok, {uuid, Acc}}; _ -> {error, nil} end; <> -> case {hex_byte(Hi), hex_byte(Lo)} of {{ok, H}, {ok, L}} -> decode_hex_bytes(Rest, <>); {_, _} -> {error, nil} end; _ -> {error, nil} end. -file("src/postgleam/value.gleam", 65). ?DOC( " Parse a UUID string into a Uuid value.\n" " Accepts formats: \"550e8400-e29b-41d4-a716-446655440000\"\n" " or \"550e8400e29b41d4a716446655440000\" (with or without dashes).\n" ). -spec uuid_from_string(binary()) -> {ok, value()} | {error, nil}. uuid_from_string(S) -> Bytes = gleam_stdlib:identity(S), case Bytes of <> -> decode_hex_bytes( <>, <<>> ); _ -> case erlang:byte_size(Bytes) of 32 -> decode_hex_bytes(Bytes, <<>>); _ -> {error, nil} end end. -file("src/postgleam/value.gleam", 154). -spec byte_to_hex(integer()) -> binary(). byte_to_hex(B) -> Hi = gleam@string:slice(<<"0123456789abcdef"/utf8>>, B div 16, 1), Lo = gleam@string:slice(<<"0123456789abcdef"/utf8>>, B rem 16, 1), <>. -file("src/postgleam/value.gleam", 94). ?DOC( " Format a Uuid value as a hyphenated string.\n" " Returns \"550e8400-e29b-41d4-a716-446655440000\" format.\n" ). -spec uuid_to_string(value()) -> {ok, binary()} | {error, nil}. uuid_to_string(Val) -> case Val of {uuid, <>} -> {ok, <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<(byte_to_hex(A))/binary, (byte_to_hex( B ))/binary>>/binary, (byte_to_hex( C ))/binary>>/binary, (byte_to_hex( D ))/binary>>/binary, "-"/utf8>>/binary, (byte_to_hex( E ))/binary>>/binary, (byte_to_hex( F ))/binary>>/binary, "-"/utf8>>/binary, (byte_to_hex(G))/binary>>/binary, (byte_to_hex(H))/binary>>/binary, "-"/utf8>>/binary, (byte_to_hex(I))/binary>>/binary, (byte_to_hex(J))/binary>>/binary, "-"/utf8>>/binary, (byte_to_hex(K))/binary>>/binary, (byte_to_hex(L))/binary>>/binary, (byte_to_hex(M))/binary>>/binary, (byte_to_hex(N))/binary>>/binary, (byte_to_hex(O))/binary>>/binary, (byte_to_hex(P))/binary>>}; _ -> {error, nil} end.