HttpStructuredField (http_structured_field v0.1.1) View Source

Top level API to parse and serialize data.

Link to this section Summary

Functions

Parse Structured Field datadata.

Link to this section Types

Specs

item() ::
  {:integer, integer()}
  | {:decimal, float(), []}
  | {:boolean, bool()}
  | {:boolean, bool(), []}
  | {:string, binary()}
  | {:string, binary(), []}
  | {:token, binary()}
  | {:token, binary(), []}
  | {:binary, binary()}
  | {:binary, binary(), []}
  | list()

Link to this section Functions

Link to this function

parse(value, opts \\ [])

View Source

Specs

parse(binary(), Keyword.t()) :: {:ok, item()} | {:error, term()}

Parse Structured Field datadata.

By default, assumes that input is a single Item or a List. For Dictionaries, set the type: :dict option.

Returns a tagged tuple with the result or an error if parsing failed. If the item has paramerters, then the tuple also contains a list of parameters. Parmeters and dictionary members are represented as lists of tuples where the name is the first tuple element.

Examples

iex> HttpStructuredField.parse("42")
{:ok, {:integer, 42}}

iex> HttpStructuredField.parse("4.5")
{:ok, {:decimal, 4.5}}

iex> HttpStructuredField.parse("?1")
{:ok, {:boolean, true}}

iex> HttpStructuredField.parse(~S("hello world"))
{:ok, {:string, "hello world"}}

iex> HttpStructuredField.parse("foo123/456")
{:ok, {:token, "foo123/456"}}

iex> HttpStructuredField.parse(":cHJldGVuZCB0aGlzIGlzIGJpbmFyeSBjb250ZW50Lg==:")
{:ok, {:binary, "pretend this is binary content."}}

iex> HttpStructuredField.parse("1; abc; b=?0")
{:ok, {:integer, 1, [{"abc", {:boolean, true}}, {"b", {:boolean, false}}]}}

iex> HttpStructuredField.parse("foo, bar")
{:ok, [{:token, "foo"}, {:token, "bar"}]}

iex> HttpStructuredField.parse("a=(1 2), b=3, c=4;aa=bb, d=(5 6);valid", type: :dict)
{:ok, [
  {"a", {:inner_list, [integer: 1, integer: 2]}},
  {"b", {:integer, 3}},
  {"c", {:integer, 4, [{"aa", {:token, "bb"}}]}},
  {"d", {:inner_list, [integer: 5, integer: 6], [{"valid", {:boolean, true}}]}}
]}