gleyre

Types

Analog of the Result – analagous to a Result(a, Report).

pub type Outcome(a) {
  Okay(a)
  Err(Report)
}

Constructors

  • Okay(a)
  • Err(Report)

The guts of the error variant of an Outcome.

Contains the layers of error messages.

pub opaque type Report

Values

pub fn all(outcomes: List(Outcome(a))) -> Outcome(List(a))

Like result.all(), either returns the first Err() value in the list, or the whole list of positive outcomes.

pub fn annotate(report: Report, message: String) -> Report

Add a layer of messaging to an error variant.

pub fn error(message: String) -> Outcome(a)

Create a new error variant.

pub fn from(std_result: Result(a, b)) -> Outcome(a)

Convert from a regular Result by using string.inspect() if the Result is an error.

pub fn into(outcome: Outcome(a)) -> Result(a, String)

Turn you Outcome back into a regular Gleam Error.

pub fn map(outcome: Outcome(a), f: fn(a) -> b) -> Outcome(b)

Like result.map() – applies the given function if the outcome is Okay.

pub fn okay(ok: a) -> Outcome(a)

Create a new okay variant.

pub fn replace(outcome: Outcome(a), replacement: b) -> Outcome(b)

Like result.replace(), replaces the Outcome’s value if it’s Okay.

pub fn to_string(report: Report) -> String

Turn a Report into a formatted message displaying the layers of error messaging.

pub fn try(
  outcome: Outcome(a),
  next: fn(a) -> Outcome(b),
) -> Outcome(b)

Like result.try; for exploiting use sugar.

fn fallible_function(data: Kind) -> Outcome(OtherKind) {
  use step_one <- gleyre.try(other_fallible_function(data))
  use step_two <- gleyre.try(yet_another(step_one))

  Okay(step_two)
}
pub fn try_or_wrap(
  outcome: Outcome(a),
  message: String,
  next: fn(a) -> Outcome(b),
) -> Outcome(b)

For the use-case, but also adds an extra level of messaging to the error.

pub fn wrap(outcome: Outcome(a), message: String) -> Outcome(a)

If the Outcome is an error, this adds another layer of message to it.

pub fn yield_errors(report: Report) -> Yielder(String)

Iterate down through the layers of error messages.

pub fn yield_indexed(report: Report) -> Yielder(#(String, Int))

Iterate down through the indexed layers of error messages.

Search Document