Toml
TOML v1.0 parser (pure March, no FFI)
Supports: strings (basic, literal, multiline), integers, floats, booleans, arrays, inline tables, table headers [x], array-of-tables [[x]], dotted keys, quoted keys, comments.
Public API: Toml.parse(src) : Result(TomlValue, TomlError) Toml.parse_exn(src) : TomlValue (panics on error) Toml.to_string(v) : String Toml.get(v, key) : Option(TomlValue) Toml.get_in(v, keys) : Option(TomlValue) Toml.get_str(v, key) : Option(String) Toml.get_int(v, key) : Option(Int) Toml.get_float(v, key) : Option(Float) Toml.get_bool(v, key) : Option(Bool) Toml.get_table(v, key) : Option(TomlValue) Toml.get_array(v, key) : Option(List(TomlValue))
Types
Functions
Look up a top-level key in a TOML table value.
march> let table = Toml.parse_exn("port = 8080")
march> Toml.get(table, "port")
Some(TInt(8080))
march> let table = Toml.parse_exn("port = 8080")
march> Toml.get(table, "host")
NoneGet an array value by key as a list of its elements, or None if the key is absent or its value is not an array.
Examples
march> Toml.get_array(Toml.parse_exn("tags = [\"a\", \"b\"]"), "tags")
Some([TStr("a"), TStr("b")])
march> Toml.get_array(Toml.parse_exn("tags = \"a\""), "tags")
NoneGet a boolean value by key, or None if the key is absent or its value is not a boolean.
Examples
march> Toml.get_bool(Toml.parse_exn("debug = true"), "debug")
Some(true)
march> Toml.get_bool(Toml.parse_exn("debug = true"), "verbose")
NoneGet a float value by key, or None if the key is absent or its value is not a float (an integer such as 8080 is not converted).
Examples
march> Toml.get_float(Toml.parse_exn("ratio = 0.5"), "ratio")
Some(0.5)
march> Toml.get_float(Toml.parse_exn("port = 8080"), "port")
NoneLook up a nested key path.
march> let config = Toml.parse_exn("[server]\nhost = \"localhost\"\nport = 8080")
march> Toml.get_in(config, ["server", "host"])
Some(TStr("localhost"))
march> let config = Toml.parse_exn("[server]\nhost = \"localhost\"\nport = 8080")
march> Toml.get_in(config, ["server", "user"])
NoneGet an integer value by key, or None if the key is absent or its value is not an integer (a float such as 0.5 is not converted).
Examples
march> Toml.get_int(Toml.parse_exn("port = 8080"), "port")
Some(8080)
march> Toml.get_int(Toml.parse_exn("ratio = 0.5"), "ratio")
NoneGet a string value by key, or None if the key is absent or its value is not a string (no conversion is attempted). None when v is not a table.
Examples
march> Toml.get_str(Toml.parse_exn("name = \"app\""), "name")
Some("app")
march> Toml.get_str(Toml.parse_exn("port = 8080"), "port")
NoneGet a table value (a [section] or inline table) by key, still wrapped as TTable, or None if the key is absent or its value is not a table.
Examples
march> Toml.get_table(Toml.parse_exn("[db]\nurl = \"pg\""), "db")
Some(TTable([("url", TStr("pg"))]))
march> Toml.get_table(Toml.parse_exn("name = \"app\""), "name")
NoneParse a TOML document string. Returns Ok(TomlValue) on success or Err(TomlError(message, line, col)) on failure.
march> Toml.parse("port = 8080")
Ok(TTable([("port", TInt(8080))]))
march> Toml.parse("port = ")
Err(TomlError("expected value, got end of input", 1, 7))Parse TOML like parse, returning the value directly. Panics with TOML parse error at LINE:COL: MESSAGE if the document is invalid, so use it only for trusted, known-good input such as embedded literals.
Examples
march> Toml.parse_exn("x = 1\ny = 'lit'")
TTable([("x", TInt(1)), ("y", TStr("lit"))])Convert a TomlValue to a compact, single-line string representation: tables are rendered as inline tables { k = v, ... } (including the top-level document), arrays as [a, b]. Strings are wrapped in double quotes WITHOUT escaping, and TNull renders as null, so the output is meant for display and is not guaranteed to be valid TOML.
Examples
march> Toml.to_string(Toml.parse_exn("port = 8080"))
"{ port = 8080 }"
march> Toml.to_string(Toml.parse_exn("a = [1, 2]\n[t]\nb = true"))
"{ a = [1, 2], t = { b = true } }"