-module(outcome). -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]). -export([new_defect/1, new_failure/1, result_to_defect/1, result_to_failure/1, map_error/2, tap/2, tap_error/2, tap_defect/2, tap_failure/2, context/2, get_failure_in_problem/2, to_simple_result/1, pretty_print/2, print_line/2]). -export_type([severity/0, problem/1]). -if(?OTP_RELEASE >= 27). -define(MODULEDOC(Str), -moduledoc(Str)). -define(DOC(Str), -doc(Str)). -else. -define(MODULEDOC(Str), -compile([])). -define(DOC(Str), -compile([])). -endif. -type severity() :: defect | failure. -type problem(FWT) :: {problem, FWT, severity(), list(binary())}. -file("src/outcome.gleam", 59). -spec new_problem(FXE, severity()) -> problem(FXE). new_problem(Error, Severity) -> {problem, Error, Severity, []}. -file("src/outcome.gleam", 41). ?DOC( " Create a Defect\n" " Use this if you need the `Problem` type only.\n" " Usually you will use `result_to_defect` instead.\n" "\n" " ## Example\n" "\n" " ```gleam\n" " new_defect(\"Something went wrong\")\n" " ```\n" ). -spec new_defect(FXA) -> problem(FXA). new_defect(Error) -> new_problem(Error, defect). -file("src/outcome.gleam", 55). ?DOC( " Create a Failure\n" " Use this if you need the `Problem` type only.\n" " Usually you will use `result_to_failure` instead.\n" "\n" " ## Example\n" "\n" " ```gleam\n" " new_failure(\"Something went wrong\")\n" " ```\n" ). -spec new_failure(FXC) -> problem(FXC). new_failure(Error) -> new_problem(Error, failure). -file("src/outcome.gleam", 73). ?DOC( " Convert an `Error(String)` into an `Error(Defect)`\n" " This is useful when you have a `Result(t, String)` and\n" " want to convert it into a `Result(t, Problem)`\n" "\n" " ## Example\n" "\n" " ```gleam\n" " Error(\"Something went wrong\")\n" " |> result_to_defect\n" " ```\n" ). -spec result_to_defect({ok, FXG} | {error, FXH}) -> {ok, FXG} | {error, problem(FXH)}. result_to_defect(Result) -> gleam@result:map_error(Result, fun new_defect/1). -file("src/outcome.gleam", 87). ?DOC( " Convert an `Error(String)` into an `Error(Failure)`\n" " This is useful when you have a `Result(t, String)` and\n" " want to convert it into a `Result(t, Problem)`\n" "\n" " ## Example\n" "\n" " ```gleam\n" " Error(\"Invalid input\")\n" " |> result_to_failure\n" " ```\n" ). -spec result_to_failure({ok, FXM} | {error, FXN}) -> {ok, FXM} | {error, problem(FXN)}. result_to_failure(Result) -> gleam@result:map_error(Result, fun new_failure/1). -file("src/outcome.gleam", 95). -spec map_error_in_problem(problem(FXS), fun((FXS) -> FXS)) -> problem(FXS). map_error_in_problem(Problem, Mapper) -> _record = Problem, {problem, Mapper(erlang:element(2, Problem)), erlang:element(3, _record), erlang:element(4, _record)}. -file("src/outcome.gleam", 103). ?DOC(" Map the error value\n"). -spec map_error({ok, FXV} | {error, problem(FXW)}, fun((FXW) -> FXW)) -> {ok, FXV} | {error, problem(FXW)}. map_error(Outcome, Mapper) -> gleam@result:map_error( Outcome, fun(_capture) -> map_error_in_problem(_capture, Mapper) end ). -file("src/outcome.gleam", 114). -spec tap_error_in_problem(problem(FYB), fun((FYB) -> any())) -> problem(FYB). tap_error_in_problem(Problem, Fun) -> Fun(erlang:element(2, Problem)), Problem. -file("src/outcome.gleam", 122). -spec tap_defect_in_problem(problem(FYF), fun((FYF) -> any())) -> problem(FYF). tap_defect_in_problem(Problem, Fun) -> case erlang:element(3, Problem) of defect -> Fun(erlang:element(2, Problem)), Problem; _ -> Problem end. -file("src/outcome.gleam", 135). -spec tap_failure_in_problem(problem(FYJ), fun((FYJ) -> any())) -> problem(FYJ). tap_failure_in_problem(Problem, Fun) -> case erlang:element(3, Problem) of failure -> Fun(erlang:element(2, Problem)), Problem; _ -> Problem end. -file("src/outcome.gleam", 150). ?DOC( " Use tap functions to log the errors\n" " This yields the `Problem` type.\n" ). -spec tap({ok, FYN} | {error, problem(FYO)}, fun((problem(FYO)) -> any())) -> {ok, FYN} | {error, problem(FYO)}. tap(Outcome, Fun) -> gleam@result:map_error( Outcome, fun(Problem) -> Fun(Problem), Problem end ). -file("src/outcome.gleam", 161). ?DOC(" This yields your error type.\n"). -spec tap_error({ok, FYV} | {error, problem(FYW)}, fun((FYW) -> any())) -> {ok, FYV} | {error, problem(FYW)}. tap_error(Outcome, Fun) -> gleam@result:map_error( Outcome, fun(_capture) -> tap_error_in_problem(_capture, Fun) end ). -file("src/outcome.gleam", 170). ?DOC( " Yield your error type\n" " Only called if the severity is Defect\n" ). -spec tap_defect({ok, FZC} | {error, problem(FZD)}, fun((FZD) -> any())) -> {ok, FZC} | {error, problem(FZD)}. tap_defect(Outcome, Fun) -> gleam@result:map_error( Outcome, fun(_capture) -> tap_defect_in_problem(_capture, Fun) end ). -file("src/outcome.gleam", 179). ?DOC( " Yield your error type\n" " Only called if the severity is Failure\n" ). -spec tap_failure({ok, FZJ} | {error, problem(FZK)}, fun((FZK) -> any())) -> {ok, FZJ} | {error, problem(FZK)}. tap_failure(Outcome, Fun) -> gleam@result:map_error( Outcome, fun(_capture) -> tap_failure_in_problem(_capture, Fun) end ). -file("src/outcome.gleam", 208). -spec push_to_stack(list(binary()), binary()) -> list(binary()). push_to_stack(Stack, Entry) -> [Entry | Stack]. -file("src/outcome.gleam", 212). -spec add_context_to_problem(problem(FZX), binary()) -> problem(FZX). add_context_to_problem(Problem, Value) -> _record = Problem, {problem, erlang:element(2, _record), erlang:element(3, _record), push_to_stack(erlang:element(4, Problem), Value)}. -file("src/outcome.gleam", 201). ?DOC( " Add context to an Outcome\n" " This will add a Context entry to the stack\n" "\n" " ## Example\n" "\n" " ```gleam\n" " Error(\"Something went wrong\")\n" " |> result_to_defect\n" " |> context(\"In find user function\")\n" " ```\n" ). -spec context({ok, FZQ} | {error, problem(FZR)}, binary()) -> {ok, FZQ} | {error, problem(FZR)}. context(Outcome, Context) -> gleam@result:map_error( Outcome, fun(_capture) -> add_context_to_problem(_capture, Context) end ). -file("src/outcome.gleam", 228). ?DOC( " Use this to show a failure to a user.\n" " Extracts the Error value from a `Problem` when the severity is `Failure`.\n" " otherwise it will return the default value given.\n" "\n" " ## Example\n" "\n" " ```gleam\n" " case result {\n" " Ok(value) -> io.debug(\"Success\")\n" " Error(problem) -> io.error(get_failure_in_problem(problem, \"Something went wrong\"))\n" " }\n" " ```\n" ). -spec get_failure_in_problem(problem(GAA), GAA) -> GAA. get_failure_in_problem(Problem, Default_value) -> case erlang:element(3, Problem) of defect -> Default_value; failure -> erlang:element(2, Problem) end. -file("src/outcome.gleam", 248). -spec problem_to_error(problem(GAI)) -> GAI. problem_to_error(Problem) -> erlang:element(2, Problem). -file("src/outcome.gleam", 244). ?DOC( " Remove the Problem wrapping, returning just your error.\n" "\n" " ## Example\n" "\n" " ```gleam\n" " let outcome = Error(\"Fail\") |> result_to_defect\n" "\n" " to_simple_result(outcome) == Error(\"Fail\")\n" " ```\n" ). -spec to_simple_result({ok, GAC} | {error, problem(GAD)}) -> {ok, GAC} | {error, GAD}. to_simple_result(Outcome) -> _pipe = Outcome, gleam@result:map_error(_pipe, fun problem_to_error/1). -file("src/outcome.gleam", 327). -spec long_suffix(severity()) -> binary(). long_suffix(Severity) -> case Severity of defect -> <<"Defect: "/utf8>>; failure -> <<"Failure: "/utf8>> end. -file("src/outcome.gleam", 323). -spec prettry_print_problem_error(severity(), binary()) -> binary(). prettry_print_problem_error(Severity, Error) -> <<(long_suffix(Severity))/binary, Error/binary>>. -file("src/outcome.gleam", 334). -spec pretty_print_stack_entry(binary()) -> binary(). pretty_print_stack_entry(Value) -> Value. -file("src/outcome.gleam", 252). -spec stack_to_lines(list(binary())) -> list(binary()). stack_to_lines(Stack) -> _pipe = Stack, _pipe@1 = lists:reverse(_pipe), gleam@list:map(_pipe@1, fun pretty_print_stack_entry/1). -file("src/outcome.gleam", 300). -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(4, Problem) of [] -> <<""/utf8>>; _ -> Stack end, <>. -file("src/outcome.gleam", 278). ?DOC( " Pretty print a Problem, including the stack.\n" " The latest problem appears at the top of the stack.\n" "\n" " ## Example\n" "\n" " ```gleam\n" " Error(\"Something went wrong\")\n" " |> result_to_defect\n" " |> context(\"In find user function\")\n" " |> context(\"More context\")\n" " |> pretty_print(function.identity)\n" " ```\n" "\n" " ```\n" " Defect: Something went wrong\n" "\n" " stack:\n" " In find user function\n" " More context\n" " ```\n" ). -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 ). -file("src/outcome.gleam", 296). ?DOC( " Print problem in one line\n" "\n" " ## Example\n" "\n" " ```gleam\n" " Error(\"Something went wrong\")\n" " |> result_to_defect\n" " |> context(\"In find user function\")\n" " |> print_line(function.identity)\n" " ```\n" "\n" " ```\n" " Defect: Something went wrong << In find user function\n" " ```\n" ). -spec print_line(problem(GAN), fun((GAN) -> binary())) -> binary(). print_line(Problem, To_s) -> pretty_print_with_joins(Problem, <<" < "/utf8>>, <<" < "/utf8>>, To_s).