Current section
Files
Jump to
Current section
Files
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, replace_with_defect/2, replace_with_failure/2, map_error/2, map_defect/2, map_failure/2, tap/2, tap_error/2, tap_defect/2, tap_failure/2, context/2, unwrap_failure/2, extract_error/1, pretty_print/2, print_line/2]).
-export_type([severity/0, problem/1]).
-type severity() :: defect | failure.
-type problem(FWG) :: {problem, FWG, severity(), list(binary())}.
-spec new_problem(FWR, severity()) -> problem(FWR).
new_problem(Error, Severity) ->
{problem, Error, Severity, []}.
-spec new_defect(FWN) -> problem(FWN).
new_defect(Error) ->
new_problem(Error, defect).
-spec new_failure(FWP) -> problem(FWP).
new_failure(Error) ->
new_problem(Error, failure).
-spec error_with_defect(FWT) -> {ok, any()} | {error, problem(FWT)}.
error_with_defect(Defect) ->
{error, new_defect(Defect)}.
-spec error_with_failure(FWX) -> {ok, any()} | {error, problem(FWX)}.
error_with_failure(Failure) ->
{error, new_failure(Failure)}.
-spec into_defect({ok, FXB} | {error, FXC}) -> {ok, FXB} | {error, problem(FXC)}.
into_defect(Result) ->
gleam@result:map_error(Result, fun new_defect/1).
-spec into_failure({ok, FXH} | {error, FXI}) -> {ok, FXH} |
{error, problem(FXI)}.
into_failure(Result) ->
gleam@result:map_error(Result, fun new_failure/1).
-spec map_into_defect({ok, FXN} | {error, FXO}, fun((FXO) -> FXR)) -> {ok, FXN} |
{error, problem(FXR)}.
map_into_defect(Result, Mapper) ->
_pipe = Result,
_pipe@1 = gleam@result:map_error(_pipe, Mapper),
into_defect(_pipe@1).
-spec map_into_failure({ok, FXU} | {error, FXV}, fun((FXV) -> FXY)) -> {ok, FXU} |
{error, problem(FXY)}.
map_into_failure(Result, Mapper) ->
_pipe = Result,
_pipe@1 = gleam@result:map_error(_pipe, Mapper),
into_failure(_pipe@1).
-spec replace_with_defect({ok, FYB} | {error, any()}, FYF) -> {ok, FYB} |
{error, problem(FYF)}.
replace_with_defect(Result, E) ->
gleam@result:replace_error(Result, new_defect(E)).
-spec replace_with_failure({ok, FYI} | {error, any()}, FYM) -> {ok, FYI} |
{error, problem(FYM)}.
replace_with_failure(Result, E) ->
gleam@result:replace_error(Result, new_failure(E)).
-spec map_error_in_problem(problem(FYP), fun((FYP) -> FYP)) -> problem(FYP).
map_error_in_problem(Problem, Mapper) ->
erlang:setelement(2, Problem, Mapper(erlang:element(2, Problem))).
-spec map_defect_in_problem(problem(FYS), fun((FYS) -> FYS)) -> problem(FYS).
map_defect_in_problem(Problem, Mapper) ->
case erlang:element(3, Problem) of
defect ->
erlang:setelement(2, Problem, Mapper(erlang:element(2, Problem)));
_ ->
Problem
end.
-spec map_failure_in_problem(problem(FYV), fun((FYV) -> FYV)) -> problem(FYV).
map_failure_in_problem(Problem, Mapper) ->
case erlang:element(3, Problem) of
failure ->
erlang:setelement(2, Problem, Mapper(erlang:element(2, Problem)));
_ ->
Problem
end.
-spec map_error({ok, FYY} | {error, problem(FYZ)}, fun((FYZ) -> FYZ)) -> {ok,
FYY} |
{error, problem(FYZ)}.
map_error(Outcome, Mapper) ->
gleam@result:map_error(
Outcome,
fun(_capture) -> map_error_in_problem(_capture, Mapper) end
).
-spec map_defect({ok, FZE} | {error, problem(FZF)}, fun((FZF) -> FZF)) -> {ok,
FZE} |
{error, problem(FZF)}.
map_defect(Outcome, Mapper) ->
gleam@result:map_error(
Outcome,
fun(_capture) -> map_defect_in_problem(_capture, Mapper) end
).
-spec map_failure({ok, FZJ} | {error, problem(FZK)}, fun((FZK) -> FZK)) -> {ok,
FZJ} |
{error, problem(FZK)}.
map_failure(Outcome, Mapper) ->
gleam@result:map_error(
Outcome,
fun(_capture) -> map_failure_in_problem(_capture, Mapper) end
).
-spec tap_error_in_problem(problem(FZO), fun((FZO) -> any())) -> problem(FZO).
tap_error_in_problem(Problem, Fun) ->
Fun(erlang:element(2, Problem)),
Problem.
-spec tap_defect_in_problem(problem(FZS), fun((FZS) -> any())) -> problem(FZS).
tap_defect_in_problem(Problem, Fun) ->
case erlang:element(3, Problem) of
defect ->
Fun(erlang:element(2, Problem)),
Problem;
_ ->
Problem
end.
-spec tap_failure_in_problem(problem(FZW), fun((FZW) -> any())) -> problem(FZW).
tap_failure_in_problem(Problem, Fun) ->
case erlang:element(3, Problem) of
failure ->
Fun(erlang:element(2, Problem)),
Problem;
_ ->
Problem
end.
-spec tap({ok, GAA} | {error, problem(GAB)}, fun((problem(GAB)) -> any())) -> {ok,
GAA} |
{error, problem(GAB)}.
tap(Outcome, Fun) ->
gleam@result:map_error(
Outcome,
fun(Problem) ->
Fun(Problem),
Problem
end
).
-spec tap_error({ok, GAI} | {error, problem(GAJ)}, fun((GAJ) -> any())) -> {ok,
GAI} |
{error, problem(GAJ)}.
tap_error(Outcome, Fun) ->
gleam@result:map_error(
Outcome,
fun(_capture) -> tap_error_in_problem(_capture, Fun) end
).
-spec tap_defect({ok, GAP} | {error, problem(GAQ)}, fun((GAQ) -> any())) -> {ok,
GAP} |
{error, problem(GAQ)}.
tap_defect(Outcome, Fun) ->
gleam@result:map_error(
Outcome,
fun(_capture) -> tap_defect_in_problem(_capture, Fun) end
).
-spec tap_failure({ok, GAW} | {error, problem(GAX)}, fun((GAX) -> any())) -> {ok,
GAW} |
{error, problem(GAX)}.
tap_failure(Outcome, Fun) ->
gleam@result:map_error(
Outcome,
fun(_capture) -> tap_failure_in_problem(_capture, Fun) end
).
-spec push_to_stack(list(binary()), binary()) -> list(binary()).
push_to_stack(Stack, Entry) ->
[Entry | Stack].
-spec add_context_to_problem(problem(GBK), binary()) -> problem(GBK).
add_context_to_problem(Problem, Value) ->
erlang:setelement(
4,
Problem,
push_to_stack(erlang:element(4, Problem), Value)
).
-spec context({ok, GBD} | {error, problem(GBE)}, binary()) -> {ok, GBD} |
{error, problem(GBE)}.
context(Outcome, Context) ->
gleam@result:map_error(
Outcome,
fun(_capture) -> add_context_to_problem(_capture, Context) end
).
-spec unwrap_failure(problem(GBN), GBN) -> GBN.
unwrap_failure(Problem, Default_value) ->
case erlang:element(3, Problem) of
defect ->
Default_value;
failure ->
erlang:element(2, Problem)
end.
-spec problem_to_error(problem(GBV)) -> GBV.
problem_to_error(Problem) ->
erlang:element(2, Problem).
-spec extract_error({ok, GBP} | {error, problem(GBQ)}) -> {ok, GBP} |
{error, GBQ}.
extract_error(Outcome) ->
_pipe = Outcome,
gleam@result:map_error(_pipe, fun problem_to_error/1).
-spec long_suffix(severity()) -> binary().
long_suffix(Severity) ->
case Severity of
defect ->
<<"Defect: "/utf8>>;
failure ->
<<"Failure: "/utf8>>
end.
-spec prettry_print_problem_error(severity(), binary()) -> binary().
prettry_print_problem_error(Severity, Error) ->
<<(long_suffix(Severity))/binary, Error/binary>>.
-spec pretty_print_stack_entry(binary()) -> binary().
pretty_print_stack_entry(Value) ->
Value.
-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(GCC),
binary(),
binary(),
fun((GCC) -> binary())
) -> binary().
pretty_print_with_joins(Problem, Join_current, Join_stack, To_s) ->
Current = prettry_print_problem_error(
erlang:element(3, Problem),
To_s(erlang:element(2, Problem))
),
Stack = <<Join_current/binary,
(begin
_pipe = erlang:element(4, Problem),
_pipe@1 = stack_to_lines(_pipe),
gleam@string:join(_pipe@1, Join_stack)
end)/binary>>,
Stack@1 = case erlang:element(4, Problem) of
[] ->
<<""/utf8>>;
_ ->
Stack
end,
<<Current/binary, Stack@1/binary>>.
-spec pretty_print(problem(GBY), fun((GBY) -> binary())) -> binary().
pretty_print(Problem, To_s) ->
pretty_print_with_joins(
Problem,
<<"\n\nstack:\n "/utf8>>,
<<"\n "/utf8>>,
To_s
).
-spec print_line(problem(GCA), fun((GCA) -> binary())) -> binary().
print_line(Problem, To_s) ->
pretty_print_with_joins(Problem, <<" << "/utf8>>, <<" < "/utf8>>, To_s).