Skip to content

toml

TOML serialization and deserialization.

Types

TypeDescription
Data A value decoded from TOML.
Encodable A value that encodes as TOML.

Data = (Bool | Int | Float | Str | Array[Data] | Dict[Str, Data])

A value decoded from TOML.

Encodable = (Bool | Int | Float | Str | Sym | Array[Encodable] | Tuple[...Encodable] | Record[...{...Sym: Encodable}] | Dict[Str | Sym, Encodable])

A value that encodes as TOML.

Functions

decode toml -> Data

Parses a TOML string into a Do value.

TOML Value Do Type
boolean Bool
integer Int
float Float
string Str
array Array
table Dict

Full documents and bare TOML values such as numbers, arrays, and inline tables are accepted.

Parameters

NameTypeDescription
toml Str TOML input to parse.

Errors

ValueError if the input is invalid TOML or uses TOML datetime, date, or time values, which are not mapped by this module yet.

Example

assert_eq (decode "42") 42
assert_eq (decode "[1, 2, 3]") [1, 2, 3]

let doc = decode |
  title = "Example"
  [server]
  port = 8080

assert_eq $doc["title"] "Example"
assert_eq $doc["server"]["port"] 8080
Open in playground

encode value -> Str

Serializes a Do value to a TOML string.

Do Type TOML Value
Bool boolean
Int integer
Float float
Str string
Sym string
Array array
Tuple array
Dict table
Record table

Top-level dicts and records serialize as documents, while other values serialize as values.

Parameters

NameTypeDescription
value Encodable The value to serialize.

Errors

TypeError if a value is not TOML-representable, including nil, binary data, custom objects, or tables with non-string and non-symbol keys.

Example

assert_eq (encode 42) "42"
assert_eq (decode $ encode [1, 2, 3]) [1, 2, 3]

let doc = encode {"enabled": true, "name": "alice"}
assert_eq (decode doc) {"enabled": true, "name": "alice"}