Tomlt_bytesrwBytesrw integration for TOML 1.1 parsing and encoding.
This module provides I/O operations for TOML values and codecs using Bytesrw for efficient streaming.
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_stringParse TOML from various sources.
val of_string : string -> (Tomlt.Toml.t, Tomlt.Toml.Error.t) resultof_string s parses s as a TOML document.
val of_reader :
?file:string ->
Bytesrw.Bytes.Reader.t ->
(Tomlt.Toml.t, Tomlt.Toml.Error.t) resultof_reader r parses a TOML document from reader r.
val parse : string -> Tomlt.Toml.tparse s parses s as a TOML document.
val parse_reader : ?file:string -> Bytesrw.Bytes.Reader.t -> Tomlt.Toml.tparse_reader r parses a TOML document from reader r.
Encode TOML values to strings and writers.
val to_string : Tomlt.Toml.t -> stringto_string t encodes t as a TOML-formatted string.
val to_writer : Bytesrw.Bytes.Writer.t -> Tomlt.Toml.t -> unitto_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) valueConvenience functions that combine parsing/encoding with codec operations.
val decode_string : 'a Tomlt.t -> string -> ('a, Tomlt.Toml.Error.t) resultdecode_string c s parses TOML string s and decodes with codec c.
val decode_string_exn : 'a Tomlt.t -> string -> 'adecode_string_exn c s is like decode_string but raises on error.
val encode_string : 'a Tomlt.t -> 'a -> stringencode_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) resultdecode_reader c r parses TOML from reader r and decodes with codec c.
val encode_writer : 'a Tomlt.t -> 'a -> Bytesrw.Bytes.Writer.t -> unitencode_writer c v w encodes v using codec c and writes TOML to writer w.
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