Current section

Files

Jump to
gladvent src parse.erl
Raw

src/parse.erl

-module(parse).
-compile(no_auto_import).
-export([int/1, valid_int/3, day/1, days/1]).
-spec int(binary()) -> {ok, integer()} | {error, snag:snag()}.
int(S) ->
_pipe = S,
_pipe@1 = gleam@int:parse(_pipe),
gleam@result:replace_error(
_pipe@1,
begin
_pipe@2 = [<<"failed to parse \""/utf8>>, S, <<"\" as int"/utf8>>],
_pipe@3 = gleam@string:concat(_pipe@2),
snag:new(_pipe@3)
end
).
-spec valid_int(binary(), fun((integer()) -> boolean()), binary()) -> {ok,
integer()} |
{error, snag:snag()}.
valid_int(S, Is_valid, Invalid_msg) ->
case int(S) of
{error, _try} -> {error, _try};
{ok, I} ->
case Is_valid(I) of
false ->
{error, snag:new(Invalid_msg)};
true ->
{ok, I}
end
end.
-spec greater_than_0(integer()) -> boolean().
greater_than_0(I) ->
I > 0.
-spec less_than_26(integer()) -> boolean().
less_than_26(I) ->
I < 26.
-spec valid_day(integer()) -> boolean().
valid_day(I) ->
greater_than_0(I) andalso less_than_26(I).
-spec day(binary()) -> {ok, integer()} | {error, snag:snag()}.
day(S) ->
_pipe = valid_int(
S,
fun valid_day/1,
<<"day must be an integer from 1 to 25"/utf8>>
),
snag:context(
_pipe,
gleam@string:concat(
[<<"invalid day value "/utf8>>, <<"'"/utf8>>, S, <<"'"/utf8>>]
)
).
-spec days(list(binary())) -> {ok, list(integer())} | {error, snag:snag()}.
days(L) ->
case L of
[] ->
{error, snag:new(<<"no days selected"/utf8>>)};
_@1 ->
_pipe = L,
_pipe@1 = gleam@list:try_map(_pipe, fun day/1),
snag:context(
_pipe@1,
<<"could not map day values to integers"/utf8>>
)
end.