TOML 1.1 codecs for OCaml
README.md

Tomlt #

Tomlt is a type-safe TOML 1.1 codec library for OCaml.

Design #

Tomlt provides bidirectional encoding and decoding using a combinator-based
approach inspired by Jsont. The design
is based on the paper:

Daniel Bünzli. An alphabet for your data soups, 2024.
Available at: https://github.com/dbuenzli/jsont/tree/main/paper

Each codec 'a t defines both a decoder (Toml.t -> ('a, error) result) and
an encoder ('a -> Toml.t), composing through combinators to build complex
types from simple primitives.

Quick Start #

Define a codec for your OCaml types:

type config = { host : string; port : int; debug : bool }

let config_codec =
  Tomlt.(Table.(
    obj (fun host port debug -> { host; port; debug })
    |> mem "host" string ~enc:(fun c -> c.host)
    |> mem "port" int ~enc:(fun c -> c.port)
    |> mem "debug" bool ~enc:(fun c -> c.debug) ~dec_absent:false
    |> finish
  ))

For I/O operations (parsing strings, reading files), use Tomlt_bytesrw:

let () =
  match Tomlt_bytesrw.decode_string config_codec {|
    host = "localhost"
    port = 8080
  |} with
  | Ok config -> Printf.printf "Host: %s\n" config.host
  | Error e -> prerr_endline (Toml.Error.to_string e)

Packages #

  • tomlt - Core library with value types and codec combinators
  • tomlt.bytesrw - Streaming parser/encoder using Bytesrw
  • tomlt.eio - Eio integration with system clock
  • tomlt.unix - Unix I/O with system clock
  • tomlt.jsont - Jsont codecs for toml-test JSON format

License #

ISC