Module
tome
src/tome.pookaRead TOML 1.0, the configuration format that tries hard to be obvious.
A document becomes a Toml value: a table at the top, holding strings,
numbers, booleans, dates, arrays and more tables. Take it apart with
match, or with the helpers at the end of this module.
import tome
main() {
let text = "name = \"night_library\"\n\n[owner]\nname = \"Ada\"\n"
let assert Ok(config) = tome.parse(text)
echo tome.path(config, ["owner", "name"])
}
Tables keep their keys in the order they were written.
API reference
- classToml
- classTString
- classTInt
- classTFloat
- classTInfinity
- classTNan
- classTBool
- classTDateTime
- classTDate
- classTTime
- classTArray
- classTTable
- classTomlError
- functionparse
- functionparse_bytes
- functiondescribe
- functionget
- functionpath
- functionat
- functionas_string
- functionas_int
- functionas_float
- functionas_bool
- functionas_datetime
- functionas_list
- functionas_entries
class Toml
Any TOML value.
class TString(value: String) is Toml
A string, with its escapes already resolved.
value: String
String
fn(String) -> tome.TStringclass TInt(value: Int) is Toml
A whole number, written in decimal, hexadecimal, octal or binary.
value: Int
Int
fn(Int) -> tome.TIntclass TFloat(value: Float) is Toml
A number with a fraction or an exponent.
value: Float
Float
fn(Float) -> tome.TFloatclass TInfinity(positive: Bool) is Toml
inf, +inf or -inf.
positive: Bool
Bool
fn(Bool) -> tome.TInfinityclass TNan is Toml
nan, the float that is not a number.
class TBool(value: Bool) is Toml
true or false.
value: Bool
Bool
fn(Bool) -> tome.TBoolclass TDateTime(value: String) is Toml
A date with a time, with or without an offset, as it was written:
1979-05-27T07:32:00Z. A space between the date and the time becomes T.
value: String
String
fn(String) -> tome.TDateTimeclass TDate(value: String) is Toml
A date on its own: 1979-05-27.
value: String
String
fn(String) -> tome.TDateclass TTime(value: String) is Toml
A time on its own: 07:32:00.
value: String
String
fn(String) -> tome.TTimeclass TArray(items: List(Toml)) is Toml
An array. An array of tables, [[name]], is an array whose items are
tables.
items: List(Toml)
List(tome.Toml)
fn(List(tome.Toml)) -> tome.TArrayclass TTable(entries: List(#(String, Toml))) is Toml
A table, as its entries in the order they were written.
entries: List(#(String, Toml))
List(#(String, tome.Toml))
fn(List(#(String, tome.Toml))) -> tome.TTableclass TomlError(line: Int, message: String)
Why a text is not TOML, and the line where the trouble was noticed.
line: Int
Intmessage: String
String
fn(Int, String) -> tome.TomlErrorparse(text: String) -> Result(Toml, TomlError)
fn(String) -> Result(tome.Toml, tome.TomlError)Read a TOML document. The result is always a TTable.
import tome
assert tome.parse("answer = 42") == Ok(tome.TTable([#("answer", tome.TInt(42))]))
assert tome.parse("answer = ") == Error(tome.TomlError(1, "a value is missing"))
parse_bytes(input: BitArray) -> Result(Toml, TomlError)
fn(BitArray) -> Result(tome.Toml, tome.TomlError)Read a document from bytes, rejecting invalid UTF-8 before opening a table. This is useful with file.read_bytes, where decoding has not happened yet.
import tome
assert tome.parse_bytes(<<110, 32, 61, 32, 49>>) == tome.parse("n = 1")
let assert Error(_) = tome.parse_bytes(<<255>>)
describe(problem: TomlError) -> String
fn(tome.TomlError) -> StringA sentence for the person who wrote the file.
import tome
assert tome.describe(tome.TomlError(3, "a value is missing")) == "line 3: a value is missing"
get(value: Toml, key: String) -> Option(Toml)
The value under a key, if this is a table that has it.
import tome
let assert Ok(config) = tome.parse("name = \"Ada\"")
assert tome.get(config, "name") == Some(tome.TString("Ada"))
assert tome.get(config, "wings") == None
path(value: Toml, keys: List(String)) -> Option(Toml)
Follow several keys, one table inside another.
import tome
let assert Ok(config) = tome.parse("[owner]\nname = \"Ada\"")
assert tome.path(config, ["owner", "name"]) == Some(tome.TString("Ada"))
at(value: Toml, index: Int) -> Option(Toml)
The item at a position, counting from zero, if this is an array that has it. Negative and out-of-range positions return None.
let assert Ok(config) = tome.parse("birds = ['owl', 'crow']")
let assert Some(birds) = tome.get(config, "birds")
assert tome.at(birds, 1) == Some(tome.TString("crow"))
assert tome.at(birds, -1) == None
as_string(value: Toml) -> Option(String)
fn(tome.Toml) -> Option(String)The text, if this is a string.
assert tome.as_string(tome.TString("owl")) == Some("owl")
assert tome.as_string(tome.TInt(2)) == None
as_int(value: Toml) -> Option(Int)
fn(tome.Toml) -> Option(Int)The number, if this is a whole number.
assert tome.as_int(tome.TInt(42)) == Some(42)
as_float(value: Toml) -> Option(Float)
fn(tome.Toml) -> Option(Float)The number, if this is a float. Whole numbers are not floats in TOML. Infinity and NaN have their own value classes; they do not become BEAM floats.
assert tome.as_float(tome.TFloat(1.5)) == Some(1.5)
assert tome.as_float(tome.TInt(1)) == None
as_bool(value: Toml) -> Option(Bool)
fn(tome.Toml) -> Option(Bool)The truth value, if this is true or false.
assert tome.as_bool(tome.TBool(True)) == Some(True)
as_datetime(value: Toml) -> Option(String)
fn(tome.Toml) -> Option(String)The text of a date, a time, or a date with a time. Fractional precision and offsets stay as written. A space between the date and time becomes T.
assert tome.as_datetime(tome.TDate("2024-02-29")) == Some("2024-02-29")
as_list(value: Toml) -> Option(List(Toml))
The items, if this is an array.
assert tome.as_list(tome.TArray([])) == Some([])
as_entries(value: Toml) -> Option(List(#(String, Toml)))
The entries, if this is a table, in their source order.
let assert Ok(config) = tome.parse("z = 1\na = 2")
let assert Some(entries) = tome.as_entries(config)
assert list.map(entries, fn(entry) { let #(key, _) = entry
key
}) == ["z", "a"]