tom

A pure Gleam TOML parser!

import tom

const config = "
  [person]
  name = \"Lucy\"
  is_cool = true
"

pub fn main() {
  // Parse a string of TOML
  let assert Ok(parsed) = tom.parse(config)

  // Now you can work with the data directly, or you can use the `get_*`
  // functions to retrieve values.

  tom.get_string(parsed, ["person", "name"])
  // -> Ok("Lucy")

  let is_cool = tom.get_bool(parsed, ["person", "is_cool"])
  // -> Ok(True)
}

Types

pub type Date {
  DateValue(year: Int, month: Int, day: Int)
}

Constructors

  • DateValue(year: Int, month: Int, day: Int)
pub type DateTime {
  DateTimeValue(date: Date, time: Time, offset: Offset)
}

Constructors

  • DateTimeValue(date: Date, time: Time, offset: Offset)

An error that can occur when retrieving a value from a TOML document with one of the get_* functions.

pub type GetError {
  NotFound(key: List(String))
  WrongType(key: List(String), expected: String, got: String)
}

Constructors

  • NotFound(key: List(String))

    There was no value at the given key.

  • WrongType(key: List(String), expected: String, got: String)

    The value at the given key was not of the expected type.

A number of any kind, returned by the get_number function.

pub type Number {
  NumberInt(Int)
  NumberFloat(Float)
  NumberInfinity(Sign)
  NumberNan(Sign)
}

Constructors

  • NumberInt(Int)
  • NumberFloat(Float)
  • NumberInfinity(Sign)
  • NumberNan(Sign)
pub type Offset {
  Local
  Offset(direction: Sign, hours: Int, minutes: Int)
}

Constructors

  • Local
  • Offset(direction: Sign, hours: Int, minutes: Int)

An error that can occur when parsing a TOML document.

pub type ParseError {
  Unexpected(got: String, expected: String)
  KeyAlreadyInUse(key: List(String))
}

Constructors

  • Unexpected(got: String, expected: String)

    An unexpected character was encountered when parsing the document.

  • KeyAlreadyInUse(key: List(String))

    More than one items have the same key in the document.

pub type Sign {
  Positive
  Negative
}

Constructors

  • Positive
  • Negative
pub type Time {
  TimeValue(
    hour: Int,
    minute: Int,
    second: Int,
    millisecond: Int,
  )
}

Constructors

  • TimeValue(hour: Int, minute: Int, second: Int, millisecond: Int)

A TOML document.

pub type Toml {
  Int(Int)
  Float(Float)
  Infinity(Sign)
  Nan(Sign)
  Bool(Bool)
  String(String)
  Date(Date)
  Time(Time)
  DateTime(DateTime)
  Array(List(Toml))
  ArrayOfTables(List(Dict(String, Toml)))
  Table(Dict(String, Toml))
  InlineTable(Dict(String, Toml))
}

Constructors

  • Int(Int)
  • Float(Float)
  • Infinity(Sign)

    Infinity is a valid number in TOML but Gleam does not support it, so this variant represents the infinity values.

  • Nan(Sign)

    NaN is a valid number in TOML but Gleam does not support it, so this variant represents the NaN values.

  • Bool(Bool)
  • String(String)
  • Date(Date)
  • Time(Time)
  • DateTime(DateTime)
  • Array(List(Toml))
  • ArrayOfTables(List(Dict(String, Toml)))
  • Table(Dict(String, Toml))
  • InlineTable(Dict(String, Toml))

Functions

pub fn get(
  toml: Dict(String, Toml),
  key: List(String),
) -> Result(Toml, GetError)

Get a value of any type from a TOML document.

Examples

let assert Ok(parsed) = parse("a.b.c = 1")
get(parsed, ["a", "b", "c"])
// -> Ok(Int(1))
pub fn get_array(
  toml: Dict(String, Toml),
  key: List(String),
) -> Result(List(Toml), GetError)

Get an array from a TOML document.

Examples

let assert Ok(parsed) = parse("a.b.c = [1, 2]")
get_array(parsed, ["a", "b", "c"])
// -> Ok([Int(1), Int(2)])
pub fn get_bool(
  toml: Dict(String, Toml),
  key: List(String),
) -> Result(Bool, GetError)

Get a bool from a TOML document.

Examples

let assert Ok(parsed) = parse("a.b.c = true")
get_bool(parsed, ["a", "b", "c"])
// -> Ok(True)
pub fn get_date(
  toml: Dict(String, Toml),
  key: List(String),
) -> Result(Date, GetError)

Get a date from a TOML document.

Examples

let assert Ok(parsed) = parse("a.b.c = 1979-05-27")
get_date(parsed, ["a", "b", "c"])
// -> Ok("1979-05-27")
pub fn get_date_time(
  toml: Dict(String, Toml),
  key: List(String),
) -> Result(DateTime, GetError)

Get a date-time from a TOML document.

Examples

let assert Ok(parsed) = parse("a.b.c = 1979-05-27T07:32:00")
get_date_time(parsed, ["a", "b", "c"])
// -> Ok("1979-05-27T07:32:00")
pub fn get_float(
  toml: Dict(String, Toml),
  key: List(String),
) -> Result(Float, GetError)

Get a float from a TOML document.

Examples

let assert Ok(parsed) = parse("a.b.c = 1.1")
get_float(parsed, ["a", "b", "c"])
// -> Ok(1.1)
pub fn get_int(
  toml: Dict(String, Toml),
  key: List(String),
) -> Result(Int, GetError)

Get an int from a TOML document.

Examples

let assert Ok(parsed) = parse("a.b.c = 1")
get_int(parsed, ["a", "b", "c"])
// -> Ok(1)
pub fn get_number(
  toml: Dict(String, Toml),
  key: List(String),
) -> Result(Number, GetError)

Get a number of any kind from a TOML document. This could be an int, a float, a NaN, or an infinity.

Examples

let assert Ok(parsed) = parse("a.b.c = { d = inf }")
get_number(parsed, ["a", "b", "c"])
// -> Ok(NumberInfinity(Positive)))
pub fn get_string(
  toml: Dict(String, Toml),
  key: List(String),
) -> Result(String, GetError)

Get a string from a TOML document.

Examples

let assert Ok(parsed) = parse("a.b.c = \"ok\"")
get_string(parsed, ["a", "b", "c"])
// -> Ok("ok")
pub fn get_table(
  toml: Dict(String, Toml),
  key: List(String),
) -> Result(Dict(String, Toml), GetError)

Get a table from a TOML document.

Examples

let assert Ok(parsed) = parse("a.b.c = { d = 1 }")
get_table(parsed, ["a", "b", "c"])
// -> Ok(dict.from_list([#("d", Int(1))]))
pub fn get_time(
  toml: Dict(String, Toml),
  key: List(String),
) -> Result(Time, GetError)

Get a time from a TOML document.

Examples

let assert Ok(parsed) = parse("a.b.c = 07:32:00")
get_time(parsed, ["a", "b", "c"])
// -> Ok("07:32:00")
pub fn parse(
  input: String,
) -> Result(Dict(String, Toml), ParseError)
Search Document