Edit toml files while preserving whitespace and formatting from Lua.
The parse function creates a table with metatable,
which can be converted back to toml (preserving comments)
using tostring:
local toml_content = [[ [rocks] # Some comment "toml-edit" = "1.0.0" ]] local toml_edit = require("toml_edit") local toml_tbl = toml_edit.parse(toml_content) toml_tbl.rocks["toml-edit"] = "2.0.0" print(tostring(toml_tbl)) -- outputs: -- [rocks] -- # Some comment -- "toml-edit" = "2.0.0"
The parse_as_tbl function parses toml as a regular lua table:
local toml_content = [[ [rocks] "toml-edit" = "1.0.0" ]] local toml_edit = require("toml_edit") local lua_tbl = toml_edit.parse_as_tbl(toml_content) print(tostring(toml_tbl)) -- outputs: table: 0x7ff975807668
Tip
- Use
parsewhen you need to modify the toml, and you are accessing or setting fields by name. - Use
parse_as_tblwhen you need to perform operations that don't access fields by name (e.g. iterating over key/value pairs).
Using Nix:
nix flake check -LUsing luarocks:
mkdir luarocks luarocks make --tree=luarocks luarocks test
Note
You may need to luarocks install --local luarocks-build-rust-mlua.