ymlr v0.0.1 Ymlr

Encodes data into YAML documents using the Ymlr.Encoder. Every document starts with a separator ("---") and can be enhanced with comments.

Link to this section Summary

Functions

Encodes a given data as YAML document with a separator ("---") at the beginning.

Encodes a given data as YAML document with a separator ("---") at the beginning. Raises if it cannot be encoded.

Encodes a given list of data as "---" separated YAML documents.

Encodes a given list of data as "---" separated YAML documents. Raises if it cannot be encoded.

Link to this section Types

Specs

document() ::
  Ymlr.Encoder.data()
  | {binary(), Ymlr.Encoder.data()}
  | {[binary()], Ymlr.Encoder.data()}

Link to this section Functions

Link to this function

document(document)

Specs

document(document()) :: {:ok, binary()} | {:error, binary()}

Encodes a given data as YAML document with a separator ("---") at the beginning.

Optinally you can pass a tuple with comment(s) and data as first argument.

Examples

iex> Ymlr.document(%{a: 1})
{:ok, "---\na: 1\n"}

iex> Ymlr.document({"comment", %{a: 1}})
{:ok, "---\n# comment\na: 1\n"}

iex> Ymlr.document({["comment 1", "comment 2"], %{a: 1}})
{:ok, "---\n# comment 1\n# comment 2\na: 1\n"}

iex> Ymlr.document({[], {"a", "b"}})
{:error, "The given data {\"a\", \"b\"} cannot be converted to YAML."}
Link to this function

document!(document)

Specs

document!(document()) :: binary()

Encodes a given data as YAML document with a separator ("---") at the beginning. Raises if it cannot be encoded.

Optinally you can pass a tuple with comment(s) and data as first argument.

Examples

iex> Ymlr.document!(%{a: 1})
"---\na: 1\n"

iex> Ymlr.document!({"comment", %{a: 1}})
"---\n# comment\na: 1\n"

iex> Ymlr.document!({["comment 1", "comment 2"], %{a: 1}})
"---\n# comment 1\n# comment 2\na: 1\n"

iex> Ymlr.document!({[], {"a", "b"}})
** (ArgumentError) The given data {"a", "b"} cannot be converted to YAML.
Link to this function

documents(documents)

Specs

documents([document()]) :: {:ok, binary()} | {:error, binary()}

Encodes a given list of data as "---" separated YAML documents.

Examples

iex> Ymlr.documents([%{a: 1}])
{:ok, "---\na: 1\n"}

iex> Ymlr.documents([%{a: 1}, %{b: 2}])
{:ok, "---\na: 1\n\n---\nb: 2\n"}

iex> Ymlr.documents([{[], {"a", "b"}}])
{:error, "The given data {\"a\", \"b\"} cannot be converted to YAML."}

iex> Ymlr.documents(%{a: "a"})
{:error, "The given argument is not a list of documents. Use document/1, document/2, document!/1 or document!/2 for a single document."}
Link to this function

documents!(documents)

Encodes a given list of data as "---" separated YAML documents. Raises if it cannot be encoded.

Examples

iex> Ymlr.documents!([%{a: 1}])
"---\na: 1\n"

iex> Ymlr.documents!([%{a: 1}, %{b: 2}])
"---\na: 1\n\n---\nb: 2\n"

iex> Ymlr.documents!([{[], {"a", "b"}}])
** (ArgumentError) The given data {"a", "b"} cannot be converted to YAML.

iex> Ymlr.documents!(%{a: "a"})
** (ArgumentError) The given argument is not a list of documents. Use document/1, document/2, document!/1 or document!/2 for a single document.