open Tomlt (* Helper to encode TOML to string via writer *) let toml_to_string value = let buf = Buffer.create 256 in Tomlt_bytesrw.to_writer (Bytesrw.Bytes.Writer.of_buffer buf) value; Buffer.contents buf type config = { name : string; timeout : int option } let config_codec = Table.( obj (fun name timeout -> { name; timeout }) |> mem "name" string ~enc:(fun c -> c.name) |> opt_mem "timeout" int ~enc:(fun c -> c.timeout) |> finish) let () = (* Test encoding *) let c = { name = "app"; timeout = None } in let toml = encode config_codec c in Fmt.pr "Encoded TOML:\n%s\n" (toml_to_string toml); (* Show raw structure *) Fmt.pr "\nRaw structure: %s\n" (match toml with | Toml.Table pairs -> String.concat ", " (List.map (fun (k, v) -> Fmt.str "%s=%s" k (match v with | Toml.String s -> Fmt.str "\"%s\"" s | Toml.Bool b -> string_of_bool b | Toml.Int i -> Int64.to_string i | _ -> "?")) pairs) | _ -> "not a table"); (* Test decoding the encoded value *) Fmt.pr "\nDecoding...\n"; match decode config_codec toml with | Ok { name; timeout } -> Fmt.pr "Decoded: name=%s, timeout=%s\n" name (match timeout with Some t -> string_of_int t | None -> "None") | Error e -> Fmt.pr "Decode error: %s\n" (Toml.Error.to_string e) let test_encode_decode_roundtrip () = let c = { name = "app"; timeout = Some 30 } in let toml = encode config_codec c in match decode config_codec toml with | Ok decoded -> Alcotest.(check string) "name" c.name decoded.name; Alcotest.(check (option int)) "timeout" c.timeout decoded.timeout | Error e -> Alcotest.failf "decode failed: %s" (Toml.Error.to_string e) let test_encode_none_timeout () = let c = { name = "app"; timeout = None } in let toml = encode config_codec c in match decode config_codec toml with | Ok decoded -> Alcotest.(check string) "name" c.name decoded.name; Alcotest.(check (option int)) "timeout" None decoded.timeout | Error e -> Alcotest.failf "decode failed: %s" (Toml.Error.to_string e) let suite = ( "debug", [ ("encode_decode roundtrip", `Quick, test_encode_decode_roundtrip); ("encode none timeout", `Quick, test_encode_none_timeout); ] )