TOML for MoonBit, 1.0.0 and earlier.
moon add moonbitstack/moontoml///|
fn main raise {
let src =
#|title = "field notes"
#|beds = 4
#|
#|[soil]
#|kind = "loam"
#|ph = 6.5
let doc = @moontoml.loads(src[:])
println(@moontoml.dumps(doc))
}title = "field notes"
beds = 4
[soil]
kind = "loam"
ph = 6.5| 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 } |
///|
fn main raise {
let src =
#|title = "field notes"
#|beds = 4
let doc = @moontoml.loads(src[:])
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
}
println("\{title}, \{beds} beds")
}field notes, 4 beds///|
fn main raise {
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") }
println(m.to_string())
println(@moontoml.encode(doc))
}1979-05-27
port = 12000
when = 1979-05-27
name = "1979-05-27"///|
fn main raise {
let doc = @moontoml.loads("b = 1\na = 2\n"[:])
println(@moontoml.dumps(doc)) // b = 1, then a = 2
println(@moontoml.dumps(doc, sort=true)) // a = 2, then b = 1
}| Suite | Documents | Passing |
|---|---|---|
| valid, read as the tree the suite expects | 205 | 205 |
| valid, written by the library and read back | 205 | 205 |
| invalid, refused | 474 | 474 |
| Example | What it shows |
|---|---|
| src/examples/basic | the four steps: load, read, edit, dump |
| 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, 1.0.0 and earlier.