toml
Read and write TOML 1.0, the format of many configuration files. A document is read into a json.Json value, the same tree the json and yaml modules use, so its accessors (get, at, string, int, …) work here too and data moves freely between the three formats.
import toml
let text = "title = \"Demo\"\n\n[server]\nhost = \"example.com\"\nports = [8000, 8001]\n\n[[users]]\nname = \"ada\"\n\n[[users]]\nname = \"grace\"\n"let conf = try toml.parse(&text)let server = conf.get(&"server").unwrap()print(server.get(&"host").unwrap().string())print(server.get(&"ports").unwrap().at(1).unwrap().int())for user in conf.get(&"users").unwrap().list().unwrap() { print(user.get(&"name").unwrap().string().unwrap_or("?"))}Some("example.com")Some(8001)adagraceHow TOML values arrive:
| TOML | json.Json |
|---|---|
| table, inline table | Object, keys in the order written |
| array, array of tables | List |
| string (all four kinds) | Text |
integer (also 0x, 0o, 0b) |
Num, as decimal text |
| float | Num; inf, -inf and nan become Text, since JSON has no such numbers |
| boolean | Bool |
| date, time, date-time | Text, exactly as written |
The whole of TOML 1.0 is read: dotted keys, [table] and [[array.of.tables]] headers, every string form and escape, underscores in numbers, and comments. What the spec forbids is a TomlError with its line: defining a key or table twice, adding to an inline table after it is closed, integers beyond 64 bits, leading zeros, unknown escapes, unclosed strings and arrays, and nesting deeper than 256 levels.
Write a document
Section titled “Write a document”toml.text writes a json.Json object as TOML: plain values first, then [tables], then [[arrays of tables]].
import jsonimport toml
let doc = json.Json.Object([ ("name", json.Json.Text("brass")), ("tags", json.Json.List([json.Json.Text("c"), json.Json.Text("fast")])), ("build", json.Json.Object([("release", json.Json.Bool(true)), ("jobs", json.num(8))])),])print(try toml.text(&doc))name = "brass"tags = ["c", "fast"]
[build]release = truejobs = 8Reference
Section titled “Reference”import tomlFunctions
Section titled “Functions”pub fn parse(text: &Str) -> json.Json ! TomlErrorReads a TOML 1.0 document; dates and times stay as text, integers become decimal numbers.
pub fn text(value: &json.Json) -> Str ! TomlErrorThe top level must be an object; null, anywhere, has no TOML form and is an error.
The top level must be an object, and TOML has no null, so a Null anywhere is an error that names its place (null has no TOML form, at `a.b[1]` ). A key that appears twice in one object is an error too. Errors from text have line 0.
Dates were read as text, so they are written back as quoted strings: 2026-11-26 becomes "2026-11-26". Keep the original file if the distinction matters to another program.
Plain values are written before [tables], so keys of an object may come back in another order; TOML tables have no order, and every key keeps its value.
TomlError
Section titled “TomlError”pub struct TomlError { pub line: i64 pub message: Str}