Elixir:
Working with TOML

How to:

First, add a TOML parser to your mix dependencies. This example uses toml-elixir:

def deps do
  [
    {:toml_elixir, "~> 2.0"}
  ]
end

Read a TOML file:

{:ok, toml_data} = File.read("config.toml")
{:ok, parsed_data} = TomlElixir.parse(toml_data)

To convert Elixir data to TOML:

data = %{title: "TOML Example", owner: %{name: "Tom Preston-Werner"}}
toml_string = TomlElixir.encode(data)

Sample output:

"title = \"TOML Example\"\n\n[owner]\nname = \"Tom Preston-Werner\"\n"

Deep Dive

TOML was created by Tom Preston-Werner, co-founder of GitHub, for use in configuration files. It’s designed to be more straightforward than XML and more concise than YAML while maintaining consistency.

Alternatives include JSON, YAML, and INI files, each with their trade-offs in human readability and data structure compatibility. TOML excels in clearly representing tabular data and nested grouping of data.

In Elixir, TOML handling depends on decoding and encoding libraries, which transform TOML strings into Elixir maps and vice versa. Parsing works by matching TOML’s syntax rules and converting them into Elixir’s data types. Encoding does the opposite by mapping Elixir’s data types back to valid TOML syntax.

See Also