Skip to content

TOML (std::toml)

On this page

std::toml reads and writes the TOML format — the same format used in Cargo.toml and pyproject.toml. [table] sections become sub-maps; types are expressed unambiguously.

Parse

toml.parse(string) converts TOML text into a Zolo map. Sections become sub-maps:

Root-level scalars and a [server] section as a sub-map.

01-parse.zolo
Playground
// Feature: toml.parse — converts a TOML string into a map

// When to use: read configurations (Cargo.toml, pyproject.toml etc.).

// Note: for inline TOML, plain strings work — TOML does not use `{`/`}`

// at the top level, so there is no clash with interpolation.


use std::toml

let raw = "name = \"zolo\"\nversion = \"0.1.0\"\ndebug = true"
let cfg = toml.parse(raw)

print(cfg["name"])  // expected: zolo

print(cfg["version"])  // expected: 0.1.0

print(cfg["debug"])  // expected: true


// Sections become sub-maps.

let raw2 = "[server]\nhost = \"localhost\"\nport = 8080"
let cfg2 = toml.parse(raw2)
print(cfg2["server"]["host"])  // expected: localhost

print(cfg2["server"]["port"])  // expected: 8080

Stringify

toml.stringify(map) serialises to TOML text. Key order may vary, but the round-trip preserves the values:

Serialisation and round-trip verification.

02-stringify.zolo
Playground
// Feature: toml.stringify — serializes a map into a TOML string

// When to use: generate a configuration file programmatically.


use std::toml

let cfg = #{
  name: "zolo",
  version: "0.1.0",
  debug: false,
}

let out = toml.stringify(cfg)
print(out)

// expected (key order may vary):

// name = "zolo"

// version = "0.1.0"

// debug = false


// Round-trip: parse(stringify(x)) == x for scalar values.

let back = toml.parse(out)
print(back["name"])  // expected: zolo

print(back["debug"])  // expected: false

Supported Types

TOML distinguishes string, integer, float, bool and array without ambiguity — unlike YAML, which may interpret "0.1.0" as a number:

String, int, float, bool and array in the same TOML document.

03-types.zolo
Playground
// Feature: toml — supported types (string, int, float, bool, array)

// When to use: TOML expresses primitive types unambiguously — useful to

// distinguish strings from numbers, unlike YAML/JSON.


use std::toml

let raw = "name = \"app\"\ncount = 42\nrate = 3.14\nactive = true\ntags = [\"a\", \"b\", \"c\"]"
let cfg = toml.parse(raw)

print(cfg["name"])  // expected: app

print(cfg["count"])  // expected: 42

print(cfg["rate"])  // expected: 3.14

print(cfg["active"])  // expected: true

print(cfg["tags"][0])  // expected: a

print(cfg["tags"][2])  // expected: c

Search Zolo

9 results

enespt-br