-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_value/2, to_defect/1, to_failure/1, with_context/2, unwrap_failure/2, pretty_print/2, print_line/2]). -export_type([error/1, problem/1]). -type error(FWG) :: {defect, FWG} | {failure, FWG}. -type problem(FWH) :: {problem, error(FWH), list(binary()), error(FWH)}. -spec new_defect(FWO) -> problem(FWO). new_defect(Value) -> {problem, {defect, Value}, [], {defect, Value}}. -spec new_failure(FWQ) -> problem(FWQ). new_failure(Value) -> {problem, {failure, Value}, [], {failure, Value}}. -spec error_with_defect(FWS) -> {ok, any()} | {error, problem(FWS)}. error_with_defect(Defect) -> {error, new_defect(Defect)}. -spec error_with_failure(FWW) -> {ok, any()} | {error, problem(FWW)}. error_with_failure(Failure) -> {error, new_failure(Failure)}. -spec into_defect({ok, FXA} | {error, FXB}) -> {ok, FXA} | {error, problem(FXB)}. into_defect(Result) -> gleam@result:map_error(Result, fun new_defect/1). -spec into_failure({ok, FXG} | {error, FXH}) -> {ok, FXG} | {error, problem(FXH)}. into_failure(Result) -> gleam@result:map_error(Result, fun new_failure/1). -spec map_into_defect({ok, FXM} | {error, FXN}, fun((FXN) -> FXQ)) -> {ok, FXM} | {error, problem(FXQ)}. map_into_defect(Result, Mapper) -> _pipe = Result, _pipe@1 = gleam@result:map_error(_pipe, Mapper), into_defect(_pipe@1). -spec map_into_failure({ok, FXT} | {error, FXU}, fun((FXU) -> FXX)) -> {ok, FXT} | {error, problem(FXX)}. map_into_failure(Result, Mapper) -> _pipe = Result, _pipe@1 = gleam@result:map_error(_pipe, Mapper), into_failure(_pipe@1). -spec as_defect({ok, FYA} | {error, nil}, FYD) -> {ok, FYA} | {error, problem(FYD)}. as_defect(Result, E) -> gleam@result:replace_error(Result, new_defect(E)). -spec as_failure({ok, FYG} | {error, nil}, FYJ) -> {ok, FYG} | {error, problem(FYJ)}. as_failure(Result, E) -> gleam@result:replace_error(Result, new_failure(E)). -spec map_current_error_in_problem( problem(FYM), fun((error(FYM)) -> error(FYM)) ) -> problem(FYM). map_current_error_in_problem(Problem, Mapper) -> erlang:setelement(2, Problem, Mapper(erlang:element(2, Problem))). -spec map_current_error( {ok, FYR} | {error, problem(FYS)}, fun((error(FYS)) -> error(FYS)) ) -> {ok, FYR} | {error, problem(FYS)}. map_current_error(Outcome, Mapper) -> gleam@result:map_error( Outcome, fun(_capture) -> map_current_error_in_problem(_capture, Mapper) end ). -spec map_current_value_in_problem(problem(FYY), fun((FYY) -> FYY)) -> problem(FYY). map_current_value_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_value({ok, FZB} | {error, problem(FZC)}, fun((FZC) -> FZC)) -> {ok, FZB} | {error, problem(FZC)}. map_value(Outcome, Mapper) -> gleam@result:map_error( Outcome, fun(_capture) -> map_current_value_in_problem(_capture, Mapper) end ). -spec to_defect({ok, FZN} | {error, problem(FZO)}) -> {ok, FZN} | {error, problem(FZO)}. to_defect(Outcome) -> map_current_error( Outcome, fun(Error) -> {defect, erlang:element(2, Error)} end ). -spec to_failure({ok, FZT} | {error, problem(FZU)}) -> {ok, FZT} | {error, problem(FZU)}. 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(GAA), binary()) -> problem(GAA). add_context_to_problem(Problem, Value) -> erlang:setelement( 3, Problem, push_to_stack(erlang:element(3, Problem), Value) ). -spec with_context({ok, FZH} | {error, problem(FZI)}, binary()) -> {ok, FZH} | {error, problem(FZI)}. with_context(Outcome, Context) -> gleam@result:map_error( Outcome, fun(_capture) -> add_context_to_problem(_capture, Context) end ). -spec unwrap_failure(problem(GAD), GAD) -> GAD. unwrap_failure(Problem, Default_value) -> case erlang:element(2, Problem) of {defect, _} -> Default_value; {failure, Value} -> Value end. -spec long_suffix(error(any())) -> binary(). long_suffix(Error) -> case Error of {defect, _} -> <<"Defect: "/utf8>>; {failure, _} -> <<"Failure: "/utf8>> end. -spec short_suffix(error(any())) -> binary(). short_suffix(Error) -> case Error of {defect, _} -> <<"d: "/utf8>>; {failure, _} -> <<"f: "/utf8>> end. -spec prettry_print_error(binary(), error(GAU), fun((GAU) -> binary())) -> binary(). prettry_print_error(Suffix, Error, To_s) -> <>. -spec prettry_print_problem_error(problem(GAM), fun((GAM) -> binary())) -> binary(). prettry_print_problem_error(Problem, To_s) -> prettry_print_error( long_suffix(erlang:element(2, Problem)), erlang:element(2, Problem), To_s ). -spec prettry_print_problem_original(problem(GAQ), fun((GAQ) -> binary())) -> binary(). prettry_print_problem_original(Problem, To_s) -> prettry_print_error( short_suffix(erlang:element(2, Problem)), erlang:element(4, Problem), To_s ). -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(GAK), binary(), binary(), fun((GAK) -> binary()) ) -> binary(). pretty_print_with_joins(Problem, Join_current, Join_stack, To_s) -> Current = prettry_print_problem_error(Problem, To_s), 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, To_s), <<<<<<<>/binary, Stack/binary>>/binary, Join_stack/binary>>/binary, Original/binary>>. -spec pretty_print(problem(GAG), fun((GAG) -> binary())) -> binary(). pretty_print(Problem, To_s) -> pretty_print_with_joins( Problem, <<"\n\nstack:\n "/utf8>>, <<"\n "/utf8>>, To_s ). -spec print_line(problem(GAI), fun((GAI) -> binary())) -> binary(). print_line(Problem, To_s) -> pretty_print_with_joins(Problem, <<" << "/utf8>>, <<" < "/utf8>>, To_s).