A comprehensive TOML (Tom's Obvious, Minimal Language) parser for MoonBit with full support for TOML v1.0.0 specification including tables, arrays, dotted keys, and inline tables.
{
"deps": {
"tonyfettes/amtoml": "*"
}
}test "basic usage" {
let toml_input =
#|title = "TOML Example"
#|version = "1.0.0"
#|
#|[database]
#|server = "192.168.1.1"
#|ports = [8000, 8001, 8002]
#|enabled = true
#|
// Parse the TOML string
let result = @tonyfettes/amtoml.parse_toml(toml_input)
// Check if parsing succeeded
inspect(result is Ok(_), content="true")
// Access the parsed data
let table = result.unwrap()
inspect(table.get("title"), content=(
#|Some(String("TOML Example"))
))
inspect(table.get("version"), content=(
#|Some(String("1.0.0"))
))
// Access nested tables
match table.get("database") {
Some(TomlValue::Table(db)) => {
inspect(db.get("server"), content=(
#|Some(String("192.168.1.1"))
))
inspect(db.get("enabled"), content="Some(Boolean(true))")
}
_ => fail("Expected database table")
}
}test "toml value types" {
// String values
let str_val = TomlValue::String("hello")
inspect(str_val.to_string(), content="String(hello)")
// Integer values (Int64)
let int_val = TomlValue::Integer(42L)
inspect(int_val.to_string(), content="Integer(42)")
// Float values (Double)
let float_val = TomlValue::Float(3.14)
inspect(float_val.to_string(), content="Float(3.14)")
// Boolean values
let bool_val = TomlValue::Boolean(true)
inspect(bool_val.to_string(), content="Boolean(true)")
// Arrays
let arr_val = TomlValue::Array([
TomlValue::Integer(1L),
TomlValue::Integer(2L),
])
inspect(arr_val.to_string(), content="Array([Integer(1), Integer(2)])")
// Tables (Maps)
let table : Map[String, TomlValue] = {}
table["key"] = TomlValue::String("value")
let _table_val = TomlValue::Table(table)
}test "dotted keys example" {
let input =
#|name.first = "Tom"
#|name.last = "Preston-Werner"
#|
let result = @tonyfettes/amtoml.parse_toml(input)
inspect(result is Ok(_), content="true")
let table = result.unwrap()
match table.get("name") {
Some(TomlValue::Table(tbl)) => {
inspect(tbl.get("first"), content=(
#|Some(String("Tom"))
))
inspect(tbl.get("last"), content=(
#|Some(String("Preston-Werner"))
))
}
_ => fail("Expected name table")
}
}test "array of tables example" {
let input =
#|[[products]]
#|name = "Hammer"
#|sku = 738594937
#|
#|[[products]]
#|name = "Nail"
#|sku = 284758393
#|
let result = @tonyfettes/amtoml.parse_toml(input)
inspect(result is Ok(_), content="true")
let table = result.unwrap()
match table.get("products") {
Some(TomlValue::Array(arr)) => {
inspect(arr.length(), content="2")
match arr.get(0) {
Some(TomlValue::Table(product)) =>
inspect(product.get("name"), content=(
#|Some(String("Hammer"))
))
_ => fail("Expected product table")
}
}
_ => fail("Expected products array")
}
}test "inline tables example" {
let input =
#|point = { x = 1, y = 2 }
#|color = { r = 255, g = 128, b = 0 }
#|
let result = @tonyfettes/amtoml.parse_toml(input)
inspect(result is Ok(_), content="true")
let table = result.unwrap()
match table.get("point") {
Some(TomlValue::Table(tbl)) => {
inspect(tbl.get("x"), content="Some(Integer(1))")
inspect(tbl.get("y"), content="Some(Integer(2))")
}
_ => fail("Expected point table")
}
}test "error handling example" {
let input =
#|name = "Tom"
#|name = "Jerry" # Duplicate key!
#|
let result = @tonyfettes/amtoml.parse_toml(input)
inspect(result is Err(_), content="true")
// Error types include:
// - UnexpectedChar: Unexpected character in input
// - UnexpectedEof: Unexpected end of file
// - InvalidEscape: Invalid escape sequence
// - InvalidNumber: Invalid number format
// - InvalidKey: Invalid key format
// - InvalidValue: Invalid value format
// - DuplicateKey: Duplicate key in table
// - InvalidTable: Invalid table definition
}pub(all) suberror ParseError {
UnexpectedChar(Int, Char)
UnexpectedEof
InvalidEscape(Int, Char)
InvalidNumber(Int, String)
InvalidKey(Int, String)
InvalidValue(Int, String)
DuplicateKey(String)
InvalidTable(String)
}impl Eq for ParseErrorimpl Show for ParseErrortype Lexertype Parsertype TokenA comprehensive TOML (Tom's Obvious, Minimal Language) parser for MoonBit with full support for TOML v1.0.0 specification including tables, arrays, dotted keys, and inline tables.