Current section

Files

Jump to
ark lib ark ok.ex
Raw

lib/ark/ok.ex

defmodule Ark.Ok do
defmacro __using__(_) do
quote do
import unquote(__MODULE__)
end
end
@doc false
def __ark_alias, do: :ok
@doc false
def __ark_doc do
"""
This module provides function to work with ok/error tuples.
"""
end
@doc """
Wrapping ok.
Wraps the passed value in an `:ok` tuple if :
- The value is not already wrapped
- It is not an `{:error, ...}` tuple
- The value is not the single atom `:ok`
"""
def ok(value)
def ok(:ok),
do: :ok
def ok({:ok, val}),
do: {:ok, val}
def ok({:error, _} = err),
do: err
def ok({:error, _, _} = err),
do: err
def ok({:error, _, _, _} = err),
do: err
def ok(val),
do: {:ok, val}
@doc """
`ẁok` is an alias of wrapping function `:ok`.
"""
def wok(value),
do: ok(value)
@doc """
Unwrapping ok.
Unwraps an `{:ok, val}` tuple, giving only the value, returning anything else
as-is. Does not unwrap `{:error, ...}` tuples.
"""
def uok(value)
def uok({:ok, val}),
do: val
def uok({:error, _} = val),
do: val
def uok({:error, _, _} = val),
do: val
def uok({:error, _, _, _} = val),
do: val
def uok(other),
do: other
@doc """
Unwrapping ok with raise.
Unwraps an `{:ok, val}` tuple, giving only the value, or returns the single
`:ok` atom as-is. Raises with any other value.
"""
def uok!(value)
def uok!(:ok),
do: :ok
def uok!({:ok, val}),
do: val
def uok!(other) do
raise ArgumentError, message: "uok! got: #{inspect(other)}"
end
@doc """
Questionning ok.
Returns true if the value is an `{:ok, val}` tuple or the single
atom `:ok`.
Returns false otherwise.
"""
def ok?(value)
def ok?(:ok),
do: true
def ok?({:ok, _}),
do: true
def ok?(_),
do: false
end