TOML 1.1 codecs for OCaml
at main 71 lines 2.4 kB view raw
1open Tomlt 2 3(* Helper to encode TOML to string via writer *) 4let toml_to_string value = 5 let buf = Buffer.create 256 in 6 Tomlt_bytesrw.to_writer (Bytesrw.Bytes.Writer.of_buffer buf) value; 7 Buffer.contents buf 8 9type config = { name : string; timeout : int option } 10 11let config_codec = 12 Table.( 13 obj (fun name timeout -> { name; timeout }) 14 |> mem "name" string ~enc:(fun c -> c.name) 15 |> opt_mem "timeout" int ~enc:(fun c -> c.timeout) 16 |> finish) 17 18let () = 19 (* Test encoding *) 20 let c = { name = "app"; timeout = None } in 21 let toml = encode config_codec c in 22 Fmt.pr "Encoded TOML:\n%s\n" (toml_to_string toml); 23 24 (* Show raw structure *) 25 Fmt.pr "\nRaw structure: %s\n" 26 (match toml with 27 | Toml.Table pairs -> 28 String.concat ", " 29 (List.map 30 (fun (k, v) -> 31 Fmt.str "%s=%s" k 32 (match v with 33 | Toml.String s -> Fmt.str "\"%s\"" s 34 | Toml.Bool b -> string_of_bool b 35 | Toml.Int i -> Int64.to_string i 36 | _ -> "?")) 37 pairs) 38 | _ -> "not a table"); 39 40 (* Test decoding the encoded value *) 41 Fmt.pr "\nDecoding...\n"; 42 match decode config_codec toml with 43 | Ok { name; timeout } -> 44 Fmt.pr "Decoded: name=%s, timeout=%s\n" name 45 (match timeout with Some t -> string_of_int t | None -> "None") 46 | Error e -> Fmt.pr "Decode error: %s\n" (Toml.Error.to_string e) 47 48let test_encode_decode_roundtrip () = 49 let c = { name = "app"; timeout = Some 30 } in 50 let toml = encode config_codec c in 51 match decode config_codec toml with 52 | Ok decoded -> 53 Alcotest.(check string) "name" c.name decoded.name; 54 Alcotest.(check (option int)) "timeout" c.timeout decoded.timeout 55 | Error e -> Alcotest.failf "decode failed: %s" (Toml.Error.to_string e) 56 57let test_encode_none_timeout () = 58 let c = { name = "app"; timeout = None } in 59 let toml = encode config_codec c in 60 match decode config_codec toml with 61 | Ok decoded -> 62 Alcotest.(check string) "name" c.name decoded.name; 63 Alcotest.(check (option int)) "timeout" None decoded.timeout 64 | Error e -> Alcotest.failf "decode failed: %s" (Toml.Error.to_string e) 65 66let suite = 67 ( "debug", 68 [ 69 ("encode_decode roundtrip", `Quick, test_encode_decode_roundtrip); 70 ("encode none timeout", `Quick, test_encode_none_timeout); 71 ] )