Module Tomlt_bytesrw

Bytesrw integration for TOML 1.1 parsing and encoding.

This module provides I/O operations for TOML values and codecs using Bytesrw for efficient streaming.

Quick Start

Parse a TOML string:

  let config = Tomlt_bytesrw.of_string {|
    [server]
    host = "localhost"
    port = 8080
  |} in
  match config with
  | Ok t ->
      let server = Tomlt.Toml.find "server" t in
      let host = Tomlt.Toml.to_string (Tomlt.Toml.find "host" server) in
      let port = Tomlt.Toml.to_int (Tomlt.Toml.find "port" server) in
      Printf.printf "Server: %s:%Ld\n" host port
  | Error e -> prerr_endline (Tomlt.Toml.Error.to_string e)

Use with codecs:

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

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

  let config = Tomlt_bytesrw.decode_string config_codec toml_string

Module Overview

Parsing (Decoding)

Parse TOML from various sources.

val of_string : string -> (Tomlt.Toml.t, Tomlt.Toml.Error.t) result

of_string s parses s as a TOML document.

val of_reader : ?file:string -> Bytesrw.Bytes.Reader.t -> (Tomlt.Toml.t, Tomlt.Toml.Error.t) result

of_reader r parses a TOML document from reader r.

  • parameter file

    Optional filename for error messages.

val parse : string -> Tomlt.Toml.t

parse s parses s as a TOML document.

val parse_reader : ?file:string -> Bytesrw.Bytes.Reader.t -> Tomlt.Toml.t

parse_reader r parses a TOML document from reader r.

  • parameter file

    Optional filename for error messages.

Encoding

Encode TOML values to strings and writers.

val to_string : Tomlt.Toml.t -> string

to_string t encodes t as a TOML-formatted string.

val to_writer : Bytesrw.Bytes.Writer.t -> Tomlt.Toml.t -> unit

to_writer w t writes t as TOML to writer w.

Use with Bytesrw.Bytes.Writer to write to various destinations:

  (* To buffer *)
  let buf = Buffer.create 256 in
  Tomlt_bytesrw.to_writer (Bytes.Writer.of_buffer buf) value;
  Buffer.contents buf

  (* To channel *)
  Tomlt_bytesrw.to_writer (Bytes.Writer.of_out_channel oc) value

Codec I/O Operations

Convenience functions that combine parsing/encoding with codec operations.

val decode_string : 'a Tomlt.t -> string -> ('a, Tomlt.Toml.Error.t) result

decode_string c s parses TOML string s and decodes with codec c.

val decode_string_exn : 'a Tomlt.t -> string -> 'a

decode_string_exn c s is like decode_string but raises on error.

val encode_string : 'a Tomlt.t -> 'a -> string

encode_string c v encodes v using codec c to a TOML-formatted string.

val decode_reader : ?file:string -> 'a Tomlt.t -> Bytesrw.Bytes.Reader.t -> ('a, Tomlt.Toml.Error.t) result

decode_reader c r parses TOML from reader r and decodes with codec c.

  • parameter file

    Optional filename for error messages.

val encode_writer : 'a Tomlt.t -> 'a -> Bytesrw.Bytes.Writer.t -> unit

encode_writer c v w encodes v using codec c and writes TOML to writer w.

Tagged JSON

Functions for interoperating with the toml-test suite's tagged JSON format. These functions are primarily for testing and validation.

module Tagged_json : sig ... end