Formata.Codec.TOML.Encoder (formata v0.1.0)

Copy Markdown View Source

Focused TOML 1.0.0 encoder.

gsmlg_toml 1.0.0 only decodes, so Formata ships this encoder.

Domain

Elixir valueTOML
map with string keys (root)the document (root table)
nested map[table] section (inline {} inside arrays)
non-empty list of mapsarray of tables [[name]]
other list (incl. [])inline array
UTF-8 binarybasic string with escapes
integer in signed 64-bit rangeinteger
float / :infinity / :negative_infinity / :nanfloat / inf / -inf / nan
true / falseboolean
DateTimeoffset date-time
NaiveDateTimelocal date-time
Datelocal date
Timelocal time

Everything else is rejected: a non-map root, nil, atoms, tuples, other structs, non-string or non-UTF-8 keys, integers outside the 64-bit range, years outside 0000..9999, and DateTime offsets that are not whole minutes. Keys are emitted in sorted order; plain keys are bare when they match [A-Za-z0-9_-]+ and quoted otherwise.

Summary

Functions

Encodes a map as a TOML document.

Functions

encode(data)

@spec encode(term()) :: {:ok, binary()} | {:error, [Formata.Error.t(), ...]}

Encodes a map as a TOML document.

iex> Formata.Codec.TOML.Encoder.encode(%{"title" => "x", "owner" => %{"name" => "Tom"}})
{:ok, ~s(title = "x"\n\n[owner]\nname = "Tom"\n)}