defmodule Ark.Wok do @doc """ Wrapping ok. Wraps the passed value in an `:ok` tuple if : - The value is not already wrapped - It is not an `{:error, v}` tuple - The value is not the single atom `:ok` """ def ok(:ok), do: :ok def ok({:ok, val}), do: {:ok, val} def ok({:error, _} = err), do: err def ok(val), do: {:ok, val} @doc """ `ẁok` is an alias of wrapping function `:ok`. """ def wok(it), do: ok(it) @doc """ Unwrapping ok. Unwraps an `{:ok, val}` tuple, giving only the value, returning anything else as-is. Does not unwrap `:error` tuples. """ def uok({:ok, 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!(: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?(:ok), do: true def ok?({:ok, _}), do: true def ok?(_), do: false end