TOML (std::toml)
En esta página
std::toml lee y escribe el formato TOML — el mismo usado en Cargo.toml y
pyproject.toml. Las secciones [tabla] se convierten en sub-mapas; los
tipos se expresan de forma inequívoca.
Parse
toml.parse(cadena) convierte texto TOML en un mapa Zolo. Las secciones se
convierten en sub-mapas:
Escalares de nivel raíz y sección [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 a texto TOML. El orden de las claves puede
variar, pero el round-trip preserva los valores:
Serialización y verificación 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 Compatibles
TOML distingue string, entero, float, bool y array sin ambigüedad — a
diferencia de YAML, que puede interpretar "0.1.0" como número:
String, int, float, bool y array en el mismo 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
Consulta también