Yaml encoder/decoder for OCaml jsont codecs

Consolidate duplicate hex utilities and crypto functions

- Replace local hex_of_string/string_of_hex with ohex in tests:
- ocaml-spake2/test/test_spake2.ml
- ocaml-matter/test/test_tlv.ml
- ocaml-cbort/test/test_cbort.ml

- Make pase.ml re-export Crypto.sha256/hmac_sha256 instead of duplicating

- Remove odoc-xo rules from ocaml-yamlrw/doc/dune

- Apply ocamlformat to yamlrw, yamlt, bytesrw-eio

+194 -141
+16 -29
lib/yamlt.ml
··· 71 71 The last_byte is on the previous line, so we need to calculate 72 72 the line start position based on last_byte, not stop. *) 73 73 let last_line = 74 - if stop.Position.column = 1 && stop.Position.line > start.Position.line then 74 + if stop.Position.column = 1 && stop.Position.line > start.Position.line 75 + then 75 76 (* last_byte is on the previous line (stop.line - 1) 76 77 We need to estimate where that line starts. Since we don't have 77 78 the full text, we can't calculate it exactly, but we can use: ··· 91 92 last_byte should be the newline character on the previous line. 92 93 The line likely started much earlier, but we'll estimate conservatively. *) 93 94 (last_line_num, last_byte) 94 - else 95 - (stop.Position.line, stop.Position.index - stop.Position.column + 1) 95 + else (stop.Position.line, stop.Position.index - stop.Position.column + 1) 96 96 in 97 97 let textloc = 98 98 Jsont.Textloc.make ~file:d.file ~first_byte ~last_byte ~first_line ··· 206 206 if is_null_scalar value then 207 207 let end_meta = meta_of_span d ev.Event.span in 208 208 map.dec_finish end_meta 0 (map.dec_empty ()) 209 - else 210 - err_type_mismatch d ev.span t ~fnd:"scalar" 209 + else err_type_mismatch d ev.span t ~fnd:"scalar" 211 210 | Object map -> 212 211 (* Treat null as an empty object for convenience *) 213 212 if is_null_scalar value then ··· 223 222 let dict = String_map.fold add_default map.mem_decs Dict.empty in 224 223 let dict = Dict.add object_meta_arg meta dict in 225 224 apply_dict map.dec dict 226 - else 227 - err_type_mismatch d ev.span t ~fnd:"scalar" 225 + else err_type_mismatch d ev.span t ~fnd:"scalar" 228 226 | Map m -> 229 227 (* Handle Map combinators (e.g., from Jsont.option) *) 230 228 m.dec (decode_scalar_as d ev value style m.dom) ··· 347 345 | None -> 348 346 let meta = meta_of_span d ev.span in 349 347 err_msg meta "Unknown anchor: %s" anchor 350 - | Some json_value -> 348 + | Some json_value -> ( 351 349 (* Decode the stored JSON value through the type *) 352 350 let t' = Jsont.Repr.unsafe_to_t t in 353 351 match Jsont.Json.decode' t' json_value with 354 352 | Ok v -> v 355 - | Error e -> raise (Jsont.Error e) 353 + | Error e -> raise (Jsont.Error e)) 356 354 357 355 and decode_array : type a elt b. 358 356 decoder -> nest:int -> Event.spanned -> (a, elt, b) array_map -> a = ··· 576 574 e) 577 575 | None -> 578 576 (* Unknown member - decode as generic JSON and delay *) 579 - let v = 580 - decode d ~nest:(nest + 1) (Jsont.Repr.of_t Jsont.json) 581 - in 577 + let v = decode d ~nest:(nest + 1) (Jsont.Repr.of_t Jsont.json) in 582 578 let delayed = ((name, name_meta), v) :: delayed in 583 - decode_object_cases d ~nest obj_meta object_map umems cases 584 - mem_miss delayed dict 579 + decode_object_cases d ~nest obj_meta object_map umems cases mem_miss 580 + delayed dict 585 581 end 586 582 | None -> err_msg obj_meta "Unclosed mapping" 587 583 ··· 793 789 | `String s -> Jsont.String (s, meta) 794 790 | `A items -> Jsont.Array (List.map value_to_json items, meta) 795 791 | `O fields -> 796 - let mems = 797 - List.map 798 - (fun (k, v) -> ((k, meta), value_to_json v)) 799 - fields 800 - in 792 + let mems = List.map (fun (k, v) -> ((k, meta), value_to_json v)) fields in 801 793 Jsont.Object (mems, meta) 802 794 803 795 let decode_value' t v = ··· 836 828 (* Helper to create scalar events with common defaults *) 837 829 let scalar_event ?(anchor = None) ?(tag = None) ~value ~style () = 838 830 Event.Scalar 839 - { 840 - anchor; 841 - tag; 842 - value; 843 - plain_implicit = true; 844 - quoted_implicit = true; 845 - style; 846 - } 831 + { anchor; tag; value; plain_implicit = true; quoted_implicit = true; style } 847 832 848 833 (* Helper to emit events *) 849 834 let emit e = Emitter.emit e.emitter ··· 916 901 and encode_object : type o. encoder -> (o, o) object_map -> o -> unit = 917 902 fun e map v -> 918 903 let style = layout_style_of_format e.format in 919 - emit e (Event.Mapping_start { anchor = None; tag = None; implicit = true; style }); 904 + emit e 905 + (Event.Mapping_start { anchor = None; tag = None; implicit = true; style }); 920 906 (* Encode each member *) 921 907 List.iter 922 908 (fun (Mem_enc mem) -> ··· 975 961 let e = make_encoder ?format ?indent ?explicit_doc ?scalar_style emitter in 976 962 try 977 963 emit e (Event.Stream_start { encoding = `Utf8 }); 978 - emit e (Event.Document_start { version = None; implicit = not e.explicit_doc }); 964 + emit e 965 + (Event.Document_start { version = None; implicit = not e.explicit_doc }); 979 966 let t' = Jsont.Repr.of_t t in 980 967 encode e t' v; 981 968 emit e (Event.Document_end { implicit = not e.explicit_doc });
+14 -15
lib/yamlt.mli
··· 31 31 32 32 {2 Related Libraries} 33 33 34 - {ul 35 - {- [Jsont] - JSON codec library whose type descriptions this library interprets} 36 - {- [Yamlrw] - Pure OCaml YAML parser/emitter used for low-level YAML processing} 37 - {- [Bytesrw] - Byte-level I/O abstraction for streaming encode/decode}} 34 + - [Jsont] - JSON codec library whose type descriptions this library 35 + interprets 36 + - [Yamlrw] - Pure OCaml YAML parser/emitter used for low-level YAML 37 + processing 38 + - [Bytesrw] - Byte-level I/O abstraction for streaming encode/decode 38 39 39 40 See notes about {{!yaml_mapping}YAML to JSON mapping}, 40 41 {{!yaml_scalars}YAML scalar resolution}, and ··· 102 103 'a Jsont.t -> 103 104 Bytes.Reader.t -> 104 105 ('a, Jsont.Error.t) result Seq.t 105 - (** [decode_all'] is like {!val-decode_all} but preserves the error structure. *) 106 + (** [decode_all'] is like {!val-decode_all} but preserves the error structure. 107 + *) 106 108 107 109 val decode_string : 108 110 ?layout:bool -> ··· 113 115 'a Jsont.t -> 114 116 string -> 115 117 ('a, string) result 116 - (** [decode_string t s] decodes a value from YAML string [s] according to 117 - type [t]. This is a convenience wrapper around {!val-decode}. *) 118 + (** [decode_string t s] decodes a value from YAML string [s] according to type 119 + [t]. This is a convenience wrapper around {!val-decode}. *) 118 120 119 121 val decode_value : 'a Jsont.t -> Yamlrw.value -> ('a, string) result 120 122 (** [decode_value t v] decodes a value from a pre-parsed {!Yamlrw.value} 121 123 according to type [t]. 122 124 123 125 This is useful when you have already parsed YAML into its JSON-compatible 124 - representation (e.g., when using {!Yamlrw.of_string}) and want to decode 125 - it using a Jsont codec without re-parsing the YAML text. *) 126 + representation (e.g., when using {!Yamlrw.of_string}) and want to decode it 127 + using a Jsont codec without re-parsing the YAML text. *) 126 128 127 129 val decode_value' : 'a Jsont.t -> Yamlrw.value -> ('a, Jsont.Error.t) result 128 130 (** [decode_value'] is like {!val-decode_value} but preserves the error ··· 273 275 {[ 274 276 (* Accepts null, decodes as None *) 275 277 Jsont.Object.mem "count" (Jsont.option Jsont.int) ~dec_absent:None 276 - 277 - (* Rejects null, requires a number *) 278 - Jsont.Object.mem "count" Jsont.int ~dec_absent:0 279 - ]} 280 - 281 - *) 278 + (* Rejects null, requires a number *) 279 + Jsont.Object.mem "count" Jsont.int ~dec_absent:0 280 + ]} *)
+12 -4
tests/bin/test_arrays.ml
··· 44 44 let yaml = read_file file in 45 45 let json = read_file (file ^ ".json") in 46 46 let json_result = Jsont_bytesrw.decode_string M.numbers_codec json in 47 - let yaml_result = Yamlt.decode M.numbers_codec (Bytes.Reader.of_string yaml) in 47 + let yaml_result = 48 + Yamlt.decode M.numbers_codec (Bytes.Reader.of_string yaml) 49 + in 48 50 49 51 show_result_both "int_array" 50 52 (Result.map M.show json_result) ··· 94 96 let yaml = read_file file in 95 97 let json = read_file (file ^ ".json") in 96 98 let json_result = Jsont_bytesrw.decode_string M.measurements_codec json in 97 - let yaml_result = Yamlt.decode M.measurements_codec (Bytes.Reader.of_string yaml) in 99 + let yaml_result = 100 + Yamlt.decode M.measurements_codec (Bytes.Reader.of_string yaml) 101 + in 98 102 99 103 show_result_both "float_array" 100 104 (Result.map M.show json_result) ··· 246 250 let yaml = read_file file in 247 251 let json = read_file (file ^ ".json") in 248 252 let json_result = Jsont_bytesrw.decode_string M.nullable_codec json in 249 - let yaml_result = Yamlt.decode M.nullable_codec (Bytes.Reader.of_string yaml) in 253 + let yaml_result = 254 + Yamlt.decode M.nullable_codec (Bytes.Reader.of_string yaml) 255 + in 250 256 251 257 show_result_both "nullable_array" 252 258 (Result.map M.show json_result) ··· 278 284 (* Encode to YAML Block *) 279 285 (let b = Buffer.create 256 in 280 286 let writer = Bytes.Writer.of_buffer b in 281 - match Yamlt.encode ~format:Yamlt.Block M.data_codec data ~eod:true writer with 287 + match 288 + Yamlt.encode ~format:Yamlt.Block M.data_codec data ~eod:true writer 289 + with 282 290 | Ok () -> Printf.printf "YAML Block:\n%s" (Buffer.contents b) 283 291 | Error e -> Printf.printf "YAML Block ERROR: %s\n" e); 284 292
+3 -1
tests/bin/test_complex.ml
··· 98 98 let yaml = read_file file in 99 99 let json = read_file (file ^ ".json") in 100 100 let json_result = Jsont_bytesrw.decode_string M.collection_codec json in 101 - let yaml_result = Yamlt.decode M.collection_codec (Bytes.Reader.of_string yaml) in 101 + let yaml_result = 102 + Yamlt.decode M.collection_codec (Bytes.Reader.of_string yaml) 103 + in 102 104 103 105 show_result_both "mixed_structure" 104 106 (Result.map M.show json_result)
+11 -4
tests/bin/test_comprehensive.ml
··· 33 33 | Ok "" -> Printf.printf "✓ Quoted empty string: \"\"\n" 34 34 | _ -> Printf.printf "✗ FAIL\n"); 35 35 36 - (match Yamlt.decode string_codec (Bytes.Reader.of_string "value: \"null\"") with 36 + (match 37 + Yamlt.decode string_codec (Bytes.Reader.of_string "value: \"null\"") 38 + with 37 39 | Ok "null" -> Printf.printf "✓ Quoted 'null': \"null\"\n" 38 40 | _ -> Printf.printf "✗ FAIL\n"); 39 41 ··· 66 68 |> Jsont.Object.finish 67 69 in 68 70 69 - (match Yamlt.decode opt_array_codec (Bytes.Reader.of_string "values: [a, b, c]") with 71 + (match 72 + Yamlt.decode opt_array_codec (Bytes.Reader.of_string "values: [a, b, c]") 73 + with 70 74 | Ok (Some arr) when Array.length arr = 3 -> 71 75 Printf.printf "✓ Optional array [a, b, c]: Some([3 items])\n" 72 76 | _ -> Printf.printf "✗ FAIL\n"); ··· 87 91 let b = Buffer.create 256 in 88 92 let writer = Bytes.Writer.of_buffer b in 89 93 match 90 - Yamlt.encode ~format:Flow encode_codec ("test", [| 1.; 2.; 3. |]) ~eod:true writer 94 + Yamlt.encode ~format:Flow encode_codec 95 + ("test", [| 1.; 2.; 3. |]) 96 + ~eod:true writer 91 97 with 92 98 | Ok () 93 - when String.equal (Buffer.contents b) "{name: test, values: [1.0, 2.0, 3.0]}\n" -> 99 + when String.equal (Buffer.contents b) 100 + "{name: test, values: [1.0, 2.0, 3.0]}\n" -> 94 101 Printf.printf "✓ Flow encoding with comma separator\n" 95 102 | Ok () -> Printf.printf "✗ FAIL: %S\n" (Buffer.contents b) 96 103 | Error e -> Printf.printf "✗ ERROR: %s\n" e
+3 -1
tests/bin/test_edge.ml
··· 52 52 let yaml = read_file file in 53 53 let json = read_file (file ^ ".json") in 54 54 let json_result = Jsont_bytesrw.decode_string M.numbers_codec json in 55 - let yaml_result = Yamlt.decode M.numbers_codec (Bytes.Reader.of_string yaml) in 55 + let yaml_result = 56 + Yamlt.decode M.numbers_codec (Bytes.Reader.of_string yaml) 57 + in 56 58 57 59 show_result_both "large_numbers" 58 60 (Result.map M.show json_result)
+3 -1
tests/bin/test_flow_newline.ml
··· 11 11 let b = Buffer.create 256 in 12 12 let writer = Bytes.Writer.of_buffer b in 13 13 match 14 - Yamlt.encode ~format:Flow encode_codec ("test", [| 1.; 2.; 3. |]) ~eod:true writer 14 + Yamlt.encode ~format:Flow encode_codec 15 + ("test", [| 1.; 2.; 3. |]) 16 + ~eod:true writer 15 17 with 16 18 | Ok () -> 17 19 let yaml_flow = Buffer.contents b in
+9 -3
tests/bin/test_formats.ml
··· 95 95 let yaml = read_file file in 96 96 let json = read_file (file ^ ".json") in 97 97 let json_result = Jsont_bytesrw.decode_string M.numbers_codec json in 98 - let yaml_result = Yamlt.decode M.numbers_codec (Bytes.Reader.of_string yaml) in 98 + let yaml_result = 99 + Yamlt.decode M.numbers_codec (Bytes.Reader.of_string yaml) 100 + in 99 101 100 102 show_result_both "number_formats" 101 103 (Result.map M.show json_result) ··· 133 135 (* Encode to YAML Block style *) 134 136 (let b = Buffer.create 256 in 135 137 let writer = Bytes.Writer.of_buffer b in 136 - match Yamlt.encode ~format:Yamlt.Block M.data_codec data ~eod:true writer with 138 + match 139 + Yamlt.encode ~format:Yamlt.Block M.data_codec data ~eod:true writer 140 + with 137 141 | Ok () -> Printf.printf "YAML Block:\n%s\n" (Buffer.contents b) 138 142 | Error e -> Printf.printf "YAML Block ERROR: %s\n" e); 139 143 ··· 186 190 let yaml = read_file file in 187 191 let json = read_file (file ^ ".json") in 188 192 let json_result = Jsont_bytesrw.decode_string M.wrapper_codec json in 189 - let yaml_result = Yamlt.decode M.wrapper_codec (Bytes.Reader.of_string yaml) in 193 + let yaml_result = 194 + Yamlt.decode M.wrapper_codec (Bytes.Reader.of_string yaml) 195 + in 190 196 191 197 show_result_both "empty_document" 192 198 (Result.map M.show json_result)
+39 -13
tests/bin/test_locations.ml
··· 33 33 in 34 34 35 35 Printf.printf "=== Without locs (default) ===\n"; 36 - let result_no_locs = Yamlt.decode codec (Bytes.Reader.of_string yaml) ~locs:false in 36 + let result_no_locs = 37 + Yamlt.decode codec (Bytes.Reader.of_string yaml) ~locs:false 38 + in 37 39 show_result "Error message" result_no_locs; 38 40 39 41 Printf.printf "\n=== With locs=true ===\n"; 40 - let result_with_locs = Yamlt.decode codec (Bytes.Reader.of_string yaml) ~locs:true in 42 + let result_with_locs = 43 + Yamlt.decode codec (Bytes.Reader.of_string yaml) ~locs:true 44 + in 41 45 show_result "Error message" result_with_locs 42 46 43 47 (* Test: Show error locations for nested structures *) ··· 62 66 in 63 67 64 68 Printf.printf "=== Without locs (default) ===\n"; 65 - let result_no_locs = Yamlt.decode codec (Bytes.Reader.of_string yaml) ~locs:false in 69 + let result_no_locs = 70 + Yamlt.decode codec (Bytes.Reader.of_string yaml) ~locs:false 71 + in 66 72 show_result "Nested error" result_no_locs; 67 73 68 74 Printf.printf "\n=== With locs=true ===\n"; 69 - let result_with_locs = Yamlt.decode codec (Bytes.Reader.of_string yaml) ~locs:true in 75 + let result_with_locs = 76 + Yamlt.decode codec (Bytes.Reader.of_string yaml) ~locs:true 77 + in 70 78 show_result "Nested error" result_with_locs 71 79 72 80 (* Test: Array element error locations *) ··· 81 89 in 82 90 83 91 Printf.printf "=== Without locs (default) ===\n"; 84 - let result_no_locs = Yamlt.decode codec (Bytes.Reader.of_string yaml) ~locs:false in 92 + let result_no_locs = 93 + Yamlt.decode codec (Bytes.Reader.of_string yaml) ~locs:false 94 + in 85 95 show_result "Array error" result_no_locs; 86 96 87 97 Printf.printf "\n=== With locs=true ===\n"; 88 - let result_with_locs = Yamlt.decode codec (Bytes.Reader.of_string yaml) ~locs:true in 98 + let result_with_locs = 99 + Yamlt.decode codec (Bytes.Reader.of_string yaml) ~locs:true 100 + in 89 101 show_result "Array error" result_with_locs 90 102 91 103 (* Test: Layout preservation - check if we can decode with layout info *) ··· 164 176 show_result "Error" result1; 165 177 166 178 Printf.printf "\n=== With file path ===\n"; 167 - let result2 = Yamlt.decode codec (Bytes.Reader.of_string yaml) ~locs:true ~file:"test.yml" in 179 + let result2 = 180 + Yamlt.decode codec (Bytes.Reader.of_string yaml) ~locs:true ~file:"test.yml" 181 + in 168 182 show_result "Error" result2 169 183 170 184 (* Test: Missing field error with locs *) ··· 180 194 in 181 195 182 196 Printf.printf "=== Without locs ===\n"; 183 - let result_no_locs = Yamlt.decode codec (Bytes.Reader.of_string yaml) ~locs:false in 197 + let result_no_locs = 198 + Yamlt.decode codec (Bytes.Reader.of_string yaml) ~locs:false 199 + in 184 200 show_result "Missing field" result_no_locs; 185 201 186 202 Printf.printf "\n=== With locs=true ===\n"; 187 - let result_with_locs = Yamlt.decode codec (Bytes.Reader.of_string yaml) ~locs:true in 203 + let result_with_locs = 204 + Yamlt.decode codec (Bytes.Reader.of_string yaml) ~locs:true 205 + in 188 206 show_result "Missing field" result_with_locs 189 207 190 208 (* Test: Both locs and layout together *) ··· 200 218 in 201 219 202 220 Printf.printf "=== locs=false, layout=false (defaults) ===\n"; 203 - (match Yamlt.decode codec (Bytes.Reader.of_string yaml) ~locs:false ~layout:false with 221 + (match 222 + Yamlt.decode codec (Bytes.Reader.of_string yaml) ~locs:false ~layout:false 223 + with 204 224 | Ok (timeout, retries) -> 205 225 Printf.printf "OK: timeout=%d, retries=%d\n" timeout retries 206 226 | Error e -> Printf.printf "Error: %s\n" e); 207 227 208 228 Printf.printf "\n=== locs=true, layout=false ===\n"; 209 - (match Yamlt.decode codec (Bytes.Reader.of_string yaml) ~locs:true ~layout:false with 229 + (match 230 + Yamlt.decode codec (Bytes.Reader.of_string yaml) ~locs:true ~layout:false 231 + with 210 232 | Ok (timeout, retries) -> 211 233 Printf.printf "OK: timeout=%d, retries=%d (with precise locations)\n" 212 234 timeout retries 213 235 | Error e -> Printf.printf "Error: %s\n" e); 214 236 215 237 Printf.printf "\n=== locs=false, layout=true ===\n"; 216 - (match Yamlt.decode codec (Bytes.Reader.of_string yaml) ~locs:false ~layout:true with 238 + (match 239 + Yamlt.decode codec (Bytes.Reader.of_string yaml) ~locs:false ~layout:true 240 + with 217 241 | Ok (timeout, retries) -> 218 242 Printf.printf "OK: timeout=%d, retries=%d (with layout metadata)\n" 219 243 timeout retries 220 244 | Error e -> Printf.printf "Error: %s\n" e); 221 245 222 246 Printf.printf "\n=== locs=true, layout=true (both enabled) ===\n"; 223 - match Yamlt.decode codec (Bytes.Reader.of_string yaml) ~locs:true ~layout:true with 247 + match 248 + Yamlt.decode codec (Bytes.Reader.of_string yaml) ~locs:true ~layout:true 249 + with 224 250 | Ok (timeout, retries) -> 225 251 Printf.printf "OK: timeout=%d, retries=%d (with locations and layout)\n" 226 252 timeout retries
+55 -54
tests/bin/test_multidoc.ml
··· 37 37 let reader = Bytes.Reader.of_string yaml in 38 38 let seq = Yamlt.decode_all M.person_codec reader in 39 39 Printf.printf "Documents:\n"; 40 - seq |> Seq.iteri (fun i result -> 41 - Printf.printf " [%d] " i; 42 - show_result "" (Result.map M.show result) 43 - ) 40 + seq 41 + |> Seq.iteri (fun i result -> 42 + Printf.printf " [%d] " i; 43 + show_result "" (Result.map M.show result)) 44 44 45 45 (* Test: Count documents *) 46 46 let test_count file = ··· 67 67 let reader = Bytes.Reader.of_string yaml in 68 68 let seq = Yamlt.decode_all M.person_codec reader in 69 69 Printf.printf "Document results:\n"; 70 - seq |> Seq.iteri (fun i result -> 71 - match result with 72 - | Ok p -> Printf.printf " [%d] OK: %s\n" i (M.show p) 73 - | Error e -> Printf.printf " [%d] ERROR: %s\n" i (String.trim e) 74 - ) 70 + seq 71 + |> Seq.iteri (fun i result -> 72 + match result with 73 + | Ok p -> Printf.printf " [%d] OK: %s\n" i (M.show p) 74 + | Error e -> Printf.printf " [%d] ERROR: %s\n" i (String.trim e)) 75 75 76 76 (* Test: Location tracking with locs=true *) 77 77 let test_locations file = ··· 89 89 Printf.printf "=== Without locs (default) ===\n"; 90 90 let reader = Bytes.Reader.of_string yaml in 91 91 let seq = Yamlt.decode_all ~locs:false M.person_codec reader in 92 - seq |> Seq.iteri (fun i result -> 93 - match result with 94 - | Ok _ -> Printf.printf " [%d] OK\n" i 95 - | Error e -> Printf.printf " [%d] ERROR:\n%s\n" i (String.trim e) 96 - ); 92 + seq 93 + |> Seq.iteri (fun i result -> 94 + match result with 95 + | Ok _ -> Printf.printf " [%d] OK\n" i 96 + | Error e -> Printf.printf " [%d] ERROR:\n%s\n" i (String.trim e)); 97 97 98 98 Printf.printf "\n=== With locs=true ===\n"; 99 99 let reader = Bytes.Reader.of_string yaml in 100 - let seq = Yamlt.decode_all ~locs:true ~file:"test.yml" M.person_codec reader in 101 - seq |> Seq.iteri (fun i result -> 102 - match result with 103 - | Ok _ -> Printf.printf " [%d] OK\n" i 104 - | Error e -> Printf.printf " [%d] ERROR:\n%s\n" i (String.trim e) 105 - ) 100 + let seq = 101 + Yamlt.decode_all ~locs:true ~file:"test.yml" M.person_codec reader 102 + in 103 + seq 104 + |> Seq.iteri (fun i result -> 105 + match result with 106 + | Ok _ -> Printf.printf " [%d] OK\n" i 107 + | Error e -> Printf.printf " [%d] ERROR:\n%s\n" i (String.trim e)) 106 108 107 109 (* Test: Roundtrip to JSON - decode YAML multidoc, encode each to JSON *) 108 110 let test_json_roundtrip file = ··· 110 112 let reader = Bytes.Reader.of_string yaml in 111 113 let seq = Yamlt.decode_all Jsont.json reader in 112 114 Printf.printf "JSON outputs:\n"; 113 - seq |> Seq.iteri (fun i result -> 114 - match result with 115 - | Ok json_val -> 116 - (match Jsont_bytesrw.encode_string Jsont.json json_val with 117 - | Ok json_str -> Printf.printf " [%d] %s\n" i (String.trim json_str) 118 - | Error e -> Printf.printf " [%d] ENCODE ERROR: %s\n" i e) 119 - | Error e -> Printf.printf " [%d] DECODE ERROR: %s\n" i (String.trim e) 120 - ) 115 + seq 116 + |> Seq.iteri (fun i result -> 117 + match result with 118 + | Ok json_val -> ( 119 + match Jsont_bytesrw.encode_string Jsont.json json_val with 120 + | Ok json_str -> Printf.printf " [%d] %s\n" i (String.trim json_str) 121 + | Error e -> Printf.printf " [%d] ENCODE ERROR: %s\n" i e) 122 + | Error e -> Printf.printf " [%d] DECODE ERROR: %s\n" i (String.trim e)) 121 123 122 124 (* Test: Nested objects in multidoc *) 123 125 let test_nested file = ··· 147 149 let reader = Bytes.Reader.of_string yaml in 148 150 let seq = Yamlt.decode_all M.person_codec reader in 149 151 Printf.printf "Nested documents:\n"; 150 - seq |> Seq.iteri (fun i result -> 151 - Printf.printf " [%d] " i; 152 - show_result "" (Result.map M.show result) 153 - ) 152 + seq 153 + |> Seq.iteri (fun i result -> 154 + Printf.printf " [%d] " i; 155 + show_result "" (Result.map M.show result)) 154 156 155 157 (* Test: Arrays in multidoc *) 156 158 let test_arrays file = ··· 158 160 let reader = Bytes.Reader.of_string yaml in 159 161 let seq = Yamlt.decode_all Jsont.json reader in 160 162 Printf.printf "Array documents:\n"; 161 - seq |> Seq.iteri (fun i result -> 162 - match result with 163 - | Ok json_val -> 164 - (match Jsont_bytesrw.encode_string Jsont.json json_val with 165 - | Ok json_str -> Printf.printf " [%d] %s\n" i (String.trim json_str) 166 - | Error e -> Printf.printf " [%d] ERROR: %s\n" i e) 167 - | Error e -> Printf.printf " [%d] ERROR: %s\n" i (String.trim e) 168 - ) 163 + seq 164 + |> Seq.iteri (fun i result -> 165 + match result with 166 + | Ok json_val -> ( 167 + match Jsont_bytesrw.encode_string Jsont.json json_val with 168 + | Ok json_str -> Printf.printf " [%d] %s\n" i (String.trim json_str) 169 + | Error e -> Printf.printf " [%d] ERROR: %s\n" i e) 170 + | Error e -> Printf.printf " [%d] ERROR: %s\n" i (String.trim e)) 169 171 170 172 (* Test: Scalars in multidoc *) 171 173 let test_scalars file = ··· 173 175 let reader = Bytes.Reader.of_string yaml in 174 176 let seq = Yamlt.decode_all Jsont.json reader in 175 177 Printf.printf "Scalar documents:\n"; 176 - seq |> Seq.iteri (fun i result -> 177 - match result with 178 - | Ok json_val -> 179 - (match Jsont_bytesrw.encode_string Jsont.json json_val with 180 - | Ok json_str -> Printf.printf " [%d] %s\n" i (String.trim json_str) 181 - | Error e -> Printf.printf " [%d] ERROR: %s\n" i e) 182 - | Error e -> Printf.printf " [%d] ERROR: %s\n" i (String.trim e) 183 - ) 178 + seq 179 + |> Seq.iteri (fun i result -> 180 + match result with 181 + | Ok json_val -> ( 182 + match Jsont_bytesrw.encode_string Jsont.json json_val with 183 + | Ok json_str -> Printf.printf " [%d] %s\n" i (String.trim json_str) 184 + | Error e -> Printf.printf " [%d] ERROR: %s\n" i e) 185 + | Error e -> Printf.printf " [%d] ERROR: %s\n" i (String.trim e)) 184 186 185 187 (* Test: Summary stats - count successes vs failures *) 186 188 let test_summary file = ··· 198 200 let seq = Yamlt.decode_all M.person_codec reader in 199 201 let success = ref 0 in 200 202 let failure = ref 0 in 201 - seq |> Seq.iter (fun result -> 202 - match result with 203 - | Ok _ -> incr success 204 - | Error _ -> incr failure 205 - ); 203 + seq 204 + |> Seq.iter (fun result -> 205 + match result with Ok _ -> incr success | Error _ -> incr failure); 206 206 Printf.printf "Summary: %d documents (%d ok, %d error)\n" 207 207 (!success + !failure) !success !failure 208 208 ··· 231 231 prerr_endline " simple <file> - Decode person documents"; 232 232 prerr_endline " count <file> - Count documents"; 233 233 prerr_endline " errors <file> - Show success/error for each document"; 234 - prerr_endline " locations <file> - Test location tracking with locs=true"; 234 + prerr_endline 235 + " locations <file> - Test location tracking with locs=true"; 235 236 prerr_endline " json <file> - Roundtrip to JSON"; 236 237 prerr_endline " nested <file> - Decode nested objects"; 237 238 prerr_endline " arrays <file> - Decode arrays";
+2 -2
tests/bin/test_null_collections.ml
··· 84 84 in 85 85 match Yamlt.decode codec3 (Bytes.Reader.of_string yaml8) with 86 86 | Ok (name, items, tags) -> 87 - Printf.printf "Result: {name=%s; items_count=%d; tags_count=%d}\n" 88 - name (List.length items) (List.length tags) 87 + Printf.printf "Result: {name=%s; items_count=%d; tags_count=%d}\n" name 88 + (List.length items) (List.length tags) 89 89 | Error e -> Printf.printf "Error: %s\n" e
+19 -10
tests/bin/test_objects.ml
··· 105 105 let yaml = read_file file in 106 106 let json = read_file (file ^ ".json") in 107 107 let json_result = Jsont_bytesrw.decode_string M.settings_codec json in 108 - let yaml_result = Yamlt.decode M.settings_codec (Bytes.Reader.of_string yaml) in 108 + let yaml_result = 109 + Yamlt.decode M.settings_codec (Bytes.Reader.of_string yaml) 110 + in 109 111 110 112 show_result_both "settings" 111 113 (Result.map M.show json_result) ··· 138 140 let yaml = read_file file in 139 141 let json = read_file (file ^ ".json") in 140 142 let json_result = Jsont_bytesrw.decode_string M.employee_codec json in 141 - let yaml_result = Yamlt.decode M.employee_codec (Bytes.Reader.of_string yaml) in 143 + let yaml_result = 144 + Yamlt.decode M.employee_codec (Bytes.Reader.of_string yaml) 145 + in 142 146 143 147 show_result_both "employee" 144 148 (Result.map M.show json_result) ··· 176 180 let yaml = read_file file in 177 181 let json = read_file (file ^ ".json") in 178 182 let json_result = Jsont_bytesrw.decode_string M.flexible_codec json in 179 - let yaml_result = Yamlt.decode M.flexible_codec (Bytes.Reader.of_string yaml) in 183 + let yaml_result = 184 + Yamlt.decode M.flexible_codec (Bytes.Reader.of_string yaml) 185 + in 180 186 181 187 show_result_both "flexible" 182 188 (Result.map M.show json_result) ··· 244 250 (* Encode to YAML Block *) 245 251 (let b = Buffer.create 256 in 246 252 let writer = Bytes.Writer.of_buffer b in 247 - match Yamlt.encode ~format:Yamlt.Block M.person_codec person ~eod:true writer with 253 + match 254 + Yamlt.encode ~format:Yamlt.Block M.person_codec person ~eod:true writer 255 + with 248 256 | Ok () -> Printf.printf "YAML Block:\n%s" (Buffer.contents b) 249 257 | Error e -> Printf.printf "YAML Block ERROR: %s\n" e); 250 258 251 259 (* Encode to YAML Flow *) 252 260 let b = Buffer.create 256 in 253 261 let writer = Bytes.Writer.of_buffer b in 254 - match Yamlt.encode ~format:Yamlt.Flow M.person_codec person ~eod:true writer with 262 + match 263 + Yamlt.encode ~format:Yamlt.Flow M.person_codec person ~eod:true writer 264 + with 255 265 | Ok () -> Printf.printf "YAML Flow: %s" (Buffer.contents b) 256 266 | Error e -> Printf.printf "YAML Flow ERROR: %s\n" e 257 267 ··· 276 286 (* Decode from YAML *) 277 287 match Yamlt.decode M.flexible_codec (Bytes.Reader.of_string yaml) with 278 288 | Error e -> Printf.printf "Decode error: %s\n" e 279 - | Ok v -> 280 - Printf.printf "Decoded: name=%S, extra=%s\n" v.M.name (M.show_json v.M.extra); 289 + | Ok v -> ( 290 + Printf.printf "Decoded: name=%S, extra=%s\n" v.M.name 291 + (M.show_json v.M.extra); 281 292 282 293 (* Encode to YAML Block *) 283 294 let b = Buffer.create 256 in ··· 290 301 291 302 (* Re-decode the encoded YAML to verify roundtrip *) 292 303 let encoded = Buffer.contents b in 293 - (match 294 - Yamlt.decode M.flexible_codec (Bytes.Reader.of_string encoded) 295 - with 304 + match Yamlt.decode M.flexible_codec (Bytes.Reader.of_string encoded) with 296 305 | Error e -> Printf.printf "Re-decode error: %s\n" e 297 306 | Ok v2 -> 298 307 Printf.printf "Re-decoded: name=%S, extra=%s\n" v2.M.name
+8 -4
tests/bin/test_roundtrip.ml
··· 40 40 let yaml_block_encoded = 41 41 let b = Buffer.create 256 in 42 42 let writer = Bytes.Writer.of_buffer b in 43 - match Yamlt.encode ~format:Yamlt.Block M.data_codec original ~eod:true writer with 43 + match 44 + Yamlt.encode ~format:Yamlt.Block M.data_codec original ~eod:true writer 45 + with 44 46 | Ok () -> Ok (Buffer.contents b) 45 47 | Error e -> Error e 46 48 in 47 49 let yaml_block_decoded = 48 50 Result.bind yaml_block_encoded (fun yaml -> 49 - Yamlt.decode M.data_codec (Bytes.Reader.of_string yaml)) 51 + Yamlt.decode M.data_codec (Bytes.Reader.of_string yaml)) 50 52 in 51 53 (match yaml_block_decoded with 52 54 | Ok decoded when M.equal original decoded -> ··· 58 60 let yaml_flow_encoded = 59 61 let b = Buffer.create 256 in 60 62 let writer = Bytes.Writer.of_buffer b in 61 - match Yamlt.encode ~format:Yamlt.Flow M.data_codec original ~eod:true writer with 63 + match 64 + Yamlt.encode ~format:Yamlt.Flow M.data_codec original ~eod:true writer 65 + with 62 66 | Ok () -> Ok (Buffer.contents b) 63 67 | Error e -> Error e 64 68 in 65 69 let yaml_flow_decoded = 66 70 Result.bind yaml_flow_encoded (fun yaml -> 67 - Yamlt.decode M.data_codec (Bytes.Reader.of_string yaml)) 71 + Yamlt.decode M.data_codec (Bytes.Reader.of_string yaml)) 68 72 in 69 73 match yaml_flow_decoded with 70 74 | Ok decoded when M.equal original decoded ->