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.
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)Tomlt.Toml - Core TOML value types and operationsTomlt - Codec combinators for bidirectional TOML encoding/decodingTomlt_bytesrw - Streaming parser and encoderTomlt_eio - Eio-native I/O integrationTomlt_unix - Unix I/O integrationTomlt_jsont - JSON codec for toml-test formatThe cookbook provides patterns and recipes for common TOML scenarios:
Tomlt is inspired by Jsont's approach to JSON codecs. Each codec 'a Tomlt.t defines both:
Toml.t -> ('a, error) result'a -> Toml.tCodecs compose through combinators, allowing complex types to be built from simple primitives while maintaining bidirectionality.