Packages

A package to deal with remote data in Gleam

Current section

Files

Jump to
remote_data src remote_data.erl
Raw

src/remote_data.erl

-module(remote_data).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([map/2, map2/3, map3/4, map_error/2, 'try'/2, unwrap/2, to_option/1, from_option/2, to_result/2, from_result/1, from_list/1, is_not_asked/1, is_loading/1, is_failure/1, is_success/1]).
-export_type([remote_data/2]).
-type remote_data(FKG, FKH) :: not_asked |
loading |
{failure, FKH} |
{success, FKG}.
-spec map(remote_data(FKI, FKJ), fun((FKI) -> FKM)) -> remote_data(FKM, FKJ).
map(Data, Mapper) ->
case Data of
not_asked ->
not_asked;
loading ->
loading;
{failure, Error} ->
{failure, Error};
{success, A} ->
{success, Mapper(A)}
end.
-spec map2(remote_data(FKP, FKQ), remote_data(FKT, FKQ), fun((FKP, FKT) -> FKW)) -> remote_data(FKW, FKQ).
map2(Data1, Data2, Mapper) ->
case {Data1, Data2} of
{{success, A}, {success, B}} ->
{success, Mapper(A, B)};
{{failure, Error}, _} ->
{failure, Error};
{_, {failure, Error}} ->
{failure, Error};
{not_asked, _} ->
not_asked;
{_, not_asked} ->
not_asked;
{loading, _} ->
loading;
{_, loading} ->
loading
end.
-spec map3(
remote_data(FKZ, FLA),
remote_data(FLD, FLA),
remote_data(FLG, FLA),
fun((FKZ, FLD, FLG) -> FLJ)
) -> remote_data(FLJ, FLA).
map3(Data1, Data2, Data3, Mapper) ->
case {Data1, Data2, Data3} of
{{success, A}, {success, B}, {success, C}} ->
{success, Mapper(A, B, C)};
{{failure, Error}, _, _} ->
{failure, Error};
{_, {failure, Error}, _} ->
{failure, Error};
{_, _, {failure, Error}} ->
{failure, Error};
{not_asked, _, _} ->
not_asked;
{_, not_asked, _} ->
not_asked;
{_, _, not_asked} ->
not_asked;
{loading, _, _} ->
loading;
{_, loading, _} ->
loading;
{_, _, loading} ->
loading
end.
-spec map_error(remote_data(FLM, FLN), fun((FLN) -> FLQ)) -> remote_data(FLM, FLQ).
map_error(Data, Mapper) ->
case Data of
not_asked ->
not_asked;
loading ->
loading;
{failure, Error} ->
{failure, Mapper(Error)};
{success, A} ->
{success, A}
end.
-spec 'try'(remote_data(FLT, FLU), fun((FLT) -> remote_data(FLX, FLU))) -> remote_data(FLX, FLU).
'try'(Data, Mapper) ->
case Data of
not_asked ->
not_asked;
loading ->
loading;
{failure, Error} ->
{failure, Error};
{success, A} ->
Mapper(A)
end.
-spec unwrap(remote_data(FMC, any()), FMC) -> FMC.
unwrap(Data, Default) ->
case Data of
{success, A} ->
A;
_ ->
Default
end.
-spec to_option(remote_data(FMG, any())) -> gleam@option:option(FMG).
to_option(Data) ->
case Data of
{success, A} ->
{some, A};
_ ->
none
end.
-spec from_option(gleam@option:option(FML), FMN) -> remote_data(FML, FMN).
from_option(Option, Error) ->
case Option of
{some, A} ->
{success, A};
none ->
{failure, Error}
end.
-spec to_result(remote_data(FMQ, FMR), FMR) -> {ok, FMQ} | {error, FMR}.
to_result(Data, Error) ->
case Data of
{success, A} ->
{ok, A};
{failure, Error@1} ->
{error, Error@1};
_ ->
{error, Error}
end.
-spec from_result({ok, FMW} | {error, FMX}) -> remote_data(FMW, FMX).
from_result(Result) ->
case Result of
{ok, A} ->
{success, A};
{error, Error} ->
{failure, Error}
end.
-spec from_list(list(remote_data(FNC, FND))) -> remote_data(list(FNC), FND).
from_list(Data_list) ->
gleam@list:fold(
Data_list,
{success, []},
fun(Acc, Data) ->
map2(
Acc,
Data,
fun(Acc_value, Data_value) ->
gleam@list:append(Acc_value, [Data_value])
end
)
end
).
-spec is_not_asked(remote_data(any(), any())) -> boolean().
is_not_asked(Data) ->
case Data of
not_asked ->
true;
_ ->
false
end.
-spec is_loading(remote_data(any(), any())) -> boolean().
is_loading(Data) ->
case Data of
loading ->
true;
_ ->
false
end.
-spec is_failure(remote_data(any(), any())) -> boolean().
is_failure(Data) ->
case Data of
{failure, _} ->
true;
_ ->
false
end.
-spec is_success(remote_data(any(), any())) -> boolean().
is_success(Data) ->
case Data of
{success, _} ->
true;
_ ->
false
end.