View Source Punt (punt v0.1.0)

Documentation for Punt.

Link to this section Summary

Functions

Apply parser, and then decide what to do

Parse a boolean

Always failing parser

Parse a float

Parse a map key

Gets a nested value from nested maps

Parse a map, fallback to default

Parse a value at index

Parse a integer

Parse a list

Apply parser and map over result

Parse into a map or struct

Parse a nil

Parse a number

Parse into a map

Parse into a struct

Chooses one of the given parser

Parse a pair

Check whether a value holds for the input

Parse a pair

Parse a string

Always succeeding parser

Returns value you put in

Link to this section Functions

Apply parser, and then decide what to do

Useful for conditional parsing based on result of previous parse

Parse a boolean

example

Example

iex> Punt.boolean() |> Punt.parse(true)
{:ok, true}

iex> Punt.boolean() |> Punt.parse(false)
{:ok, false}

iex> Punt.boolean() |> Punt.parse("foo")
{:error, :not_a_boolean}

Always failing parser

Parse a float

example

Example

iex> Punt.float() |> Punt.parse(3.4)
{:ok, 3.4}

iex> Punt.float() |> Punt.parse(3)
{:error, :not_a_float}

iex> Punt.float() |> Punt.parse("foo")
{:error, :not_a_float}

Parse a map key

example

Example

iex> Punt.get(:name, Punt.string) |> Punt.parse(%{name: "foo"})
{:ok, "foo"}

iex> Punt.get(:name, Punt.string) |> Punt.parse(%{bar: "foo"})
{:error, %{code: :no_such_get, field: :name, input: %{bar: "foo"}}}

Gets a nested value from nested maps

example

Example

iex> Punt.get_in([:a, :b], Punt.number()) |> Punt.parse(%{a: %{b: 123}})
{:ok, 123}
Link to this function

get_or_missing(name, default, p)

View Source

Parse a map, fallback to default

example

Example

iex> Punt.get_or_missing(:name, "default", Punt.string) |> Punt.parse(%{name: "foo"})
{:ok, "foo"}

iex> Punt.get_or_missing(:name, "default", Punt.string) |> Punt.parse(%{bar: "foo"})
{:ok, "default"}

Parse a value at index

example

Example

iex> Punt.index(2, Punt.number) |> Punt.parse([1,2,3])
{:ok, 3}

iex> Punt.index(10, Punt.number) |> Punt.parse([1,2,3])
{:error, :not_a_number}

Parse a integer

example

Example

iex> Punt.integer() |> Punt.parse(3)
{:ok, 3}

iex> Punt.integer() |> Punt.parse(3.4)
{:error, :not_an_integer}

iex> Punt.integer() |> Punt.parse("foo")
{:error, :not_an_integer}

Parse a list

example

Example

iex> Punt.list_of(Punt.string) |> Punt.parse(["foo", "bar"])
{:ok, ["foo", "bar"]}

iex> Punt.list_of(Punt.string) |> Punt.parse(["foo", 1])
{:error, %{details: :not_a_string, failed_element: 1}}
Link to this function

map(result_fun, decoders)

View Source
Link to this function

map(p, map_fun, gen_fun \\ nil)

View Source

Apply parser and map over result

example

Example

iex> Punt.map(Punt.string(), &String.to_integer(&1)) |> Punt.parse("123")
Link to this function

new(map, input, module \\ nil)

View Source

Parse into a map or struct

example

Example

iex> Punt.new([a: Punt.get(:b, Punt.string)], %{b: "foo"})
{:ok, %{a: "foo"}}

Parse a nil

example

Example

iex> Punt.null() |> Punt.parse(nil)
{:ok, nil}

iex> Punt.null() |> Punt.parse("foo")
{:error, :not_a_null}

Parse a number

example

Example

iex> Punt.number() |> Punt.parse(3.4)
{:ok, 3.4}

iex> Punt.number() |> Punt.parse(3)
{:ok, 3}

iex> Punt.number() |> Punt.parse("foo")
{:error, :not_a_number}

Parse into a map

example

Example

iex> Punt.of_map([a: Punt.get(:b, Punt.string)]) |> Punt.parse(%{b: "foo"})
{:ok, %{a: "foo"}}

Parse into a struct

example

Example

iex> Punt.of_struct([a: Punt.get(:b, Punt.string)], A) |> Punt.parse(%{b: "foo"})
{:ok, %PuntTest.A{a: "foo"}}

Chooses one of the given parser

example

Example

iex> Punt.one_of([Punt.null(), Punt.integer()]) |> Punt.parse(123)
{:ok, 123}

iex> Punt.one_of([Punt.null(), Punt.integer()]) |> Punt.parse(nil)
{:ok, nil}

Parse a pair

example

Example

iex> Punt.pair_of(Punt.string, Punt.string) |> Punt.parse(["foo", "bar"])
{:ok, {"foo", "bar"}}
Link to this function

predicate(fun, context \\ [])

View Source

Check whether a value holds for the input

example

Example

iex> Punt.predicate(& &1 == 123) |> Punt.parse(123)
{:ok, 123}

iex> Punt.predicate(& &1 != 123) |> Punt.parse(123)
{:error, %{context: [], input: 123, reason: :predicate_failed}}

Parse a pair

example

Example

iex> Punt.singleton_of(Punt.string) |> Punt.parse(["foo"])
{:ok, "foo"}

Parse a string

example

Example

iex> Punt.string() |> Punt.parse("foo")
{:ok, "foo"}

iex> Punt.string() |> Punt.parse(3.4)
{:error, :not_a_string}

iex> Punt.string() |> Punt.parse(3)
{:error, :not_a_string}

Always succeeding parser

Returns value you put in

example

Example

iex> Punt.value() |> Punt.parse("123")
{:ok, "123"}