Box.Result (box v0.15.1)

View Source

Helper function to deal with :ok/:error tuples

Summary

Functions

Apply another result function to a success result

Returns an error tuple

Creates a result from a boolean value

Returns {:ok, value} if the value is not nil, returns an error tuple if the given value is nil with error as second value

Log value using success or error function depending on the result

Maps a success value

Returns an ok tuple

Returns true if the result is an ok tuple

Applies a function on success result but without keeping the previous result

Extracts the value from a success tuple

Returns success value if success, fallbacks otherwise

Types

error(error_type)

@type error(error_type) :: {:error, error_type} | :error

success(success_type)

@type success(success_type) :: {:ok, success_type}

t(success_type, error_type)

@type t(success_type, error_type) :: success(success_type) | error(error_type)

Functions

and_then(error, function)

@spec and_then(t(any(), any()), (any() -> t(any(), any()))) :: t(any(), any())

Apply another result function to a success result

fail(error)

@spec fail(any()) :: t(any(), any())

Returns an error tuple

from_boolean(bool, success, error)

@spec from_boolean(boolean(), any(), any()) :: t(any(), any())

Creates a result from a boolean value

from_nil(value, error \\ nil)

@spec from_nil(any(), any()) :: t(any(), any())

Returns {:ok, value} if the value is not nil, returns an error tuple if the given value is nil with error as second value

log(result, success_function, error_function \\ &Function.identity/1)

@spec log(t(any(), any()), (any() -> any()), (any() -> any())) :: t(any(), any())

Log value using success or error function depending on the result

map(error, function)

@spec map(t(any(), any()), (any() -> any())) :: t(any(), any())

Maps a success value

succeed(record)

@spec succeed(any()) :: t(any(), any())

Returns an ok tuple

succeeded?(arg1)

@spec succeeded?(t(any(), any())) :: boolean()

Returns true if the result is an ok tuple

tap(result, success_function, error_function \\ &Function.identity/1)

@spec tap(t(any(), any()), (any() -> any()), (any() -> any())) :: t(any(), any())

Applies a function on success result but without keeping the previous result

unwrap!(other)

@spec unwrap!(t(any(), any())) :: any()

Extracts the value from a success tuple

with_default(arg1, fallback_function)

@spec with_default(t(any(), any()), any()) :: any()

Returns success value if success, fallbacks otherwise