Current section

Files

Jump to
outcome src outcome.erl
Raw

src/outcome.erl

-module(outcome).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([error_with_defect/1, error_with_failure/1, into_defect/1, into_failure/1, map_into_defect/2, map_into_failure/2, as_defect/2, as_failure/2, map_message/2, to_defect/1, to_failure/1, with_context/2, unwrap_failure/2, pretty_print/1, print_line/1]).
-export_type([error/0, problem/0]).
-type error() :: {defect, binary()} | {failure, binary()}.
-type problem() :: {problem, error(), list(binary()), error()}.
-spec new_defect(binary()) -> problem().
new_defect(Message) ->
{problem, {defect, Message}, [], {defect, Message}}.
-spec new_failure(binary()) -> problem().
new_failure(Message) ->
{problem, {failure, Message}, [], {failure, Message}}.
-spec error_with_defect(binary()) -> {ok, any()} | {error, problem()}.
error_with_defect(Defect) ->
{error, new_defect(Defect)}.
-spec error_with_failure(binary()) -> {ok, any()} | {error, problem()}.
error_with_failure(Failure) ->
{error, new_failure(Failure)}.
-spec into_defect({ok, FWO} | {error, binary()}) -> {ok, FWO} |
{error, problem()}.
into_defect(Result) ->
gleam@result:map_error(Result, fun new_defect/1).
-spec into_failure({ok, FWS} | {error, binary()}) -> {ok, FWS} |
{error, problem()}.
into_failure(Result) ->
gleam@result:map_error(Result, fun new_failure/1).
-spec map_into_defect({ok, FWW} | {error, FWX}, fun((FWX) -> binary())) -> {ok,
FWW} |
{error, problem()}.
map_into_defect(Result, Mapper) ->
_pipe = Result,
_pipe@1 = gleam@result:map_error(_pipe, Mapper),
into_defect(_pipe@1).
-spec map_into_failure({ok, FXB} | {error, FXC}, fun((FXC) -> binary())) -> {ok,
FXB} |
{error, problem()}.
map_into_failure(Result, Mapper) ->
_pipe = Result,
_pipe@1 = gleam@result:map_error(_pipe, Mapper),
into_failure(_pipe@1).
-spec as_defect({ok, FXG} | {error, nil}, binary()) -> {ok, FXG} |
{error, problem()}.
as_defect(Result, E) ->
gleam@result:replace_error(Result, new_defect(E)).
-spec as_failure({ok, FXK} | {error, nil}, binary()) -> {ok, FXK} |
{error, problem()}.
as_failure(Result, E) ->
gleam@result:replace_error(Result, new_failure(E)).
-spec map_current_error_in_problem(problem(), fun((error()) -> error())) -> problem().
map_current_error_in_problem(Problem, Mapper) ->
erlang:setelement(2, Problem, Mapper(erlang:element(2, Problem))).
-spec map_current_error(
{ok, FXO} | {error, problem()},
fun((error()) -> error())
) -> {ok, FXO} | {error, problem()}.
map_current_error(Outcome, Mapper) ->
gleam@result:map_error(
Outcome,
fun(_capture) -> map_current_error_in_problem(_capture, Mapper) end
).
-spec map_current_message_in_problem(problem(), fun((binary()) -> binary())) -> problem().
map_current_message_in_problem(Problem, Mapper) ->
map_current_error_in_problem(Problem, fun(Error) -> case Error of
{defect, Message} ->
{defect, Mapper(Message)};
{failure, Message@1} ->
{failure, Mapper(Message@1)}
end end).
-spec map_message({ok, FXR} | {error, problem()}, fun((binary()) -> binary())) -> {ok,
FXR} |
{error, problem()}.
map_message(Outcome, Mapper) ->
gleam@result:map_error(
Outcome,
fun(_capture) -> map_current_message_in_problem(_capture, Mapper) end
).
-spec to_defect({ok, FXX} | {error, problem()}) -> {ok, FXX} |
{error, problem()}.
to_defect(Outcome) ->
map_current_error(
Outcome,
fun(Error) -> {defect, erlang:element(2, Error)} end
).
-spec to_failure({ok, FYA} | {error, problem()}) -> {ok, FYA} |
{error, problem()}.
to_failure(Outcome) ->
map_current_error(
Outcome,
fun(Error) -> {failure, erlang:element(2, Error)} end
).
-spec push_to_stack(list(binary()), binary()) -> list(binary()).
push_to_stack(Stack, Entry) ->
[Entry | Stack].
-spec add_context_to_problem(problem(), binary()) -> problem().
add_context_to_problem(Problem, Value) ->
erlang:setelement(
3,
Problem,
push_to_stack(erlang:element(3, Problem), Value)
).
-spec with_context({ok, FXU} | {error, problem()}, binary()) -> {ok, FXU} |
{error, problem()}.
with_context(Outcome, Context) ->
gleam@result:map_error(
Outcome,
fun(_capture) -> add_context_to_problem(_capture, Context) end
).
-spec unwrap_failure(problem(), binary()) -> binary().
unwrap_failure(Problem, Default_message) ->
case erlang:element(2, Problem) of
{defect, _} ->
Default_message;
{failure, Message} ->
Message
end.
-spec long_suffix(error()) -> binary().
long_suffix(Error) ->
case Error of
{defect, _} ->
<<"Defect: "/utf8>>;
{failure, _} ->
<<"Failure: "/utf8>>
end.
-spec short_suffix(error()) -> binary().
short_suffix(Error) ->
case Error of
{defect, _} ->
<<"d: "/utf8>>;
{failure, _} ->
<<"f: "/utf8>>
end.
-spec prettry_print_error(binary(), error()) -> binary().
prettry_print_error(Suffix, Error) ->
<<Suffix/binary, (erlang:element(2, Error))/binary>>.
-spec prettry_print_problem_error(problem()) -> binary().
prettry_print_problem_error(Problem) ->
prettry_print_error(
long_suffix(erlang:element(2, Problem)),
erlang:element(2, Problem)
).
-spec prettry_print_problem_original(problem()) -> binary().
prettry_print_problem_original(Problem) ->
prettry_print_error(
short_suffix(erlang:element(2, Problem)),
erlang:element(4, Problem)
).
-spec pretty_print_stack_entry(binary()) -> binary().
pretty_print_stack_entry(Value) ->
<<"c: "/utf8, Value/binary>>.
-spec stack_to_lines(list(binary())) -> list(binary()).
stack_to_lines(Stack) ->
_pipe = Stack,
gleam@list:map(_pipe, fun pretty_print_stack_entry/1).
-spec pretty_print_with_joins(problem(), binary(), binary()) -> binary().
pretty_print_with_joins(Problem, Join_current, Join_stack) ->
Current = prettry_print_problem_error(Problem),
Stack = begin
_pipe = erlang:element(3, Problem),
_pipe@1 = stack_to_lines(_pipe),
gleam@string:join(_pipe@1, Join_stack)
end,
Original = prettry_print_problem_original(Problem),
<<<<<<<<Current/binary, Join_current/binary>>/binary, Stack/binary>>/binary,
Join_stack/binary>>/binary,
Original/binary>>.
-spec pretty_print(problem()) -> binary().
pretty_print(Problem) ->
pretty_print_with_joins(Problem, <<"\n\nstack:\n "/utf8>>, <<"\n "/utf8>>).
-spec print_line(problem()) -> binary().
print_line(Problem) ->
pretty_print_with_joins(Problem, <<" << "/utf8>>, <<" < "/utf8>>).