plymio_fontais v0.2.0 Plymio.Fontais.Result View Source

Functions for Result Patterns: {:ok, value} or {:error, error}.

See Plymio.Fontais for overview and documentation terms.

Link to this section Summary

Functions

normalise0_result/1 takes an argument an enforces pattern 0

normalise0_results/1 takes an enum and applies pattern 0 to each element, returning {:ok, enum} where enum will be a Stream

normalise1_result/1 takes an argument an enforces pattern 1

normalise1_results/1 takes an enum and applies pattern 1 to each element, returning {:ok, enum} where enum will be a Stream

normalise2_result/1 takes an argument an applies pattern 2

normalise2_results/1 takes an enum and applies pattern 2 to each element, returning {:ok, enum} where enum will be a Stream

validate_results/1 takes an enum and confirms each value is a result, returning {:ok, enum}

Link to this section Types

Link to this section Functions

Link to this function normalise0_result(result) View Source
normalise0_result(any()) :: {:ok, any()} | {:error, error()}

normalise0_result/1 takes an argument an enforces pattern 0.

If the argument is {:ok, value} or {:error, error}, it is passed through unchanged.

If the argument is anything else, it is converted into {:error, error}.

Examples

iex> {:ok, 42} |> normalise0_result
{:ok, 42}

iex> {:error, error} = {:error, %ArgumentError{message: "value is 42"}} |> normalise0_result
...> error |> Exception.message
"value is 42"

iex> {:error, error} = 42 |> normalise0_result
...> error |> Exception.message
"pattern0 result invalid, got: 42"
Link to this function normalise0_results(results) View Source
normalise0_results(any()) :: {:ok, results()} | {:error, error()}

normalise0_results/1 takes an enum and applies pattern 0 to each element, returning {:ok, enum} where enum will be a Stream.

Examples

iex> {:ok, enum} = [
...>   {:ok, 42},
...>   {:error, %BadMapError{term: :not_a_map}},
...>   "HelloWorld"
...> ] |> normalise0_results
...> enum |> Enum.to_list
[{:ok, 42}, {:error, %BadMapError{term: :not_a_map}},
 {:error, %ArgumentError{message: "pattern0 result invalid, got: HelloWorld"}}]
Link to this function normalise1_result(result) View Source
normalise1_result(any()) :: {:ok, any()} | {:error, error()}

normalise1_result/1 takes an argument an enforces pattern 1.

If the argument is {:ok, value} or {:error, error}, it is passed through unchanged.

An other value is converted into {:ok, value}.

Examples

iex> {:ok, 42} |> normalise1_result
{:ok, 42}

iex> {:error, error} = {:error, %ArgumentError{message: "value is 42"}} |> normalise1_result
...> error |> Exception.message
"value is 42"

iex> 42 |> normalise1_result
{:ok, 42}
Link to this function normalise1_results(results) View Source
normalise1_results(any()) :: {:ok, results()} | {:error, error()}

normalise1_results/1 takes an enum and applies pattern 1 to each element, returning {:ok, enum} where enum will be a Stream.

Examples

iex> {:ok, enum} = [
...>   {:ok, 42},
...>   {:error, %BadMapError{term: :not_a_map}},
...>   "HelloWorld"
...> ] |> normalise1_results
...> enum |> Enum.to_list
[{:ok, 42}, {:error, %BadMapError{term: :not_a_map}}, {:ok, "HelloWorld"}]
Link to this function normalise2_result(result) View Source
normalise2_result(any()) :: atom() | {:ok, any()} | {:error, error()}

normalise2_result/1 takes an argument an applies pattern 2.

Its works like normalise1_result/1 except if the value is nil or the unset value (see Plymio.Fontais.Guard.the_unset_value/0), it is passed through unchanged.

Examples

iex> {:ok, 42} |> normalise2_result
{:ok, 42}

iex> {:error, error} = {:error, %ArgumentError{message: "value is 42"}} |> normalise2_result
...> error |> Exception.message
"value is 42"

iex> 42 |> normalise2_result
{:ok, 42}

iex> nil |> normalise2_result
nil

iex> value1 = Plymio.Fontais.Guard.the_unset_value
...> value2 = value1
...> value3 = value1 |> normalise2_result
...> value3 == value2
true
Link to this function normalise2_results(results) View Source
normalise2_results(any()) :: {:ok, results()} | {:error, error()}

normalise2_results/1 takes an enum and applies pattern 2 to each element, returning {:ok, enum} where enum will be a Stream.

Examples

iex> unset_value = Plymio.Fontais.Guard.the_unset_value()
...> {:ok, enum} = [
...>   nil,
...>   {:ok, 42},
...>   {:error, %BadMapError{term: :not_a_map}},
...>   unset_value,
...>   "HelloWorld"
...> ] |> normalise2_results
...> results = enum |> Enum.to_list
...> nil = results |> Enum.at(0)
...> {:error, %BadMapError{term: :not_a_map}} = results |> Enum.at(2)
...> ^unset_value = results |> Enum.at(3)
...> results |> Enum.at(3) |> Plymio.Fontais.Guard.is_value_unset
true
Link to this function validate_results(results) View Source
validate_results(any()) :: result()

validate_results/1 takes an enum and confirms each value is a result, returning {:ok, enum}.

Examples

iex> {:ok, enum} = [
...>   {:ok, 42},
...>   {:error, %BadMapError{term: :not_a_map}},
...> ] |> validate_results
...> enum |> Enum.to_list
[{:ok, 42}, {:error, %BadMapError{term: :not_a_map}}]

iex> {:error, error} = [
...>   {:ok, 42},
...>   {:error, %BadMapError{term: :not_a_map}},
...>   "HelloWorld"
...> ] |> validate_results
...> error |> Exception.message
"result invalid, got: \"HelloWorld\""

iex> {:error, error} = [
...>   {:ok, 42},
...>   {:error, %BadMapError{term: :not_a_map}},
...>   "HelloWorld", :not_a_result
...> ] |> validate_results
...> error |> Exception.message
"results invalid, got: [\"HelloWorld\", :not_a_result]"