-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, to_defect/1, to_failure/1, with_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(), severity(), list(binary())}. -spec new_problem(FWR, severity()) -> problem(FWR). new_problem(Error, Severity) -> {problem, Error, Severity, 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_error({ok, FYS} | {error, problem(FYT)}, fun((FYT) -> FYT)) -> {ok, FYS} | {error, problem(FYT)}. map_error(Outcome, Mapper) -> gleam@result:map_error( Outcome, fun(_capture) -> map_error_in_problem(_capture, Mapper) end ). -spec problem_to_defect(problem(FZK)) -> problem(FZK). problem_to_defect(Problem) -> erlang:setelement(3, Problem, defect). -spec to_defect({ok, FZE} | {error, problem(FZF)}) -> {ok, FZE} | {error, problem(FZF)}. to_defect(Outcome) -> gleam@result:map_error(Outcome, fun problem_to_defect/1). -spec problem_to_failure(problem(FZT)) -> problem(FZT). problem_to_failure(Problem) -> erlang:setelement(3, Problem, failure). -spec to_failure({ok, FZN} | {error, problem(FZO)}) -> {ok, FZN} | {error, problem(FZO)}. to_failure(Outcome) -> gleam@result:map_error(Outcome, fun problem_to_failure/1). -spec push_to_stack(list(binary()), binary()) -> list(binary()). push_to_stack(Stack, Entry) -> [Entry | Stack]. -spec add_context_to_problem(problem(FZX), binary()) -> problem(FZX). add_context_to_problem(Problem, Value) -> erlang:setelement( 5, Problem, push_to_stack(erlang:element(5, Problem), Value) ). -spec with_context({ok, FYY} | {error, problem(FYZ)}, binary()) -> {ok, FYY} | {error, problem(FYZ)}. with_context(Outcome, Context) -> gleam@result:map_error( Outcome, fun(_capture) -> add_context_to_problem(_capture, Context) end ). -spec unwrap_failure(problem(GAA), GAA) -> GAA. 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(GAI)) -> GAI. problem_to_error(Problem) -> erlang:element(2, Problem). -spec extract_error({ok, GAC} | {error, problem(GAD)}) -> {ok, GAC} | {error, GAD}. 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(GAP), binary(), binary(), fun((GAP) -> 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 = <>, Stack@1 = case erlang:element(5, Problem) of [] -> <<""/utf8>>; _ -> Stack end, <>. -spec pretty_print(problem(GAL), fun((GAL) -> binary())) -> binary(). pretty_print(Problem, To_s) -> pretty_print_with_joins( Problem, <<"\n\nstack:\n "/utf8>>, <<"\n "/utf8>>, To_s ). -spec print_line(problem(GAN), fun((GAN) -> binary())) -> binary(). print_line(Problem, To_s) -> pretty_print_with_joins(Problem, <<" << "/utf8>>, <<" < "/utf8>>, To_s).