View Source Want.String (want v1.17.1)

Manages conversions to and from strings.

Summary

Functions

Casts a given value to a string.

Callback implementation for Want.Type.cast/2.

Types

@type result() :: {:ok, binary()} | {:error, binary()}

Functions

@spec cast(value :: any()) :: result()

Casts a given value to a string.

Options

  • :max - Maximum allowable string length.
  • :min - Minimum allowable string length.
  • ':decode' - Currently only supports :uri; runs URI.decode on the input value
  • :matches - The resulting string must match the given regex.

Examples

iex> Want.String.cast(1)
{:ok, "1"}

iex> Want.String.cast({:a, :b})
{:error, "Failed to convert value {:a, :b} to string."}

iex> Want.String.cast(:hello, max: 3)
{:error, "String length exceeds maximum of 3."}

iex> Want.String.cast(:a, min: 3)
{:error, "String length below minimum of 3."}

iex> Want.String.cast("hello%20world", decode: :uri)
{:ok, "hello world"}

iex> Want.String.cast(:a, matches: ~r/a/)
{:ok, "a"}
@spec cast(value :: any(), opts :: Keyword.t()) :: result()

Callback implementation for Want.Type.cast/2.