Skip to content

toml

TOML codec. See the formats overview for the shared codec shape and encoding rules common to every format.

Examples

Generate a config:

lua
local rb = require("rootbeer")

rb.toml.write("~/.config/myapp/config.toml", {
  app = {
    name = "rootbeer",
    workers = 4,
  },
  features = { "fast", "secure" },
})

Read and patch:

lua
local cfg = rb.toml.read("~/.cargo/config.toml")
cfg.build = cfg.build or {}
cfg.build.jobs = 8
rb.toml.write("~/.cargo/config.toml", cfg)

Notes

  • Arrays of scalars use inline syntax (features = ["fast", "secure"]); arrays of tables use [[array]] syntax.
  • TOML datetimes are decoded as strings — bring your own date library if you need to manipulate them.

API

rootbeer.toml.decode(s)

Parses a TOML string into a Lua table. TOML datetimes are returned as strings (use a date library if you need richer types).

Parameters

sstring
The TOML-encoded string.

Returns

table — The decoded value.

rootbeer.toml.encode(t)

Serializes a Lua table to a TOML string.

Parameters

ttable
The table to serialize. Top-level value must be a table.

Returns

string — The TOML-encoded string (no trailing newline).

rootbeer.toml.read(path)

Reads and decodes a TOML file. Equivalent to decode(slurp(path)). Path supports ~ expansion and is resolved against the script directory. This call is synchronous — the file must exist at plan time.

Parameters

pathstring
The file to read.

Returns

table — The decoded value.

rootbeer.toml.write(path, t)

Encodes a table and writes it to a file. Equivalent to rb.file(path, encode(t)). The write is deferred until the apply stage. A trailing newline is always added so the file is well-formed.

Parameters

pathstring
The destination file path.
ttable
The table to serialize.