Current section
Files
Jump to
Current section
Files
src/parse.erl
-module(parse).
-compile(no_auto_import).
-export([int/1, day/1, days/1, replace_error/2]).
-spec int(binary()) -> {ok, integer()} | {error, snag:snag()}.
int(S) ->
_pipe = S,
_pipe@1 = gleam@int:parse(_pipe),
replace_error(
_pipe@1,
begin
_pipe@2 = [<<"failed to parse \""/utf8>>, S, <<"\" as int"/utf8>>],
gleam@string:concat(_pipe@2)
end
).
-spec day(binary()) -> {ok, integer()} | {error, snag:snag()}.
day(S) ->
case int(S) of
{error, _try} -> {error, _try};
{ok, I} ->
case (I > 0) andalso (I < 26) of
true ->
{ok, I};
false ->
_pipe = [<<"invalid day value "/utf8>>,
<<"'"/utf8>>,
S,
<<"'"/utf8>>],
_pipe@1 = gleam@string:concat(_pipe),
_pipe@2 = snag:error(_pipe@1),
snag:context(
_pipe@2,
<<"day must be an integer from 1 to 25"/utf8>>
)
end
end.
-spec days(list(binary())) -> {ok, list(integer())} | {error, snag:snag()}.
days(L) ->
case L of
[] ->
snag:error(<<"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.
-spec replace_error({ok, GUB} | {error, any()}, binary()) -> {ok, GUB} |
{error, snag:snag()}.
replace_error(R, S) ->
gleam@result:replace_error(R, snag:new(S)).