TOML 1.0.0 for Ant: read a document into a type you name, and write a value back.
ant add github.com/ant-src/tomlName the shape of the document as a type and read it with Toml.decode. A field the document leaves
out takes its default:
import "toml" Toml
type Server = { host: string, port: int = 8080 }
type Config = { title: string, debug: bool = false, servers: Array[Server] }
let text = `title = "demo"
[[servers]]
host = "alpha"
[[servers]]
host = "beta"
port = 9090
`
let config = try Toml.decode(text, type(Config))
println(config)Config{ title: "demo", debug: false, servers: [Server{ host: "alpha", port: 8080 }, Server{ host: "beta", port: 9090 }] }
Nothing is converted to make the text fit, and an error names the key:
Toml.decode("host = \"a\"\nport = \"80\"", type(Server))
// Err{ error: Error{ message: "port: want int, got \"80\"", kind: null } }Pass rejectUnknown: true to make a key that names no field an error too.
Toml.encode writes a value of a record or map type. Toml.decode at the same type reads it back:
let config = Config{ title: "demo", servers: [Server{ host: "alpha" }] }
print(Toml.encode(type(Config), config))title = "demo"
debug = false
[[servers]]
host = "alpha"
port = 8080Toml.parse reads a document whose shape the program does not name into a Toml.Table, and
Toml.stringify writes one:
let doc = try Toml.parse(`[package]
name = "ant"
released = 2026-11-08
`)
let pkg = doc["package"]
if pkg is Map {
println(pkg["released"])
}2026-11-08
| TOML | Ant |
|---|---|
| String | string |
| Integer | int, or any integer type that holds it |
| Float | float64 or float32 |
| Boolean | bool |
| Offset date-time | Time.ZonedDateTime or Time.Instant |
| Local date-time | Time.PlainDateTime |
| Local date | Time.PlainDate |
| Local time | Time.PlainTime |
| Array | Array[T] or Set[T] |
| Table | A record type, or Map[string, T] |
- An integer also fits a float type when the float holds it exactly.
- TOML has no null. A field whose type allows null and that has no default is null when its key is
missing, and
encodeleaves out a field that is null. - A union is read by the kind of the value, so
int | stringtakes1and"one". A union of two types that read one kind, such as two records, has no TOML form. - A named type, such as
type UserId = string, reads and writes as its base type. - An offset date-time keeps its offset as the zone of the
Time.ZonedDateTime.encodewrites aTime.Instantin UTC. - A fraction of a second past nine digits is dropped.