data v0.5.2 Data.Parser.BuiltIn

Parsers for built-in Elixir data types.

Link to this section Summary

Functions

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

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 floats, and returns the domain error :not_a_float for all other inputs.

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 successfully parses only nil. Any other values, including false and the atom :nothing cause the parser to fail with a domain error of :not_nil

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

Creates a parser that successfully parses strings representing either Integers or Floats.

Link to this section Functions

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

Examples

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

iex> Data.Parser.BuiltIn.atom().(:other_atom)
{:ok, :other_atom}

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


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

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 floats, and returns the domain error :not_a_float for all other inputs.

Examples

iex> Data.Parser.BuiltIn.float().(1.0)
{:ok, 1.0}

iex> {:error, e} = Data.Parser.BuiltIn.float().(1)
...> Error.reason(e)
:not_a_float

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

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 successfully parses only nil. Any other values, including false and the atom :nothing cause the parser to fail with a domain error of :not_nil

Examples

iex> Data.Parser.BuiltIn.null().(nil)
{:ok, nil}

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

iex> {:error, e} = Data.Parser.BuiltIn.null().(false)
...> Error.reason(e)
:not_nil

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
Link to this function

string_of(mod)

string_of(Integer | Float) :: Data.Parser.t(integer() | float(), Error.t())

Creates a parser that successfully parses strings representing either Integers or Floats.

Returns a domain error detailing the parse failure on bad inputs.

Look out! Partial results, such as that of Integer.parse("abc123"), still count as errors!

Examples

iex> Data.Parser.BuiltIn.string_of(Float).("1.1")
{:ok, 1.1}

iex> {:error, e} = Data.Parser.BuiltIn.string_of(Float).("abc")
...> Error.reason(e)
:not_parseable_as_float
...> Error.details(e)
%{input: "abc", native_parser_output: :error}

iex> Data.Parser.BuiltIn.string_of(Integer).("1234567890")
{:ok, 1234567890}

iex> {:error, e} = Data.Parser.BuiltIn.string_of(Integer).("123abc")
...> Error.reason(e)
:not_parseable_as_integer
...> Error.details(e)
%{input: "123abc", native_parser_output: {123, "abc"}}

iex> {:error, e} = Data.Parser.BuiltIn.string_of(Integer).([])
...> Error.reason(e)
:not_a_string