Skip to content

yaml

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

Examples

Materialize a config:

lua
local rb = require("rootbeer")

rb.yaml.write("~/.config/myapp/config.yaml", {
  theme = "dracula",
  plugins = { "git", "format-on-save" },
})

Read and patch:

lua
local cfg = rb.yaml.read("~/.config/myapp/config.yaml")
cfg.theme = "tokyonight"
rb.yaml.write("~/.config/myapp/config.yaml", cfg)

Notes

  • Mapping keys are always emitted as strings; non-string source keys are stringified.
  • Tagged YAML values (e.g. !!str) decode transparently to the underlying scalar.

API

rootbeer.yaml.decode(s)

Parses a YAML string into a Lua table. Mapping keys that are not already strings are stringified.

Parameters

sstring
The YAML-encoded string.

Returns

table — The decoded value.

rootbeer.yaml.encode(t)

Serializes a Lua table to a YAML string.

Parameters

ttable
The table to serialize.

Returns

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

rootbeer.yaml.read(path)

Reads and decodes a YAML 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.yaml.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.