package toml

  1. Overview
  2. Docs
Library for TOML with a parser, a serializer and a printer

Install

Dune Dependency

Authors

Maintainers

Sources

7.0.0.tar.gz
sha256=551185fbf299d596834eaa4e0a83385c2733c3aa4964e5326e280b1da8a5f908
sha512=afef067912ac41c29b7555554d3aed2c2f6e8f21d06733bd3e3dd6927d09ea13540fbaf5ded1cb23f0ac4bbb34b78a88ff668460ede3a73726996146431d9d27

Description

toml is an OCaml library providing a parser, a serializer and a printer for TOML, a minimal configuration file format. Helpful getters to retrieve data as OCaml primitive types are also supplied.

Published: 17 Mar 2021

README

toml

OCaml library for TOML.

Documentation

Have a look at the online documentation. Otherwise, here's a quickstart guide.

Reading TOML data

utop # (* This will return either `Ok $tomltable or `Error $error_with_location *)
let ok_or_error = Toml.Parser.from_string "key=[1,2]";;
val ok_or_error : Toml.Parser.result = `Ok <abstr> 

utop # (* You can use the 'unsafe' combinator to get the result directly, or an
exception if a parsing error occurred *)
let parsed_toml = Toml.Parser.(from_string "key=[1,2]" |> unsafe);;
val parsed_toml : Toml.Types.table = <abstr>

utop # (* Use simple pattern matching to read the value *)
Toml.Types.Table.find (Toml.Min.key "key") parsed_toml;;
- : Toml.Types.value = Toml.Types.TArray (Toml.Types.NodeInt [1; 2])

Writing TOML data

utop # let toml_data = Toml.of_key_values [
    Toml.key "ints", Toml.Types.TArray (Toml.Types.NodeInt [1; 2]);
    Toml.key "string", Toml.Types.TString "string value";
];;
val toml_data : Toml.Types.table = <abstr>

utop # Toml.Printer.string_of_table toml_data;;
- : bytes = "ints = [1, 2]\nstring = \"string value\"\n"

Lenses

Through lenses, it is possible to read/write deeply nested data with ease. The Toml.Lenses module provides partial lenses (that is, lenses returning option types) to manipulate TOML data structures.

utop # let toml_data = Toml.Parser.(from_string "
[this.is.a.deeply.nested.table]
answer=42" |> unsafe);;
val toml_data : Toml.Types.table = <abstr>

utop # Toml.Lenses.(get toml_data (
  key "this" |-- table
  |-- key "is" |-- table
  |-- key "a" |-- table
  |-- key "deeply" |-- table
  |-- key "nested" |-- table
  |-- key "table" |-- table
  |-- key "answer"|-- int ));;
- : int option = Some 42

utop # let maybe_toml_data' = Toml.Lenses.(set 2015 toml_data (
  key "this" |-- table
  |-- key "is" |-- table
  |-- key "a" |-- table
  |-- key "deeply" |-- table
  |-- key "nested" |-- table
  |-- key "table" |-- table
  |-- key "answer"|-- int ));;
val maybe_toml_data' : Toml.Types.table option = Some <abstr>

utop # Toml.Printer.string_of_table toml_data';;
- : bytes = "[this.is.a.deeply.nested.table]\nanswer = 2015\n"

toml_cconv

A second library, toml_cconv for encoding/decoding with cconv can be installed if cconv is present.

toml supports ppx via cconv:

utop # #require "cconv.ppx";;
utop # #require "toml.cconv";;

utop # type t = { ints : int list; string : string } [@@deriving cconv];;
type t = { ints : int list; string : string; }                                                  
val encode : t CConv.Encode.encoder = {CConv.Encode.emit = <fun>}                               
val decode : t CConv.Decode.decoder =
  {CConv.Decode.dec =
    {CConv.Decode.accept_unit = <fun>; accept_bool = <fun>;
     accept_float = <fun>; accept_int = <fun>; accept_int32 = <fun>;
     accept_int64 = <fun>; accept_nativeint = <fun>; accept_char = <fun>;
     accept_string = <fun>; accept_list = <fun>; accept_option = <fun>;
     accept_record = <fun>; accept_tuple = <fun>; accept_sum = <fun>}}

utop # let toml = Toml.Parser.(from_string "ints = [1, 2]\nstring = \"string value\"\n"
                               |> unsafe);;
val toml : Toml.Types.table = <abstr>

utop # Toml_cconv.decode_exn decode toml;;
- : t = {ints = [1; 2]; string = "string value"}

Limitations

  • Keys don't quite follow the TOML standard. Both section keys (eg, [key1.key2]) and ordinary keys (key=...) may not contain the following characters: space, \t, \n, \r, ., [, ], " and #.

Projects using toml

If you want to add your project, feel free to open a PR.

Dependencies (5)

  1. menhir < "20211215"
  2. ISO8601 >= "0.2"
  3. menhir build
  4. dune >= "2.7"
  5. ocaml >= "4.07"

Dev Dependencies (4)

  1. odoc with-doc
  2. ocb with-test & >= "0.1" & dev
  3. bisect_ppx with-test & >= "2.5" & dev
  4. ounit2 with-test

Used by (8)

  1. autofonce_config
  2. binsec >= "0.6.0"
  3. camyll >= "0.2.0" & < "0.4.0"
  4. comby
  5. drom_lib >= "0.6.0" & < "0.8.0"
  6. fromager < "0.5.0"
  7. soupault >= "2.3.1" & < "3.0.0"
  8. toml_cconv

Conflicts

None

OCaml

Innovation. Community. Security.