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 dictionary 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 as_array(toml: Toml) -> Result(List(Toml), GetError)

Get an array from a TOML document.

Examples

as_array(Array([]))
// -> Ok([])
as_array(Int(1))
// -> Error(WrongType([], "Array", "Int"))
pub fn as_bool(toml: Toml) -> Result(Bool, GetError)

Get a bool from a TOML document.

Examples

as_bool(Bool(true))
// -> Ok(true)
as_bool(Int(1))
// -> Error(WrongType([], "Bool", "Int"))
pub fn as_date(toml: Toml) -> Result(Date, GetError)

Get a date from a TOML document.

Examples

as_date(Date(date))
// -> Ok(date)
as_date(Int(1))
// -> Error(WrongType([], "Date", "Int"))
pub fn as_date_time(toml: Toml) -> Result(DateTime, GetError)

Get a datetime from a TOML document.

Examples

as_date_time(DateTime(datetime))
// -> Ok(datetime)
as_date_time(Int(1))
// -> Error(WrongType([], "DateTime", "Int"))
pub fn as_float(toml: Toml) -> Result(Float, GetError)

Get a float from a TOML document.

Examples

as_float(Float(1.5))
// -> Ok(1.5)
as_float(Int(1))
// -> Error(WrongType([], "Float", "Int"))
pub fn as_int(toml: Toml) -> Result(Int, GetError)

Get a int from a TOML document.

Examples

as_int(Int(1))
// -> Ok(1)
as_int(Float(1.4))
// -> Error(WrongType([], "Int", "Float"))
pub fn as_number(toml: Toml) -> Result(Number, GetError)

Get a number (int or float) from a TOML document.

Examples

as_number(Int(1))
// -> Ok(NumberInt(1))
as_number(Float(1.5))
// -> Ok(NumberFloat(1.5))
as_number(Bool(true))
// -> Error(WrongType([], "Number", "Bool"))
pub fn as_string(toml: Toml) -> Result(String, GetError)

Get a string from a TOML document.

Examples

as_string(String("hello"))
// -> Ok("hello")
as_string(Int(1))
// -> Error(WrongType([], "String", "Int"))
pub fn as_table(
  toml: Toml,
) -> Result(Dict(String, Toml), GetError)

Get a table from a TOML document.

Examples

as_table(Table(dict.new()))
// -> Ok(dict.new())
as_table(Int(1))
// -> Error(WrongType([], "Table", "Int"))
pub fn as_time(toml: Toml) -> Result(Time, GetError)

Get a time from a TOML document.

Examples

as_time(Time(time))
// -> Ok(time)
as_time(Int(1))
// -> Error(WrongType([], "Time", "Int"))
pub fn get(
  toml: Dict(String, Toml),
  key: List(String),
) -> Result(Toml, GetError)

Get a value of any type from a TOML document dictionary.

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 dictionary.

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 dictionary.

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 dictionary.

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 dictionary.

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 dictionary.

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 dictionary.

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 dictionary. 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 dictionary.

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 dictionary.

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 dictionary.

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