TOML for MoonBit, at the edition the file was written in, with a reading that keeps the document as it was.
moon add moonbitstack/moontomllet src = "title = \"field notes\"\nbeds = 4\n\n[soil]\nkind = \"loam\"\nph = 6.5\n"
let doc = @moontoml.loads(src[:])
assert_eq(
@moontoml.dumps(doc),
"title = \"field notes\"\nbeds = 4\n\n[soil]\nkind = \"loam\"\nph = 6.5\n",
)| TOML | loads | decode | Example |
|---|---|---|---|
| string | String | String(String) | "loam" |
| integer | Number | Integer(Int64) | 42, 0x2A, 0o52, 0b101010 |
| float | Number | Float(Real) | 1.5, 5e+22, inf, nan |
| boolean | True / False | Boolean(Bool) | true |
| date-time | String | Datetime(Moment) | 1979-05-27T07:32:00Z |
| array | Array | Array(Array[Value]) | [1, 2, 3] |
| table | Object | Table(Map[String, Value]) | { x = 1 } |
let doc = @moontoml.decode_doc("beds = 4 # the ones that get sun\nsoil = \"loam\"\n")
assert_eq(doc.set(["beds"], @moontoml.Value::integer(6)), Some(true))
// the comment and the order are the source; only the value moved
assert_eq(doc.render(), "beds = 6 # the ones that get sun\nsoil = \"loam\"\n")// 0.4 has no dotted keys, no hex or binary integers, no inf, and only the offset date-time.
let src = "a.b = 1\n"
let refused = try {
let _ = @v04.loads(src[:])
false
} catch {
_ => true
}
assert_eq(refused, true)
// 0.5 adds dotted keys, prefixed integers, inf and nan, and the local date-time kinds.
assert_eq(@v05.loads("a.b = 1\n"[:]).stringify(), "{\"a\":{\"b\":1}}")
// 1.1 lets an inline table span lines and end with a comma.
assert_eq(
@v110.loads("p = { x = 1,\n y = 2,\n}"[:]).stringify(),
"{\"p\":{\"x\":1,\"y\":2}}",
)| Edition | The grammar |
|---|---|
| moonbitstack/moontoml | 1.0.0 — dotted keys, prefixed integers, mixed-type arrays, one-line inline tables |
| …/moontoml/v110 | 1.1 — inline tables that span lines with a trailing comma, \x and \e escapes, a time that may drop its seconds |
| …/moontoml/v05 | 0.5 — dotted keys, prefixed integers, inf/nan, the local date-time kinds; arrays are one kind |
| …/moontoml/v04 | 0.4 — no dotted keys, no prefixed integers, no inf/nan, only the offset date-time |
let doc = @moontoml.loads("title = \"field notes\"\nbeds = 4"[:])
guard doc is Object(root) else { abort("a document is a table") }
let title = match root.get("title") {
Some(String(s)) => s
_ => "?"
}
let beds = match root.get("beds") {
Some(Number(n, ..)) => n.to_int()
_ => 0
}
assert_eq((title, beds), ("field notes", 4))let doc = @moontoml.decode("port = 12000\nwhen = 1979-05-27\nname = \"1979-05-27\"\n")
guard doc is Table(fields) else { abort("a document is a table") }
guard fields.get("when") is Some(Datetime(m)) else { abort("a date") }
assert_eq(m.to_string(), "1979-05-27")let doc = @moontoml.loads("b = 1\na = 2"[:])
assert_eq(@moontoml.dumps(doc), "b = 1\na = 2\n")
assert_eq(@moontoml.dumps(doc, sort=true), "a = 2\nb = 1\n")| Suite | Edition | Documents | Passing |
|---|---|---|---|
| valid, read as the tree the suite expects | 1.0.0 | 205 | 205 |
| valid, written by the library and read back | 1.0.0 | 205 | 205 |
| invalid, refused | 1.0.0 | 474 | 474 |
| valid, read as the tree the suite expects | 1.1.0 | 214 | 214 |
| valid, written by the library and read back | 1.1.0 | 214 | 214 |
| invalid, refused | 1.1.0 | 467 | 467 |
| valid, read into the Doc and written back byte for byte | 1.0.0 | 205 | 205 |
| valid, read into the Doc and written back byte for byte | 1.1.0 | 214 | 214 |
| Example | What it shows |
|---|---|
| src/examples/basic | read, change one value, write back — once as a tree, once as the document kept with its comments and order |
| src/examples/integration | a site config read into a struct, changed, written back sorted, and a refusal reported by line |
moon run src/examples/basic
moon run src/examples/integrationlet depth : IntInstall
Download zipTOML for MoonBit, at the edition the file was written in, with a reading that keeps the document as it was.