···1+{0 Tomlt}
2+3+{1 TOML 1.1 Codec Library}
4+5+Tomlt is a bidirectional codec library for {{:https://toml.io/en/v1.1.0}TOML 1.1}
6+configuration files. It provides type-safe encoding and decoding between
7+OCaml types and TOML values.
8+9+{2 Quick Start}
10+11+Define a codec for your configuration type:
12+13+{[
14+type config = { host : string; port : int; debug : bool }
15+16+let config_codec =
17+ Tomlt.(Table.(
18+ obj (fun host port debug -> { host; port; debug })
19+ |> mem "host" string ~enc:(fun c -> c.host)
20+ |> mem "port" int ~enc:(fun c -> c.port)
21+ |> mem "debug" bool ~enc:(fun c -> c.debug) ~dec_absent:false
22+ |> finish
23+ ))
24+]}
25+26+Parse and use it:
27+28+{[
29+let () =
30+ match Tomlt_bytesrw.decode_string config_codec {|
31+ host = "localhost"
32+ port = 8080
33+ |} with
34+ | Ok config -> Printf.printf "Host: %s\n" config.host
35+ | Error e -> prerr_endline (Toml.Error.to_string e)
36+]}
37+38+{2 Library Structure}
39+40+- {!Tomlt.Toml} - Core TOML value types and operations
41+- {!Tomlt} - Codec combinators for bidirectional TOML encoding/decoding
42+- {!Tomlt_bytesrw} - Streaming parser and encoder
43+- {!Tomlt_eio} - Eio-native I/O integration
44+- {!Tomlt_unix} - Unix I/O integration
45+- {!Tomlt_jsont} - JSON codec for toml-test format
46+47+{2 Cookbook}
48+49+The {{!page-cookbook}cookbook} provides patterns and recipes for common
50+TOML scenarios:
51+52+- {{!page-cookbook.config_files}Parsing configuration files}
53+- {{!page-cookbook.optional_values}Optional and absent values}
54+- {{!page-cookbook.datetimes}Working with datetimes}
55+- {{!page-cookbook.arrays}Working with arrays}
56+- {{!page-cookbook.tables}Nested tables and objects}
57+- {{!page-cookbook.unknown_members}Unknown member handling}
58+- {{!page-cookbook.validation}Validation and constraints}
59+- {{!page-cookbook.roundtripping}Roundtripping TOML}
60+- {{!page-cookbook.error_handling}Error handling}
61+62+{2 Design}
63+64+Tomlt is inspired by {{:https://erratique.ch/software/jsont}Jsont}'s approach
65+to JSON codecs. Each codec ['a Tomlt.t] defines both:
66+67+- A decoder: [Toml.t -> ('a, error) result]
68+- An encoder: ['a -> Toml.t]
69+70+Codecs compose through combinators, allowing complex types to be built
71+from simple primitives while maintaining bidirectionality.
+9-7
lib/toml.mli
···78 This module provides the core TOML value type and operations for
9 constructing, accessing, and manipulating TOML data. For parsing and
10- encoding, see {!Tomlt_bytesrw}.
01112 {2 Quick Start}
13···2425 Access values:
26 {[
27- let host = Toml.(config.%{["database"; "host"]} |> to_string)
28- let port = Toml.(config.%{["database"; "ports"]} |> to_array |> List.hd |> to_int)
029 ]}
003031 {2 Module Overview}
32···370371val pp_value : Format.formatter -> t -> unit
372(** [pp_value fmt t] pretty-prints a single TOML value.
373- Same as {!pp}. *)
374375val equal : t -> t -> bool
376(** [equal a b] is structural equality on TOML values.
···382(** {1:errors Error Handling} *)
383384module Error = Toml_error
385-(** Structured error types for TOML parsing and encoding.
386-387- See {!Toml_error} for detailed documentation. *)
···78 This module provides the core TOML value type and operations for
9 constructing, accessing, and manipulating TOML data. For parsing and
10+ encoding, see {!Tomlt_bytesrw}. For codec-based bidirectional encoding,
11+ see {!Tomlt}.
1213 {2 Quick Start}
14···2526 Access values:
27 {[
28+ let host = Toml.to_string (Toml.find "host" (Toml.find "database" config))
29+ let ports = Toml.to_array (Toml.find "ports" (Toml.find "database" config))
30+ let port = Toml.to_int (List.hd ports)
31 ]}
32+33+ See the {{!page-cookbook}cookbook} for common patterns and recipes.
3435 {2 Module Overview}
36···374375val pp_value : Format.formatter -> t -> unit
376(** [pp_value fmt t] pretty-prints a single TOML value.
377+ Same as {!val:pp}. *)
378379val equal : t -> t -> bool
380(** [equal a b] is structural equality on TOML values.
···386(** {1:errors Error Handling} *)
387388module Error = Toml_error
389+(** Structured error types for TOML parsing and encoding. *)
00
+54-378
lib/tomlt.mli
···44 Codecs compose through combinators to build complex types from
45 simple primitives.
4647- {2 Datetime Handling}
48-49- Tomlt uses {{:https://erratique.ch/software/ptime}Ptime} for all datetime
50- operations, providing a unified approach to TOML's four datetime formats:
5152- {v
53- (* Accept any TOML datetime format, normalize to Ptime.t *)
54- type event = { name : string; when_ : Ptime.t }
5556- let event_codec = Tomlt.(Table.(
57- obj (fun name when_ -> { name; when_ })
58- |> mem "name" string ~enc:(fun e -> e.name)
59- |> mem "when" (ptime ()) ~enc:(fun e -> e.when_)
60- |> finish
61- ))
62-63- (* All of these work: *)
64- (* when = 2024-01-15T10:30:00Z -> offset datetime *)
65- (* when = 2024-01-15T10:30:00 -> local datetime (uses system tz) *)
66- (* when = 2024-01-15 -> date only (assumes midnight) *)
67- (* when = 10:30:00 -> time only (uses today's date) *)
68- v}
69-70- See {!section:ptime_codecs} for the complete datetime codec API.
7172 {2 Module Overview}
73···340341(** {1:ptime_codecs Ptime Datetime Codecs}
342343- Tomlt provides a unified datetime handling system built on
344- {{:https://erratique.ch/software/ptime}Ptime}. All
345- {{:https://toml.io/en/v1.1.0#offset-date-time}TOML datetime formats}
346- can be decoded to [Ptime.t] timestamps with sensible defaults for
347- incomplete information.
348-349- {2 TOML Datetime Formats}
350-351- {{:https://toml.io/en/v1.1.0}TOML 1.1} supports four datetime formats
352- with varying levels of precision:
353-354- {v
355- # Offset datetime - full timestamp with timezone (unambiguous)
356- # See: https://toml.io/en/v1.1.0#offset-date-time
357- published = 2024-01-15T10:30:00Z
358- published = 2024-01-15T10:30:00-05:00
359360- # Local datetime - no timezone (wall clock time)
361- # See: https://toml.io/en/v1.1.0#local-date-time
362- meeting = 2024-01-15T10:30:00
363-364- # Local date - date only
365- # See: https://toml.io/en/v1.1.0#local-date
366- birthday = 1979-05-27
367-368- # Local time - time only
369- # See: https://toml.io/en/v1.1.0#local-time
370- alarm = 07:30:00
371- v}
372373 {2 Choosing a Codec}
374375- - {!val:ptime} - {b Recommended for most cases.} Accepts any datetime format
376- and normalizes to [Ptime.t] by filling in sensible defaults.
377-378- - {!val:ptime_opt} - {b For strict validation.} Only accepts offset datetimes
379- with explicit timezone. Rejects ambiguous local formats.
380-381- - {!val:ptime_date} - For fields that should only contain dates.
382-383- - {!val:ptime_span} - For fields that should only contain times (as duration).
384-385- - {!val:ptime_full} - {b For roundtripping.} Preserves the exact datetime
386- variant from the source, allowing faithful re-encoding.
387-388- {2 Timezone Handling}
389-390- For local datetimes without explicit timezone, Tomlt uses
391- [Ptime_clock.current_tz_offset_s ()] to get the system timezone.
392- You can override this by passing [~tz_offset_s]:
393-394- {v
395- (* Force UTC interpretation for local datetimes *)
396- let codec = ptime ~tz_offset_s:0 ()
397-398- (* Force Eastern Time (-05:00 = -18000 seconds) *)
399- let codec = ptime ~tz_offset_s:(-18000) ()
400- v}
401-402- {2 Examples}
403-404- {3 Basic Event Tracking}
405- {v
406- type event = { name : string; timestamp : Ptime.t }
407-408- let event_codec = Tomlt.(Table.(
409- obj (fun name timestamp -> { name; timestamp })
410- |> mem "name" string ~enc:(fun e -> e.name)
411- |> mem "when" (ptime ()) ~enc:(fun e -> e.timestamp)
412- |> finish
413- ))
414-415- (* All of these decode successfully: *)
416- (* when = 2024-01-15T10:30:00Z *)
417- (* when = 2024-01-15T10:30:00 *)
418- (* when = 2024-01-15 *)
419- (* when = 10:30:00 *)
420- v}
421-422- {3 Strict Timestamp Validation}
423- {v
424- type log_entry = { message : string; timestamp : Ptime.t }
425-426- let log_codec = Tomlt.(Table.(
427- obj (fun message timestamp -> { message; timestamp })
428- |> mem "message" string ~enc:(fun e -> e.message)
429- |> mem "timestamp" (ptime_opt ()) ~enc:(fun e -> e.timestamp)
430- |> finish
431- ))
432-433- (* Only accepts: timestamp = 2024-01-15T10:30:00Z *)
434- (* Rejects: timestamp = 2024-01-15T10:30:00 *)
435- v}
436-437- {3 Birthday (Date Only)}
438- {v
439- type person = { name : string; birthday : Ptime.date }
440-441- let person_codec = Tomlt.(Table.(
442- obj (fun name birthday -> { name; birthday })
443- |> mem "name" string ~enc:(fun p -> p.name)
444- |> mem "birthday" ptime_date ~enc:(fun p -> p.birthday)
445- |> finish
446- ))
447-448- (* birthday = 1979-05-27 -> (1979, 5, 27) *)
449- v}
450-451- {3 Daily Alarm (Time Only)}
452- {v
453- type alarm = { label : string; time : Ptime.Span.t }
454-455- let alarm_codec = Tomlt.(Table.(
456- obj (fun label time -> { label; time })
457- |> mem "label" string ~enc:(fun a -> a.label)
458- |> mem "time" ptime_span ~enc:(fun a -> a.time)
459- |> finish
460- ))
461-462- (* time = 07:30:00 -> 27000 seconds (7.5 hours from midnight) *)
463- v}
464-465- {3 Preserving Datetime Format}
466- {v
467- type flexible_event = {
468- name : string;
469- when_ : Toml.ptime_datetime;
470- }
471-472- let flexible_codec = Tomlt.(Table.(
473- obj (fun name when_ -> { name; when_ })
474- |> mem "name" string ~enc:(fun e -> e.name)
475- |> mem "when" (ptime_full ()) ~enc:(fun e -> e.when_)
476- |> finish
477- ))
478-479- (* Decoding preserves the variant:
480- when = 2024-01-15T10:30:00Z -> `Datetime (ptime, Some 0)
481- when = 2024-01-15T10:30:00 -> `Datetime_local ptime
482- when = 2024-01-15 -> `Date (2024, 1, 15)
483- when = 10:30:00 -> `Time (10, 30, 0, 0)
484-485- Encoding reproduces the original format. *)
486- v} *)
487488val ptime :
489 ?tz_offset_s:int ->
···493 unit -> Ptime.t t
494(** Datetime codec that converts any TOML datetime to {!Ptime.t}.
495496- This is the recommended codec for most datetime use cases. It handles
497- all TOML datetime variants by filling in sensible defaults:
498-499- - {b Offset datetime} ([2024-01-15T10:30:00Z]): Parsed directly to [Ptime.t]
500- - {b Local datetime} ([2024-01-15T10:30:00]): Converted using the timezone
501- - {b Local date} ([2024-01-15]): Assumed to be midnight (00:00:00) in the
502- given timezone
503- - {b Local time} ([10:30:00]): Combined with today's date using [now]
504-505- Encoding always produces an RFC 3339 offset datetime string.
506-507- {4 Parameters}
508-509- @param tz_offset_s Explicit timezone offset in seconds, used for:
510- - Converting local datetimes to [Ptime.t]
511- - Converting local dates to [Ptime.t] (at midnight)
512- - Converting local times to [Ptime.t] (on today's date)
513- - Formatting the timezone when encoding
514-515- Common values:
516- - [0] = UTC
517- - [3600] = +01:00 (Central European Time)
518- - [-18000] = -05:00 (Eastern Standard Time)
519- - [-28800] = -08:00 (Pacific Standard Time)
520-521- If not provided, [get_tz] is called. If neither is provided, defaults
522- to UTC (0).
523-524- @param get_tz Function to get the current timezone offset. Called when
525- [tz_offset_s] is not provided. Pass [Tomlt_unix.current_tz_offset_s]
526- for OS-specific timezone support:
527- {[let codec = ptime ~get_tz:Tomlt_unix.current_tz_offset_s ()]}
528-529- @param now Function to get the current time. Used when decoding local
530- times (e.g., [10:30:00]) to combine with today's date. Pass
531- [Tomlt_unix.now] for OS-specific time support. If not provided,
532- defaults to [Ptime.epoch] (1970-01-01).
533-534- @param frac_s Number of fractional second digits to include when encoding.
535- Range: 0-12. Default: 0 (whole seconds only). For example, [~frac_s:3]
536- produces [2024-01-15T10:30:00.123Z].
537538- {4 Example}
539- {[
540- type event = { name : string; timestamp : Ptime.t }
541542- let event_codec = Tomlt.(Table.(
543- obj (fun name timestamp -> { name; timestamp })
544- |> mem "name" string ~enc:(fun e -> e.name)
545- |> mem "when" (ptime ()) ~enc:(fun e -> e.timestamp)
546- |> finish
547- ))
548-549- (* All of these decode to a Ptime.t: *)
550- let e1 = decode_string_exn event_codec {|name="a" when=2024-01-15T10:30:00Z|}
551- let e2 = decode_string_exn event_codec {|name="b" when=2024-01-15T10:30:00|}
552- let e3 = decode_string_exn event_codec {|name="c" when=2024-01-15|}
553- let e4 = decode_string_exn event_codec {|name="d" when=10:30:00|}
554- ]} *)
555556val ptime_opt : ?tz_offset_s:int -> ?frac_s:int -> unit -> Ptime.t t
557(** Strict datetime codec that only accepts offset datetimes.
558559- Unlike {!ptime} which accepts any datetime format, this codec requires
560- an explicit timezone and rejects local datetime variants. Use this when
561- you need unambiguous timestamps and want to reject values that would
562- require timezone assumptions.
563564- {4 Accepted}
565- - [2024-01-15T10:30:00Z] (UTC)
566- - [2024-01-15T10:30:00+05:30] (explicit offset)
567- - [2024-01-15T10:30:00-08:00] (explicit offset)
568-569- {4 Rejected}
570-571- These raise [Value_error]:
572-573- - [2024-01-15T10:30:00] (local datetime - no timezone)
574- - [2024-01-15] (local date)
575- - [10:30:00] (local time)
576577 @param tz_offset_s Timezone offset for encoding. Default: 0 (UTC).
578- @param frac_s Fractional second digits for encoding. Default: 0.
579-580- {4 Example}
581- {[
582- type audit_log = { action : string; timestamp : Ptime.t }
583-584- let audit_codec = Tomlt.(Table.(
585- obj (fun action timestamp -> { action; timestamp })
586- |> mem "action" string ~enc:(fun a -> a.action)
587- |> mem "timestamp" (ptime_opt ()) ~enc:(fun a -> a.timestamp)
588- |> finish
589- ))
590-591- (* Valid: timestamp = 2024-01-15T10:30:00Z *)
592- (* Error: timestamp = 2024-01-15T10:30:00 (no timezone) *)
593- ]} *)
594595val ptime_span : Ptime.Span.t t
596(** Codec for TOML local times as [Ptime.Span.t] (duration from midnight).
597598- Decodes a local time like [07:32:00] or [14:30:45.123] to a [Ptime.Span.t]
599- representing the time elapsed since midnight (00:00:00).
600-601- When encoding, the span is formatted as a local time string. Values are
602- clamped to the range [00:00:00] to [23:59:59.999999999].
603-604- {4 Decoding}
605- - [07:32:00] -> 27120 seconds (7 hours, 32 minutes)
606- - [14:30:45.5] -> 52245.5 seconds
607- - [00:00:00] -> 0 seconds
608-609- {4 Encoding}
610- - 27120 seconds -> [07:32:00]
611- - 52245.5 seconds -> [14:30:45.5]
612-613- {4 Example}
614- {[
615- type daily_schedule = { name : string; start_time : Ptime.Span.t }
616617- let schedule_codec = Tomlt.(Table.(
618- obj (fun name start_time -> { name; start_time })
619- |> mem "name" string ~enc:(fun s -> s.name)
620- |> mem "start_time" ptime_span ~enc:(fun s -> s.start_time)
621- |> finish
622- ))
623-624- (* start_time = 09:00:00 -> 32400 seconds *)
625- ]} *)
626627val ptime_date : Ptime.date t
628-(** Codec for TOML local dates as [Ptime.date] (a [(year, month, day)] tuple).
629-630- Decodes a local date like [1979-05-27] to an [(int * int * int)] tuple.
631- Only accepts [Date_local] TOML values; rejects datetimes and times.
632-633- {4 Example}
634- {[
635- type person = { name : string; birthday : Ptime.date }
636-637- let person_codec = Tomlt.(Table.(
638- obj (fun name birthday -> { name; birthday })
639- |> mem "name" string ~enc:(fun p -> p.name)
640- |> mem "birthday" ptime_date ~enc:(fun p -> p.birthday)
641- |> finish
642- ))
643644- (* birthday = 1979-05-27 -> (1979, 5, 27) *)
645- ]}
646647- To work with dates as [Ptime.t] (at midnight), use {!ptime} instead. *)
648649val ptime_full :
650 ?tz_offset_s:int ->
···652 unit -> Toml.ptime_datetime t
653(** Codec that preserves full datetime variant information.
654655- Unlike {!ptime} which normalizes all datetime formats to [Ptime.t],
656- this codec returns a polymorphic variant that indicates exactly what
657- was present in the TOML source. This is essential for:
658-659- - Distinguishing between datetime formats during decoding
660- - Roundtripping TOML files while preserving the original format
661- - Applications that treat different datetime formats differently
662-663- {4 Decoded Variants}
664-665- The [Toml.ptime_datetime] type is:
666- {[
667- type ptime_datetime = [
668- | `Datetime of Ptime.t * Ptime.tz_offset_s option
669- | `Datetime_local of Ptime.t
670- | `Date of Ptime.date
671- | `Time of int * int * int * int (* hour, minute, second, nanoseconds *)
672- ]
673- ]}
674-675- {4 Mapping from TOML}
676-677- - [2024-01-15T10:30:00Z] -> [`Datetime (ptime, Some 0)]
678- - [2024-01-15T10:30:00-05:00] -> [`Datetime (ptime, Some (-18000))]
679- - [2024-01-15T10:30:00] -> [`Datetime_local ptime]
680- - [2024-01-15] -> [`Date (2024, 1, 15)]
681- - [10:30:45.123] -> [`Time (10, 30, 45, 123_000_000)]
682-683- {4 Encoding}
684685- When encoding, the variant determines the output format:
686- - [`Datetime] -> offset datetime with timezone
687- - [`Datetime_local] -> local datetime (no timezone)
688- - [`Date] -> local date
689- - [`Time] -> local time
690691- @param tz_offset_s Explicit timezone offset for converting
692- [`Datetime_local] to [Ptime.t].
693-694- @param get_tz Function to get the current timezone offset. Called when
695- [tz_offset_s] is not provided. Pass [Tomlt_unix.current_tz_offset_s]
696- for OS-specific timezone support. If neither is provided, defaults to
697- UTC (0).
698-699- {4 Example}
700- {[
701- type schedule_item = {
702- description : string;
703- when_ : Toml.ptime_datetime;
704- }
705-706- let item_codec = Tomlt.(Table.(
707- obj (fun description when_ -> { description; when_ })
708- |> mem "description" string ~enc:(fun i -> i.description)
709- |> mem "when" (ptime_full ()) ~enc:(fun i -> i.when_)
710- |> finish
711- ))
712-713- (* Can distinguish between:
714- - when = 2024-01-15T10:00:00Z (specific instant)
715- - when = 2024-01-15T10:00:00 (wall clock time)
716- - when = 2024-01-15 (all day)
717- - when = 10:00:00 (daily recurring)
718- *)
719- ]} *)
720721(** {1:combinators Codec Combinators} *)
722···904905(** {1:arrays Array Codecs}
906907- Build codecs for {{:https://toml.io/en/v1.1.0#array}TOML arrays}. *)
00908909module Array : sig
910 type 'a codec = 'a t
···947(** {1:tables Table Codecs}
948949 Build codecs for {{:https://toml.io/en/v1.1.0#table}TOML tables}
950- (key-value mappings). The applicative-style builder pattern allows
951- defining bidirectional codecs declaratively.
952-953- Tables can be defined using standard headers or as
954- {{:https://toml.io/en/v1.1.0#inline-table}inline tables}.
955- {{:https://toml.io/en/v1.1.0#keys}Keys} can be bare, quoted, or dotted.
956-957- {2 Basic Usage}
958-959- {v
960- type person = { name : string; age : int }
961962- let person_codec = Tomlt.Table.(
963- obj (fun name age -> { name; age })
964- |> mem "name" Tomlt.string ~enc:(fun p -> p.name)
965- |> mem "age" Tomlt.int ~enc:(fun p -> p.age)
966- |> finish
967- )
968- v} *)
969970module Table : sig
971 type 'a codec = 'a t
···44 Codecs compose through combinators to build complex types from
45 simple primitives.
4647+ {2 Cookbook}
0004849+ See the {{!page-cookbook}cookbook} for patterns and recipes:
005051+ - {{!page-cookbook.config_files}Parsing configuration files}
52+ - {{!page-cookbook.optional_values}Optional and absent values}
53+ - {{!page-cookbook.datetimes}Working with datetimes}
54+ - {{!page-cookbook.arrays}Working with arrays}
55+ - {{!page-cookbook.tables}Nested tables and objects}
56+ - {{!page-cookbook.unknown_members}Unknown member handling}
57+ - {{!page-cookbook.validation}Validation and constraints}
000000005859 {2 Module Overview}
60···327328(** {1:ptime_codecs Ptime Datetime Codecs}
329330+ Tomlt provides unified datetime handling using
331+ {{:https://erratique.ch/software/ptime}Ptime}. All TOML datetime formats
332+ can be decoded to [Ptime.t] timestamps.
0000000000000333334+ See the {{!page-cookbook.datetimes}cookbook} for detailed patterns
335+ and examples.
0000000000336337 {2 Choosing a Codec}
338339+ - {!val:ptime} - Accepts any datetime format, normalizes to [Ptime.t]
340+ - {!val:ptime_opt} - Strict: only accepts offset datetimes with timezone
341+ - {!val:ptime_date} - For date-only fields
342+ - {!val:ptime_span} - For time-only fields (as duration from midnight)
343+ - {!val:ptime_full} - Preserves exact variant for roundtripping *)
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000344345val ptime :
346 ?tz_offset_s:int ->
···350 unit -> Ptime.t t
351(** Datetime codec that converts any TOML datetime to {!Ptime.t}.
352353+ Handles all TOML datetime variants by filling in sensible defaults.
354+ Encoding produces RFC 3339 offset datetime strings.
000000000000000000000000000000000000000355356+ See {{!page-cookbook.datetimes}Working with datetimes} for examples.
00357358+ @param tz_offset_s Timezone offset in seconds for local datetimes.
359+ Common: [0] (UTC), [3600] (+01:00), [-18000] (-05:00).
360+ @param get_tz Function to get timezone offset when [tz_offset_s]
361+ not provided. Use [Tomlt_unix.current_tz_offset_s] for system timezone.
362+ @param now Function for current time, used for time-only values.
363+ Use [Tomlt_unix.now] for system time.
364+ @param frac_s Fractional second digits (0-12) for encoding. *)
000000365366val ptime_opt : ?tz_offset_s:int -> ?frac_s:int -> unit -> Ptime.t t
367(** Strict datetime codec that only accepts offset datetimes.
368369+ Requires explicit timezone; rejects local datetimes, dates, and times.
370+ Use when you need unambiguous timestamps.
00371372+ See {{!page-cookbook.datetimes}Working with datetimes} for examples.
00000000000373374 @param tz_offset_s Timezone offset for encoding. Default: 0 (UTC).
375+ @param frac_s Fractional second digits for encoding. Default: 0. *)
000000000000000376377val ptime_span : Ptime.Span.t t
378(** Codec for TOML local times as [Ptime.Span.t] (duration from midnight).
379380+ Decodes [07:32:00] to a span representing time since midnight.
381+ Values are clamped to [00:00:00] to [23:59:59.999999999].
0000000000000000382383+ See {{!page-cookbook.datetimes}Working with datetimes} for examples. *)
00000000384385val ptime_date : Ptime.date t
386+(** Codec for TOML local dates as [Ptime.date] ([(year, month, day)] tuple).
00000000000000387388+ Decodes [1979-05-27] to [(1979, 5, 27)]. Only accepts local dates.
389+ To work with dates as [Ptime.t] (at midnight), use {!ptime} instead.
390391+ See {{!page-cookbook.datetimes}Working with datetimes} for examples. *)
392393val ptime_full :
394 ?tz_offset_s:int ->
···396 unit -> Toml.ptime_datetime t
397(** Codec that preserves full datetime variant information.
398399+ Returns a {!Toml.ptime_datetime} variant indicating exactly what was
400+ present in the TOML source. Essential for roundtripping TOML files
401+ while preserving the original format.
00000000000000000000000000402403+ See {{!page-cookbook.datetimes}Working with datetimes} and
404+ {{!page-cookbook.roundtripping}Roundtripping TOML} for examples.
000405406+ @param tz_offset_s Timezone offset for converting [`Datetime_local].
407+ @param get_tz Function for timezone when [tz_offset_s] not provided. *)
000000000000000000000000000408409(** {1:combinators Codec Combinators} *)
410···592593(** {1:arrays Array Codecs}
594595+ Build codecs for {{:https://toml.io/en/v1.1.0#array}TOML arrays}.
596+597+ See {{!page-cookbook.arrays}Working with arrays} for patterns. *)
598599module Array : sig
600 type 'a codec = 'a t
···637(** {1:tables Table Codecs}
638639 Build codecs for {{:https://toml.io/en/v1.1.0#table}TOML tables}
640+ using an applicative-style builder pattern.
0000000000641642+ See the {{!page-cookbook.config_files}cookbook} for configuration patterns,
643+ {{!page-cookbook.optional_values}optional values}, and
644+ {{!page-cookbook.unknown_members}unknown member handling}. *)
0000645646module Table : sig
647 type 'a codec = 'a t
+3-2
lib_bytesrw/tomlt_bytesrw.mli
···20 |} in
21 match config with
22 | Ok t ->
23- let host = Tomlt.Toml.(t.%{["server"; "host"]} |> to_string) in
24- let port = Tomlt.Toml.(t.%{["server"; "port"]} |> to_int) in
025 Printf.printf "Server: %s:%Ld\n" host port
26 | Error e -> prerr_endline (Tomlt.Toml.Error.to_string e)
27 ]}
···20 |} in
21 match config with
22 | Ok t ->
23+ let server = Tomlt.Toml.find "server" t in
24+ let host = Tomlt.Toml.to_string (Tomlt.Toml.find "host" server) in
25+ let port = Tomlt.Toml.to_int (Tomlt.Toml.find "port" server) in
26 Printf.printf "Server: %s:%Ld\n" host port
27 | Error e -> prerr_endline (Tomlt.Toml.Error.to_string e)
28 ]}