Current section
Files
Jump to
Current section
Files
src/parrot@internal@decoder.erl
-module(parrot@internal@decoder).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-define(FILEPATH, "src/parrot/internal/decoder.gleam").
-export([datetime_string_decoder/0, date_decoder/0, datetime_tuple_decoder/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.
?MODULEDOC(false).
-file("src/parrot/internal/decoder.gleam", 6).
?DOC(false).
-spec datetime_string_decoder() -> gleam@dynamic@decode:decoder(gleam@time@timestamp:timestamp()).
datetime_string_decoder() ->
_pipe = {decoder, fun gleam@dynamic@decode:decode_string/1},
gleam@dynamic@decode:then(
_pipe,
fun(Datetime_str) ->
case gleam@time@timestamp:parse_rfc3339(Datetime_str) of
{ok, Ts} ->
gleam@dynamic@decode:success(Ts);
{error, _} ->
gleam@dynamic@decode:failure(
gleam@time@timestamp:from_unix_seconds(0),
<<"Invalid datetime format"/utf8>>
)
end
end
).
-file("src/parrot/internal/decoder.gleam", 28).
?DOC(false).
-spec date_decoder() -> gleam@dynamic@decode:decoder(gleam@time@calendar:date()).
date_decoder() ->
gleam@dynamic@decode:field(
0,
{decoder, fun gleam@dynamic@decode:decode_int/1},
fun(Year) ->
gleam@dynamic@decode:field(
1,
begin
_pipe = {decoder, fun gleam@dynamic@decode:decode_int/1},
gleam@dynamic@decode:then(
_pipe,
fun(Month) ->
case gleam@time@calendar:month_from_int(Month) of
{error, _} ->
gleam@dynamic@decode:failure(
january,
<<"Month"/utf8>>
);
{ok, Month@1} ->
gleam@dynamic@decode:success(Month@1)
end
end
)
end,
fun(Month@2) ->
gleam@dynamic@decode:field(
2,
{decoder, fun gleam@dynamic@decode:decode_int/1},
fun(Day) ->
gleam@dynamic@decode:success(
{date, Year, Month@2, Day}
)
end
)
end
)
end
).
-file("src/parrot/internal/decoder.gleam", 54).
?DOC(false).
-spec seconds_decoder() -> gleam@dynamic@decode:decoder({integer(), integer()}).
seconds_decoder() ->
Int = begin
_pipe = {decoder, fun gleam@dynamic@decode:decode_int/1},
gleam@dynamic@decode:map(_pipe, fun(I) -> {I, 0} end)
end,
Float = begin
_pipe@1 = {decoder, fun gleam@dynamic@decode:decode_float/1},
gleam@dynamic@decode:map(
_pipe@1,
fun(F) ->
Floored = math:floor(F),
Seconds = erlang:round(Floored),
Nanoseconds = erlang:round((F - Floored) * 1000000000.0),
{Seconds, Nanoseconds}
end
)
end,
gleam@dynamic@decode:one_of(Int, [Float]).
-file("src/parrot/internal/decoder.gleam", 45).
?DOC(false).
-spec time_decoder() -> gleam@dynamic@decode:decoder(gleam@time@calendar:time_of_day()).
time_decoder() ->
gleam@dynamic@decode:field(
0,
{decoder, fun gleam@dynamic@decode:decode_int/1},
fun(Hours) ->
gleam@dynamic@decode:field(
1,
{decoder, fun gleam@dynamic@decode:decode_int/1},
fun(Minutes) ->
gleam@dynamic@decode:field(
2,
seconds_decoder(),
fun(_use0) ->
{Seconds, Nanoseconds} = _use0,
_pipe = {time_of_day,
Hours,
Minutes,
Seconds,
Nanoseconds},
gleam@dynamic@decode:success(_pipe)
end
)
end
)
end
).
-file("src/parrot/internal/decoder.gleam", 20).
?DOC(false).
-spec datetime_tuple_decoder() -> gleam@dynamic@decode:decoder(gleam@time@timestamp:timestamp()).
datetime_tuple_decoder() ->
gleam@dynamic@decode:field(
0,
date_decoder(),
fun(Date) ->
gleam@dynamic@decode:field(
1,
time_decoder(),
fun(Time) ->
_pipe = gleam@time@timestamp:from_calendar(
Date,
Time,
{duration, 0, 0}
),
gleam@dynamic@decode:success(_pipe)
end
)
end
).