package toml

  1. Overview
  2. Docs

The TOML module interface

Data types

Data types returned by the parser, can be used to build a Toml structure from scratch.

You should use the Toml.Value.To and Toml.Value.Of modules to navigate between plain OCaml data structures and Toml data structures.

module Table : sig ... end

The type of a Toml table. Toml tables implement the Map interface. Their keys are of type Toml.Table.Key.t.

val key : string -> Table.Key.t

Turns a string into a table key.

module Value : sig ... end

Convenience functions

Toml offers a number of convenience function, to access and find values with a minimum of typing.

From Toml values to OCaml values

Given a Toml value (of type Toml.Value.value), returns an OCaml value.

Example:

  # let table = Toml.Table.empty
    |> Toml.Table.add (Toml.key "foo") (Toml.Value.Of.string "bar");;
  val table : Toml.Value.value Toml.Table.t = <abstr>

  # let bar = Toml.Table.find (Toml.key "foo") table
    |> Toml.Value.To.string;;
  val bar : bytes = "bar" 

  # let table = Toml.Table.empty
    |> Toml.Table.add (Toml.key "fortytwos")
         (Toml.Value.Of.Array.int [42;42] |> Toml.Value.Of.array);;
  val table : Toml.Value.value Toml.Table.t = <abstr>

  # let fortytwos = Toml.Table.find (Toml.key "fortytwos") table
    |> Toml.to_int_array;;
  val fortytwos : int list = [42; 42]

  # let table = Toml.Table.empty
    |> Toml.Table.add (Toml.key "tables")
         ([
           Toml.Table.empty
           |> Toml.Table.add (Toml.key "foo") (Toml.of_string "foofoo");
           Toml.Table.empty
           |> Toml.Table.add (Toml.key "bar") (Toml.of_string "barbar");
          ] |> Toml.of_table_array);;
    val table : Toml.Value.value Toml.Table.t = <abstr>

 # let array_of_tables = Toml.Table.find (Toml.key "tables") table
   |> Toml.to_table_array;;
 val array_of_tables : Toml.Value.table list = [<abstr>; <abstr>]

All conversion functions raise Toml.Value.To.Bad_type if the type is wrong.

val to_bool : Value.value -> bool
  • since 2.2.0
val to_int : Value.value -> int
  • since 2.2.0
val to_float : Value.value -> float
  • since 2.2.0
val to_string : Value.value -> string
  • since 2.2.0
val to_date : Value.value -> Unix.tm
  • since 2.2.0
val to_table : Value.value -> Value.value Table.t
  • since 2.2.0
val to_bool_array : Value.value -> bool list
  • since 2.2.0
val to_int_array : Value.value -> int list
  • since 2.2.0
val to_float_array : Value.value -> float list
  • since 2.2.0
val to_string_array : Value.value -> string list
  • since 2.2.0
val to_date_array : Value.value -> Unix.tm list
  • since 2.2.0
val to_array_array : Value.value -> Value.array list
  • since 2.2.0
val to_table_array : Value.value -> Value.table list
  • since 2.2.0

Getting OCaml values from a table

These functions take a Toml key, a Toml table, and return a plain OCaml value if the key was found in the table.

Example:

  # let table = Toml.Table.empty
    |> Toml.Table.add (Toml.key "foo") (Toml.of_string "bar");;
  val table : Toml.Value.value Toml.Table.t = <abstr>

  # let bar = Toml.get_string (Toml.key "foo") table;;
  val bar : bytes = "bar" 

  # let table = Toml.Table.empty
    |> Toml.Table.add (Toml.key "fortytwos")
         (Toml.of_int_array [42;42]);;
  val table : Toml.Value.value Toml.Table.t = <abstr>

  # let fortytwos = Toml.get_int_array (Toml.key "fortytwos") table;;
  val fortytwos : int list = [42; 42]

All retrieval functions raise Not_found if the value was not found in the table, and Toml.Value.To.Bad_type if the type is wrong.

val get_bool : Table.Key.t -> Value.value Table.t -> bool
  • since 2.2.0
val get_int : Table.Key.t -> Value.value Table.t -> int
  • since 2.2.0
val get_float : Table.Key.t -> Value.value Table.t -> float
  • since 2.2.0
val get_string : Table.Key.t -> Value.value Table.t -> string
  • since 2.2.0
val get_date : Table.Key.t -> Value.value Table.t -> Unix.tm
  • since 2.2.0
  • since 2.2.0
val get_bool_array : Table.Key.t -> Value.value Table.t -> bool list
  • since 2.2.0
val get_int_array : Table.Key.t -> Value.value Table.t -> int list
  • since 2.2.0
val get_float_array : Table.Key.t -> Value.value Table.t -> float list
  • since 2.2.0
val get_string_array : Table.Key.t -> Value.value Table.t -> string list
  • since 2.2.0
val get_date_array : Table.Key.t -> Value.value Table.t -> Unix.tm list
  • since 2.2.0
val get_array_array : Table.Key.t -> Value.value Table.t -> Value.array list
  • since 2.2.0
val get_table_array : Table.Key.t -> Value.value Table.t -> Value.value Table.t list
  • since 2.2.0

From OCaml values to Toml values

These shorthand functions take an OCaml value and turn them into the appropriate Toml value.

Example:

  # let table = Toml.Table.empty
    |> Toml.Table.add (Toml.key "fortytwos")
         (Toml.of_int_array [42;42]);;
  val table : Toml.Value.value Toml.Table.t = <abstr>
val of_bool : bool -> Value.value
  • since 2.2.0
val of_int : int -> Value.value
  • since 2.2.0
val of_float : float -> Value.value
  • since 2.2.0
val of_string : string -> Value.value
  • since 2.2.0
val of_date : Unix.tm -> Value.value
  • since 2.2.0
val of_table : Value.table -> Value.value
  • since 2.2.0
val of_bool_array : bool list -> Value.value
  • since 2.2.0
val of_int_array : int list -> Value.value
  • since 2.2.0
val of_float_array : float list -> Value.value
  • since 2.2.0
val of_string_array : string list -> Value.value
  • since 2.2.0
val of_date_array : Unix.tm list -> Value.value
  • since 2.2.0
val of_array_array : Value.array list -> Value.value
  • since 2.2.0
val of_table_array : Value.table list -> Value.value
  • since 2.2.0

Parser

Simple parsing functions.

module Parser : sig ... end

Parses raw data into Toml data structures

Printing

module Printer : sig ... end

Comparison

module Compare : sig ... end
OCaml

Innovation. Community. Security.