README.md

Toml #

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

Design #

Toml 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.

Installation #

Install with opam:

opam install nox-toml

If opam cannot find the package, it may not yet be released in the public opam-repository. Add the overlay repository, then install it:

opam repo add samoht https://tangled.org/gazagnaire.org/opam-overlay.git
opam update
opam install nox-toml

Quick Start #

Define a codec for your OCaml types:

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

let config_codec =
  Toml.Codec.(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
  ))

Decode a TOML string directly with the core library:

let () =
  
match Toml.of_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)

For streaming decode from a bytesrw reader, use Toml.of_reader.

Packages #

  • nox-toml - Core library with value types, codec combinators, and a streaming parser/encoder on top of Bytesrw. Modules are exposed as Toml.*.

Licence #

ISC