TOML (std::toml)
Nesta página
std::toml lê e escreve o formato TOML — o mesmo usado em Cargo.toml e
pyproject.toml. Seções [tabela] tornam-se sub-mapas; tipos são expressos
de forma inequívoca.
Parse
toml.parse(string) converte texto TOML em mapa Zolo. Seções viram sub-mapas:
Scalares de nível raiz e seção [server] como sub-mapa.
01-parse.zolo
// 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(mapa) serializa para texto TOML. Ordem das chaves pode variar,
mas o round-trip preserva os valores:
Serialização e verificação de round-trip.
02-stringify.zolo
// 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
Tipos Suportados
TOML distingue string, inteiro, float, bool e array sem ambiguidade — ao
contrário de YAML que pode interpretar "0.1.0" como número:
String, int, float, bool e array no mesmo documento TOML.
03-types.zolo
// 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
Veja também