Tomlt

TOML 1.1 Codec Library

Tomlt is a bidirectional codec library for TOML 1.1 configuration files. It provides type-safe encoding and decoding between OCaml types and TOML values.

Quick Start

Define a codec for your configuration type:

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
  ))

Parse and use it:

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)

Library Structure

Cookbook

The cookbook provides patterns and recipes for common TOML scenarios:

Design

Tomlt is inspired by Jsont's approach to JSON codecs. Each codec 'a Tomlt.t defines both:

Codecs compose through combinators, allowing complex types to be built from simple primitives while maintaining bidirectionality.