Module Toml

module Toml: sig .. end
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
val key : string -> Table.Key.t
Turns a string into a table key.
Raises Toml.Table.Key.Bad_key if the key contains invalid characters.
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
val get_table : Table.Key.t ->
Value.value Table.t -> Value.value Table.t
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

Printing


module Printer: sig .. end

Comparison


module Compare: sig .. end