data v0.4.1 Data.Parser.BuiltIn

Parsers for built-in Elixir data types.

Link to this section Summary

Functions

Creates a parser that successfully parses booleans, and returns the domain error :not_a_boolean for all other inputs.

Creates a parser that successfully parses Date.ts or String.t that represent legitimate Date.ts.

Creates a parser that successfully parses DateTime.ts or String.t that represent legitimate DateTime.ts.

Creates a parser that successfully parses integers, and returns the domain error :not_an_integer for all other inputs.

Creates a parser that successfully parses NaiveDateTime.ts or String.t that represent legitimate NaiveDateTime.ts.

Creates a parser that succesfully parses String.ts (a.k.a binaries), and returns the domain error :not_a_string for all other inputs.

Link to this section Functions

Creates a parser that successfully parses booleans, and returns the domain error :not_a_boolean for all other inputs.

Examples

iex> Data.Parser.BuiltIn.boolean().(true)
{:ok, true}

iex> Data.Parser.BuiltIn.boolean().(false)
{:ok, false}

iex> {:error, e} = Data.Parser.BuiltIn.boolean().(1.0)
...> Error.reason(e)
:not_a_boolean


iex> {:error, e} = Data.Parser.BuiltIn.boolean().([:truth, :or, :dare])
...> Error.reason(e)
:not_a_boolean

Creates a parser that successfully parses Date.ts or String.t that represent legitimate Date.ts.

Returns a domain error representing the parse failure if the string input cannot be parsed, and the domain error :not_a_date for all other inputs.

Examples

iex> {:ok, d} = Data.Parser.BuiltIn.date().(~D[1999-12-31])
...> d
~D[1999-12-31]

iex> {:ok, d} = Data.Parser.BuiltIn.date().("1999-12-31")
...> d
~D[1999-12-31]

iex> {:error, e} = Data.Parser.BuiltIn.date().("19991232")
...> Error.reason(e)
:invalid_format

iex> {:error, e} = Data.Parser.BuiltIn.date().("1999-12-32")
...> Error.reason(e)
:invalid_date

iex> {:error, e} = Data.Parser.BuiltIn.date().(123456789)
...> Error.reason(e)
:not_a_date

Creates a parser that successfully parses DateTime.ts or String.t that represent legitimate DateTime.ts.

Returns a domain error representing the parse failure if the string input cannot be parsed, and the domain error :not_a_datetime for all other inputs.

Examples

iex> Data.Parser.BuiltIn.datetime().(~U[1999-12-31 23:59:59Z])
{:ok, ~U[1999-12-31 23:59:59Z]}

iex> Data.Parser.BuiltIn.datetime().("1999-12-31 23:59:59Z")
{:ok, ~U[1999-12-31 23:59:59Z]}

iex> {:error, e} = Data.Parser.BuiltIn.datetime().("1999-12-32 23:59:59Z")
...> Error.reason(e)
:invalid_date

iex> {:error, e} = Data.Parser.BuiltIn.datetime().("1999-12-31 23:59:99Z")
...> Error.reason(e)
:invalid_time

iex> {:error, e} = Data.Parser.BuiltIn.datetime().("1999-12-31 23:59:59")
...> Error.reason(e)
:missing_offset

iex> {:error, e} = Data.Parser.BuiltIn.datetime().(123456789)
...> Error.reason(e)
:not_a_datetime

Creates a parser that successfully parses integers, and returns the domain error :not_an_integer for all other inputs.

Examples

iex> Data.Parser.BuiltIn.integer().(1)
{:ok, 1}

iex> {:error, e} = Data.Parser.BuiltIn.integer().(1.0)
...> Error.reason(e)
:not_an_integer

iex> {:error, e} = Data.Parser.BuiltIn.integer().(:hi)
...> Error.reason(e)
:not_an_integer
Link to this function

naive_datetime()

naive_datetime() :: Data.Parser.t(NaiveDateTime.t(), Error.t())

Creates a parser that successfully parses NaiveDateTime.ts or String.t that represent legitimate NaiveDateTime.ts.

Returns a domain error representing the parse failure if the string input cannot be parsed, and the domain error :not_a_naive_datetime for all other inputs.

Examples

iex> Data.Parser.BuiltIn.naive_datetime.(~N[1999-12-31 23:59:59])
{:ok, ~N[1999-12-31 23:59:59]}

iex> Data.Parser.BuiltIn.naive_datetime.("1999-12-31 23:59:59")
{:ok, ~N[1999-12-31 23:59:59]}

iex> {:error, e} = Data.Parser.BuiltIn.naive_datetime.("1999-12-32 23:59:59")
...> Error.reason(e)
:invalid_date

iex> {:error, e} = Data.Parser.BuiltIn.naive_datetime.("1999-12-31 23:59:99")
...> Error.reason(e)
:invalid_time

iex> {:error, e} = Data.Parser.BuiltIn.naive_datetime.(123456789)
...> Error.reason(e)
:not_a_naive_datetime

Creates a parser that succesfully parses String.ts (a.k.a binaries), and returns the domain error :not_a_string for all other inputs.

Examples

iex> Data.Parser.BuiltIn.string().("hi")
{:ok, "hi"}

iex> {:error, e} = Data.Parser.BuiltIn.string().('hi')
...> Error.reason(e)
:not_a_string

iex> {:error, e} = Data.Parser.BuiltIn.string().(:hi)
...> Error.reason(e)
:not_a_string