OCaml client library for Claude Code
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

json: drop [encode] result form; rename [encode_exn] to [encode]

Per the ocaml-encodings convention, encoding never has a meaningful
runtime failure mode: the encoder walks the GADT and invokes
user-supplied [enc] callbacks. The only failure cases are (a) a codec
built around [Codec.ignore] (programmer error), or (b) a user-
supplied [enc] that raises (user's own bug). Neither is something a
caller should recover from, so the [result] variant was dead API
surface and every caller I've seen either [get_ok]-ed it or fell
back to [Json.Null] on error.

Drop [Codec.encode] / [Json.encode] result-returning, rename the
exception-raising [encode_exn] to just [encode]. [decode] keeps both
forms since malformed JSON is a legitimate runtime condition.

Downstream sweep: 28 files across claude / oci / atp / qemu / scitt /
yaml. Most were pattern A ([Error _ -> Json.Null ((), Meta.none)]) or
pattern B (print the error); both collapse to a direct call. Tests
using pattern F ([match Json.encode ... with Ok j -> ... | Error e
-> Alcotest.fail]) collapse to [let j = Json.encode ... in]. A few
helpers in claude/client.ml and the atp shims kept their names as
thin aliases to preserve grep targets.

+136 -219
+25 -29
examples/structured_error_demo.ml
··· 29 29 Claude.Control.Response.error ~request_id:"test-123" ~error:error2 () 30 30 in 31 31 32 - match Json.encode Claude.Control.Response.json error_resp with 33 - | Ok json -> ( 34 - let json_str = Json.Value.to_string json in 35 - Fmt.pr "✓ Encoded error response: %s\n" json_str; 32 + let json = Json.encode Claude.Control.Response.json error_resp in 33 + let json_str = Json.Value.to_string json in 34 + Fmt.pr "✓ Encoded error response: %s\n" json_str; 36 35 37 - (* Decode it back *) 38 - match Json.decode Claude.Control.Response.json json with 39 - | Ok (Claude.Control.Response.Error decoded) -> 40 - Fmt.pr "✓ Decoded error: [%d] %s\n" decoded.error.code 41 - decoded.error.message 42 - | Ok _ -> print_endline "✗ Wrong response type" 43 - | Error e -> Fmt.pr "✗ Decode failed: %s\n" (Json.Error.to_string e)) 44 - | Error e -> Fmt.pr "✗ Encode failed: %s\n" (Json.Error.to_string e) 36 + (* Decode it back *) 37 + match Json.decode Claude.Control.Response.json json with 38 + | Ok (Claude.Control.Response.Error decoded) -> 39 + Fmt.pr "✓ Decoded error: [%d] %s\n" decoded.error.code 40 + decoded.error.message 41 + | Ok _ -> print_endline "✗ Wrong response type" 42 + | Error e -> Fmt.pr "✗ Decode failed: %s\n" (Json.Error.to_string e) 45 43 46 44 let test_error_code_conventions () = 47 45 print_endline "\nTesting JSON-RPC error code conventions..."; ··· 155 153 () 156 154 in 157 155 158 - match Json.encode Claude.Control.Response.json error_response with 159 - | Ok json -> ( 160 - let json_str = Json.Value.to_string json in 161 - Fmt.pr "✓ Encoded control error with data:\n %s\n" json_str; 156 + let json = Json.encode Claude.Control.Response.json error_response in 157 + let json_str = Json.Value.to_string json in 158 + Fmt.pr "✓ Encoded control error with data:\n %s\n" json_str; 162 159 163 - (* Verify we can decode it back *) 164 - match Json.decode Claude.Control.Response.json json with 165 - | Ok (Claude.Control.Response.Error decoded) -> ( 166 - Fmt.pr "✓ Decoded control error:\n"; 167 - Fmt.pr " Code: %d\n" decoded.error.code; 168 - Fmt.pr " Message: %s\n" decoded.error.message; 169 - Fmt.pr " Has data: %b\n" (Option.is_some decoded.error.data); 170 - match decoded.error.data with 171 - | Some data -> Fmt.pr " Data: %s\n" (Json.Value.to_string data) 172 - | None -> ()) 173 - | Ok _ -> print_endline "✗ Wrong response type" 174 - | Error e -> Fmt.pr "✗ Decode failed: %s\n" (Json.Error.to_string e)) 175 - | Error e -> Fmt.pr "✗ Encode failed: %s\n" (Json.Error.to_string e) 160 + (* Verify we can decode it back *) 161 + match Json.decode Claude.Control.Response.json json with 162 + | Ok (Claude.Control.Response.Error decoded) -> ( 163 + Fmt.pr "✓ Decoded control error:\n"; 164 + Fmt.pr " Code: %d\n" decoded.error.code; 165 + Fmt.pr " Message: %s\n" decoded.error.message; 166 + Fmt.pr " Has data: %b\n" (Option.is_some decoded.error.data); 167 + match decoded.error.data with 168 + | Some data -> Fmt.pr " Data: %s\n" (Json.Value.to_string data) 169 + | None -> ()) 170 + | Ok _ -> print_endline "✗ Wrong response type" 171 + | Error e -> Fmt.pr "✗ Decode failed: %s\n" (Json.Error.to_string e) 176 172 177 173 let process_hook_responses messages = 178 174 let hook_called = ref false in
+3 -18
lib/client.ml
··· 7 7 8 8 module Log = (val Logs.src_log src : Logs.LOG) 9 9 10 - let encode_or_raise ~msg codec v = 11 - Json.encode codec v |> Result.map_error Json.Error.to_string |> Error.ok ~msg 10 + let encode_or_raise ~msg:_ codec v = Json.encode codec v 12 11 13 12 (** Control response builders using Control codecs *) 14 13 module Control_response = struct ··· 40 39 |> Object.seal 41 40 42 41 let encode matchers = 43 - List.map 44 - (fun m -> 45 - Json.encode json m 46 - |> Result.map_error Json.Error.to_string 47 - |> Error.ok ~msg:"Hook_matcher_wire.encode: ") 48 - matchers 49 - |> Json.Value.list 42 + List.map (fun m -> Json.encode json m) matchers |> Json.Value.list 50 43 end 51 44 52 45 type t = { ··· 96 89 let lib_result = 97 90 Permissions.Decision.to_proto_result ~original_input:input decision 98 91 in 99 - let response_data = 100 - match Json.encode Permissions.Result.json lib_result with 101 - | Ok json -> json 102 - | Error err -> 103 - Log.err (fun m -> 104 - m "Failed to encode permission result: %s" 105 - (Json.Error.to_string err)); 106 - failwith "Permission result encoding failed" 107 - in 92 + let response_data = Json.encode Permissions.Result.json lib_result in 108 93 let response = 109 94 Control_response.success ~request_id ~response:(Some response_data) 110 95 in
+1 -5
lib/hooks.ml
··· 380 380 Log.err (fun m -> m "%s: failed to decode input: %s" name msg); 381 381 raise (Invalid_argument (name ^ " input: " ^ msg)) 382 382 383 - let encode_output name codec output = 384 - match Json.encode codec output with 385 - | Ok v -> v 386 - | Error err -> 387 - failwith (name ^ " output encoding: " ^ Json.Error.to_string err) 383 + let encode_output _name codec output = Json.encode codec output 388 384 389 385 let wire_callback ~name ~input_jsont ~output_jsont ~should_block callback json = 390 386 let typed_input = decode_input name input_jsont json in
+5 -23
lib/message.ml
··· 53 53 | String s -> Json.String (s, Json.Meta.none) 54 54 | Blocks blocks -> 55 55 let jsons = 56 - List.map 57 - (fun b -> 58 - match Json.encode Content_block.json b with 59 - | Ok json -> json 60 - | Error e -> 61 - invalid_arg ("encode_content: " ^ Json.Error.to_string e)) 62 - blocks 56 + List.map (fun b -> Json.encode Content_block.json b) blocks 63 57 in 64 58 Json.Array (jsons, Json.Meta.none) 65 59 ··· 99 93 |> Object.member "message" message_jsont ~enc:Fun.id 100 94 |> Object.seal 101 95 102 - let to_json t = 103 - match Json.encode json t with 104 - | Ok json -> json 105 - | Error e -> invalid_arg ("User.to_json: " ^ Json.Error.to_string e) 96 + let to_json t = Json.encode json t 106 97 end 107 98 108 99 module Assistant = struct ··· 178 169 |> Object.member "message" json ~enc:Fun.id 179 170 |> Object.seal 180 171 181 - let to_json t = 182 - match Json.encode json t with 183 - | Ok json -> json 184 - | Error e -> invalid_arg ("Assistant.to_json: " ^ Json.Error.to_string e) 172 + let to_json t = Json.encode json t 185 173 end 186 174 187 175 module System = struct ··· 243 231 ~tag_to_string:Fun.id ~tag_compare:String.compare 244 232 |> Object.seal 245 233 246 - let to_json t = 247 - match Json.encode json t with 248 - | Ok json -> json 249 - | Error e -> invalid_arg ("System.to_json: " ^ Json.Error.to_string e) 234 + let to_json t = Json.encode json t 250 235 end 251 236 252 237 module Result = struct ··· 376 361 |> Object.keep_unknown Unknown.mems ~enc:unknown 377 362 |> Object.seal 378 363 379 - let to_json t = 380 - match Json.encode json t with 381 - | Ok json -> json 382 - | Error e -> invalid_arg ("Result.to_json: " ^ Json.Error.to_string e) 364 + let to_json t = Json.encode json t 383 365 end 384 366 385 367 type t =
+1 -5
lib/outgoing.ml
··· 61 61 |> Object.seal 62 62 63 63 let pp ppf t = Json.pp_value json () ppf t 64 - 65 - let to_json t = 66 - match Json.encode json t with 67 - | Ok json -> json 68 - | Error e -> invalid_arg ("to_json: " ^ Json.Error.to_string e) 64 + let to_json t = Json.encode json t 69 65 70 66 let of_json v = 71 67 match Json.decode json v with
+1 -5
lib/structured_output.ml
··· 26 26 |> Object.member "jsonSchema" Value.t ~enc:(fun t -> t.json_schema) 27 27 |> Object.seal 28 28 29 - let to_json t = 30 - match Json.encode json t with 31 - | Ok json -> json 32 - | Error err -> 33 - failwith ("Structured_output.to_json: " ^ Json.Error.to_string err) 29 + let to_json t = Json.encode json t 34 30 35 31 let of_json v = 36 32 match Json.decode json v with
+8 -10
test/test_claude.ml
··· 449 449 let error_resp = 450 450 Claude.Control.Response.error ~request_id:"test-123" ~error:error_detail () 451 451 in 452 - match Json.encode Claude.Control.Response.json error_resp with 453 - | Ok json -> ( 454 - match Json.decode Claude.Control.Response.json json with 455 - | Ok (Claude.Control.Response.Error decoded) -> 456 - Alcotest.(check string) "request_id" "test-123" decoded.request_id; 457 - Alcotest.(check int) "error code" (-32602) decoded.error.code; 458 - Alcotest.(check string) 459 - "error message" "Invalid parameters" decoded.error.message 460 - | Ok _ -> Alcotest.fail "Wrong response type decoded" 461 - | Error e -> Alcotest.fail (Json.Error.to_string e)) 452 + let json = Json.encode Claude.Control.Response.json error_resp in 453 + match Json.decode Claude.Control.Response.json json with 454 + | Ok (Claude.Control.Response.Error decoded) -> 455 + Alcotest.(check string) "request_id" "test-123" decoded.request_id; 456 + Alcotest.(check int) "error code" (-32602) decoded.error.code; 457 + Alcotest.(check string) 458 + "error message" "Invalid parameters" decoded.error.message 459 + | Ok _ -> Alcotest.fail "Wrong response type decoded" 462 460 | Error e -> Alcotest.fail (Json.Error.to_string e) 463 461 464 462 let structured_error_tests =
+4 -6
test/test_content_block.ml
··· 62 62 | _ -> Alcotest.fail "Expected Thinking block" 63 63 64 64 let json_roundtrip block = 65 - match Json.encode CB.json block with 66 - | Ok json -> ( 67 - match Json.decode CB.json json with 68 - | Ok back -> back 69 - | Error e -> Alcotest.fail (Json.Error.to_string e)) 70 - | Error e -> Alcotest.fail (Loc.Error.to_string e) 65 + let json = Json.encode CB.json block in 66 + match Json.decode CB.json json with 67 + | Ok back -> back 68 + | Error e -> Alcotest.fail (Json.Error.to_string e) 71 69 72 70 let test_jsont_roundtrip_text () = 73 71 let block = CB.text "roundtrip test" in
+70 -94
test/test_control.ml
··· 88 88 89 89 let test_request_jsont_interrupt () = 90 90 let req = C.Request.interrupt () in 91 - match Json.encode C.Request.json req with 92 - | Ok json -> ( 93 - match Json.decode C.Request.json json with 94 - | Ok (C.Request.Interrupt _) -> () 95 - | Ok _ -> Alcotest.fail "Wrong variant" 96 - | Error e -> Alcotest.fail (Json.Error.to_string e)) 97 - | Error e -> Alcotest.fail (Loc.Error.to_string e) 91 + let json = Json.encode C.Request.json req in 92 + match Json.decode C.Request.json json with 93 + | Ok (C.Request.Interrupt _) -> () 94 + | Ok _ -> Alcotest.fail "Wrong variant" 95 + | Error e -> Alcotest.fail (Json.Error.to_string e) 98 96 99 97 let test_request_jsont_permission () = 100 98 let input = ··· 102 100 [ Json.Value.member (Json.Value.name "cmd") (Json.Value.string "ls") ] 103 101 in 104 102 let req = C.Request.permission ~tool_name:"Bash" ~input () in 105 - match Json.encode C.Request.json req with 106 - | Ok json -> ( 107 - match Json.decode C.Request.json json with 108 - | Ok (C.Request.Permission p) -> 109 - Alcotest.(check string) "tool_name" "Bash" p.tool_name 110 - | Ok _ -> Alcotest.fail "Wrong variant" 111 - | Error e -> Alcotest.fail (Json.Error.to_string e)) 112 - | Error e -> Alcotest.fail (Loc.Error.to_string e) 103 + let json = Json.encode C.Request.json req in 104 + match Json.decode C.Request.json json with 105 + | Ok (C.Request.Permission p) -> 106 + Alcotest.(check string) "tool_name" "Bash" p.tool_name 107 + | Ok _ -> Alcotest.fail "Wrong variant" 108 + | Error e -> Alcotest.fail (Json.Error.to_string e) 113 109 114 110 let test_request_jsont_set_model () = 115 111 let req = C.Request.set_model ~model:"claude-haiku-4" () in 116 - match Json.encode C.Request.json req with 117 - | Ok json -> ( 118 - match Json.decode C.Request.json json with 119 - | Ok (C.Request.Set_model sm) -> 120 - Alcotest.(check string) "model" "claude-haiku-4" sm.model 121 - | Ok _ -> Alcotest.fail "Wrong variant" 122 - | Error e -> Alcotest.fail (Json.Error.to_string e)) 123 - | Error e -> Alcotest.fail (Loc.Error.to_string e) 112 + let json = Json.encode C.Request.json req in 113 + match Json.decode C.Request.json json with 114 + | Ok (C.Request.Set_model sm) -> 115 + Alcotest.(check string) "model" "claude-haiku-4" sm.model 116 + | Ok _ -> Alcotest.fail "Wrong variant" 117 + | Error e -> Alcotest.fail (Json.Error.to_string e) 124 118 125 119 let test_request_jsont_get_server_info () = 126 120 let req = C.Request.get_server_info () in 127 - match Json.encode C.Request.json req with 128 - | Ok json -> ( 129 - match Json.decode C.Request.json json with 130 - | Ok (C.Request.Get_server_info _) -> () 131 - | Ok _ -> Alcotest.fail "Wrong variant" 132 - | Error e -> Alcotest.fail (Json.Error.to_string e)) 133 - | Error e -> Alcotest.fail (Loc.Error.to_string e) 121 + let json = Json.encode C.Request.json req in 122 + match Json.decode C.Request.json json with 123 + | Ok (C.Request.Get_server_info _) -> () 124 + | Ok _ -> Alcotest.fail "Wrong variant" 125 + | Error e -> Alcotest.fail (Json.Error.to_string e) 134 126 135 127 let test_request_jsont_hook_callback () = 136 128 let input = Json.Value.object' [] in 137 129 let req = C.Request.hook_callback ~callback_id:"cb-1" ~input () in 138 - match Json.encode C.Request.json req with 139 - | Ok json -> ( 140 - match Json.decode C.Request.json json with 141 - | Ok (C.Request.Hook_callback hc) -> 142 - Alcotest.(check string) "callback_id" "cb-1" hc.callback_id 143 - | Ok _ -> Alcotest.fail "Wrong variant" 144 - | Error e -> Alcotest.fail (Json.Error.to_string e)) 145 - | Error e -> Alcotest.fail (Loc.Error.to_string e) 130 + let json = Json.encode C.Request.json req in 131 + match Json.decode C.Request.json json with 132 + | Ok (C.Request.Hook_callback hc) -> 133 + Alcotest.(check string) "callback_id" "cb-1" hc.callback_id 134 + | Ok _ -> Alcotest.fail "Wrong variant" 135 + | Error e -> Alcotest.fail (Json.Error.to_string e) 146 136 147 137 let test_request_jsont_mcp_message () = 148 138 let message = Json.Value.object' [] in 149 139 let req = C.Request.mcp_message ~server_name:"tools" ~message () in 150 - match Json.encode C.Request.json req with 151 - | Ok json -> ( 152 - match Json.decode C.Request.json json with 153 - | Ok (C.Request.Mcp_message mm) -> 154 - Alcotest.(check string) "server_name" "tools" mm.server_name 155 - | Ok _ -> Alcotest.fail "Wrong variant" 156 - | Error e -> Alcotest.fail (Json.Error.to_string e)) 157 - | Error e -> Alcotest.fail (Loc.Error.to_string e) 140 + let json = Json.encode C.Request.json req in 141 + match Json.decode C.Request.json json with 142 + | Ok (C.Request.Mcp_message mm) -> 143 + Alcotest.(check string) "server_name" "tools" mm.server_name 144 + | Ok _ -> Alcotest.fail "Wrong variant" 145 + | Error e -> Alcotest.fail (Json.Error.to_string e) 158 146 159 147 let test_response_jsont_success () = 160 148 let resp = C.Response.success ~request_id:"r1" () in 161 - match Json.encode C.Response.json resp with 162 - | Ok json -> ( 163 - match Json.decode C.Response.json json with 164 - | Ok (C.Response.Success s) -> 165 - Alcotest.(check string) "request_id" "r1" s.request_id 166 - | Ok _ -> Alcotest.fail "Wrong variant" 167 - | Error e -> Alcotest.fail (Json.Error.to_string e)) 168 - | Error e -> Alcotest.fail (Loc.Error.to_string e) 149 + let json = Json.encode C.Response.json resp in 150 + match Json.decode C.Response.json json with 151 + | Ok (C.Response.Success s) -> 152 + Alcotest.(check string) "request_id" "r1" s.request_id 153 + | Ok _ -> Alcotest.fail "Wrong variant" 154 + | Error e -> Alcotest.fail (Json.Error.to_string e) 169 155 170 156 let test_response_success_data () = 171 157 let data = Json.Value.string "result_data" in 172 158 let resp = C.Response.success ~request_id:"r2" ~response:data () in 173 - match Json.encode C.Response.json resp with 174 - | Ok json -> ( 175 - match Json.decode C.Response.json json with 176 - | Ok (C.Response.Success s) -> 177 - Alcotest.(check bool) "has response" true (Option.is_some s.response) 178 - | Ok _ -> Alcotest.fail "Wrong variant" 179 - | Error e -> Alcotest.fail (Json.Error.to_string e)) 180 - | Error e -> Alcotest.fail (Loc.Error.to_string e) 159 + let json = Json.encode C.Response.json resp in 160 + match Json.decode C.Response.json json with 161 + | Ok (C.Response.Success s) -> 162 + Alcotest.(check bool) "has response" true (Option.is_some s.response) 163 + | Ok _ -> Alcotest.fail "Wrong variant" 164 + | Error e -> Alcotest.fail (Json.Error.to_string e) 181 165 182 166 let test_response_jsont_error () = 183 167 let detail = 184 168 C.Response.error_detail ~code:`Internal_error ~message:"oops" () 185 169 in 186 170 let resp = C.Response.error ~request_id:"r3" ~error:detail () in 187 - match Json.encode C.Response.json resp with 188 - | Ok json -> ( 189 - match Json.decode C.Response.json json with 190 - | Ok (C.Response.Error e) -> 191 - Alcotest.(check string) "request_id" "r3" e.request_id; 192 - Alcotest.(check int) "code" (-32603) e.error.code; 193 - Alcotest.(check string) "message" "oops" e.error.message 194 - | Ok _ -> Alcotest.fail "Wrong variant" 195 - | Error e -> Alcotest.fail (Json.Error.to_string e)) 196 - | Error e -> Alcotest.fail (Loc.Error.to_string e) 171 + let json = Json.encode C.Response.json resp in 172 + match Json.decode C.Response.json json with 173 + | Ok (C.Response.Error e) -> 174 + Alcotest.(check string) "request_id" "r3" e.request_id; 175 + Alcotest.(check int) "code" (-32603) e.error.code; 176 + Alcotest.(check string) "message" "oops" e.error.message 177 + | Ok _ -> Alcotest.fail "Wrong variant" 178 + | Error e -> Alcotest.fail (Json.Error.to_string e) 197 179 198 180 let test_server_info () = 199 181 let info = ··· 212 194 C.Server_info.create ~version:"1.0.0" ~capabilities:[ "mcp" ] ~commands:[] 213 195 ~output_styles:[] () 214 196 in 215 - match Json.encode C.Server_info.json info with 216 - | Ok json -> ( 217 - match Json.decode C.Server_info.json json with 218 - | Ok back -> 219 - Alcotest.(check string) "version" "1.0.0" (C.Server_info.version back) 220 - | Error e -> Alcotest.fail (Json.Error.to_string e)) 221 - | Error e -> Alcotest.fail (Loc.Error.to_string e) 197 + let json = Json.encode C.Server_info.json info in 198 + match Json.decode C.Server_info.json json with 199 + | Ok back -> 200 + Alcotest.(check string) "version" "1.0.0" (C.Server_info.version back) 201 + | Error e -> Alcotest.fail (Json.Error.to_string e) 222 202 223 203 let test_request_envelope () = 224 204 let req = C.Request.interrupt () in ··· 245 225 unknown = Claude.Unknown.empty; 246 226 } 247 227 in 248 - match Json.encode C.control_request_jsont env with 249 - | Ok json -> ( 250 - match Json.decode C.control_request_jsont json with 251 - | Ok back -> Alcotest.(check string) "request_id" "env-1" back.request_id 252 - | Error e -> Alcotest.fail (Json.Error.to_string e)) 253 - | Error e -> Alcotest.fail (Loc.Error.to_string e) 228 + let json = Json.encode C.control_request_jsont env in 229 + match Json.decode C.control_request_jsont json with 230 + | Ok back -> Alcotest.(check string) "request_id" "env-1" back.request_id 231 + | Error e -> Alcotest.fail (Json.Error.to_string e) 254 232 255 233 let test_response_envelope_jsont () = 256 234 let resp = C.Response.success ~request_id:"x" () in ··· 261 239 unknown = Claude.Unknown.empty; 262 240 } 263 241 in 264 - match Json.encode C.control_response_jsont env with 265 - | Ok json -> ( 266 - match Json.decode C.control_response_jsont json with 267 - | Ok back -> ( 268 - match back.response with 269 - | C.Response.Success _ -> () 270 - | _ -> Alcotest.fail "Wrong variant") 271 - | Error e -> Alcotest.fail (Json.Error.to_string e)) 272 - | Error e -> Alcotest.fail (Loc.Error.to_string e) 242 + let json = Json.encode C.control_response_jsont env in 243 + match Json.decode C.control_response_jsont json with 244 + | Ok back -> ( 245 + match back.response with 246 + | C.Response.Success _ -> () 247 + | _ -> Alcotest.fail "Wrong variant") 248 + | Error e -> Alcotest.fail (Json.Error.to_string e) 273 249 274 250 let suite = 275 251 ( "control",
+10 -14
test/test_outgoing.ml
··· 41 41 let test_jsont_roundtrip_user () = 42 42 let user = M.User.of_string "test" in 43 43 let msg = O.Message (M.User user) in 44 - match Json.encode O.json msg with 45 - | Ok json -> ( 46 - match Json.decode O.json json with 47 - | Ok (O.Message (M.User _)) -> () 48 - | Ok _ -> Alcotest.fail "Wrong variant after decode" 49 - | Error e -> Alcotest.fail (Json.Error.to_string e)) 50 - | Error e -> Alcotest.fail (Loc.Error.to_string e) 44 + let json = Json.encode O.json msg in 45 + match Json.decode O.json json with 46 + | Ok (O.Message (M.User _)) -> () 47 + | Ok _ -> Alcotest.fail "Wrong variant after decode" 48 + | Error e -> Alcotest.fail (Json.Error.to_string e) 51 49 52 50 let test_jsont_roundtrip_control_response () = 53 51 let resp = C.Response.success ~request_id:"r2" () in 54 52 let envelope = mk_control_response resp in 55 53 let msg = O.Control_response envelope in 56 - match Json.encode O.json msg with 57 - | Ok json -> ( 58 - match Json.decode O.json json with 59 - | Ok (O.Control_response _) -> () 60 - | Ok _ -> Alcotest.fail "Wrong variant after decode" 61 - | Error e -> Alcotest.fail (Json.Error.to_string e)) 62 - | Error e -> Alcotest.fail (Loc.Error.to_string e) 54 + let json = Json.encode O.json msg in 55 + match Json.decode O.json json with 56 + | Ok (O.Control_response _) -> () 57 + | Ok _ -> Alcotest.fail "Wrong variant after decode" 58 + | Error e -> Alcotest.fail (Json.Error.to_string e) 63 59 64 60 let test_pp_does_not_crash () = 65 61 let user = M.User.of_string "pp test" in
+1 -1
test/test_permissions.ml
··· 76 76 77 77 let test_rule_proto_roundtrip () = 78 78 let rule = P.Rule.create ~tool_name:"Read" ~rule_content:"*.txt" () in 79 - let json = Json.encode P.Rule.json rule |> Result.get_ok in 79 + let json = Json.encode P.Rule.json rule in 80 80 let back = Json.decode P.Rule.json json |> Result.get_ok in 81 81 Alcotest.(check string) "tool_name" "Read" (P.Rule.tool_name back); 82 82 Alcotest.(check (option string))
+7 -9
test/test_structured_output.ml
··· 41 41 let test_jsont_encode_decode () = 42 42 let schema = J.object' [ J.member (J.name "type") (J.string "string") ] in 43 43 let so = SO.of_json_schema schema in 44 - match Json.encode SO.json so with 45 - | Ok json -> ( 46 - match Json.decode SO.json json with 47 - | Ok back -> ( 48 - match SO.json_schema back with 49 - | Json.Object _ -> () 50 - | _ -> Alcotest.fail "Expected object after decode") 51 - | Error e -> Alcotest.fail (Json.Error.to_string e)) 52 - | Error e -> Alcotest.fail (Loc.Error.to_string e) 44 + let json = Json.encode SO.json so in 45 + match Json.decode SO.json json with 46 + | Ok back -> ( 47 + match SO.json_schema back with 48 + | Json.Object _ -> () 49 + | _ -> Alcotest.fail "Expected object after decode") 50 + | Error e -> Alcotest.fail (Json.Error.to_string e) 53 51 54 52 let test_simple_string_schema () = 55 53 let schema = J.object' [ J.member (J.name "type") (J.string "string") ] in