View Source ExResult (ex_result v0.0.5)
Module for working with "result" tuples. Provides helpers for wrapping, unwrapping, and transforming result tuples.
An opinionated stance on what constitutes a "result" tuple is taken here. A
result tuple is a tuple of the form {:ok, value}
or {:error, value}
and
nothing else.
These helpers are most useful in pipelines.
Summary
Functions
Wraps a value in an :error
tuple.
Checks if a value is an :error
tuple.
Guard to check if a value is a result tuple.
Wraps a value in an :ok
tuple.
Checks if a value is an :ok
tuple.
Unwraps a value from an :ok
tuple or returns an :error
tuple as-is.
Unwraps a value from an :ok
tuple or raises if not an :ok
tuple.
Unwraps the "value" from an :ok
tuple and executes a transform on it or
returns an :error
tuple as-is.
Executes a transform on the "value" of an :ok
tuple or raises if not an
:ok
tuple.
Updates the "value" of an :ok
tuple or returns an :error tuple as-is.
Updates the "value" of an :ok
tuple or raises if not an :ok
tuple.
Types
@type error_result() :: {:error, any()}
@type ok_result() :: {:ok, any()}
@type result() :: ok_result() | error_result()
Functions
@spec error(any()) :: error_result()
Wraps a value in an :error
tuple.
If the value is already a result tuple, it will be returned as-is.
Examples
iex> error(:foo)
{:error, :foo}
iex> error({:ok, 1})
{:ok, 1}
iex> error({:error, :foo})
{:error, :foo}
Checks if a value is an :error
tuple.
Examples
iex> error?({:ok, 1})
false
iex> error?({:error, :foo})
true
iex> error?(1)
false
Guard to check if a value is a result tuple.
A result tuple is a tuple of the form {:ok, value}
or {:error, value}
and
nothing else.
Examples
iex> is_result({:ok, 1})
true
iex> is_result({:error, :foo})
true
iex> is_result(1)
false
Wraps a value in an :ok
tuple.
If the value is already a result tuple, it will be returned as-is.
Examples
iex> ok(1)
{:ok, 1}
iex> ok({:error, :foo})
{:error, :foo}
iex> ok({:ok, 1})
{:ok, 1}
Checks if a value is an :ok
tuple.
Examples
iex> ok?({:ok, 1})
true
iex> ok?({:error, :foo})
false
iex> ok?(1)
false
Unwraps a value from an :ok
tuple or returns an :error
tuple as-is.
If the value is not an :ok
or :error
tuple, an error will be raised.
Examples
iex> unwrap({:ok, 1})
1
iex> unwrap({:error, :foo})
{:error, :foo}
iex> unwrap(1)
** (ArgumentError) 1st argument: not an :ok or :error result tuple
Unwraps a value from an :ok
tuple or raises if not an :ok
tuple.
Examples
iex> unwrap!({:ok, 1})
1
iex> unwrap!({:error, :foo})
** (ArgumentError) 1st argument: not an :ok result tuple
iex> unwrap!(1)
** (ArgumentError) 1st argument: not an :ok result tuple
Unwraps the "value" from an :ok
tuple and executes a transform on it or
returns an :error
tuple as-is.
If the value is not an :ok
or :error
tuple, an error will be raised.
Examples
iex> unwrap_and_update({:ok, 1}, &(&1 * 2))
2
iex> unwrap_and_update({:error, :foo}, &(&1 * 2))
{:error, :foo}
iex> unwrap_and_update(1, &(&1 * 2))
** (ArgumentError) 1st argument: not an :ok or :error result tuple
Executes a transform on the "value" of an :ok
tuple or raises if not an
:ok
tuple.
Examples
iex> unwrap_and_update!({:ok, 1}, &(&1 * 2))
2
iex> unwrap_and_update!({:error, :foo}, &(&1 * 2))
** (ArgumentError) 1st argument: not an :ok result tuple
iex> unwrap_and_update!(1, &(&1 * 2))
** (ArgumentError) 1st argument: not an :ok result tuple
Updates the "value" of an :ok
tuple or returns an :error tuple as-is.
If the value is not an :ok
or :error
tuple, an error will be raised.
Examples
iex> update({:ok, 1}, &(&1 + 1))
{:ok, 2}
iex> update({:error, :foo}, &(&1 + 1))
{:error, :foo}
iex> update(1, &(&1 + 1))
** (ArgumentError) 1st argument: not an :ok or :error result tuple
Updates the "value" of an :ok
tuple or raises if not an :ok
tuple.
Examples
iex> update!({:ok, 1}, &(&1 + 1))
{:ok, 2}
iex> update!({:error, :foo}, &(&1 + 1))
** (ArgumentError) 1st argument: not an :ok result tuple
iex> update!(1, &(&1 + 1))
** (ArgumentError) 1st argument: not an :ok result tuple