forked from
anil.recoil.org/ocaml-jmap
this repo has no description
1(*---------------------------------------------------------------------------
2 Copyright (c) 2025 Anil Madhavapeddy. All rights reserved.
3 SPDX-License-Identifier: ISC
4 ---------------------------------------------------------------------------*)
5
6type t = {
7 method_responses : Proto_invocation.t list;
8 created_ids : (Proto_id.t * Proto_id.t) list option;
9 session_state : string;
10}
11
12let method_responses t = t.method_responses
13let created_ids t = t.created_ids
14let session_state t = t.session_state
15
16let make method_responses created_ids session_state =
17 { method_responses; created_ids; session_state }
18
19let jsont =
20 let kind = "Response" in
21 Jsont.Object.map ~kind make
22 |> Jsont.Object.mem "methodResponses" (Jsont.list Proto_invocation.jsont) ~enc:method_responses
23 |> Jsont.Object.opt_mem "createdIds" (Proto_json_map.of_id Proto_id.jsont) ~enc:created_ids
24 |> Jsont.Object.mem "sessionState" Jsont.string ~enc:session_state
25 |> Jsont.Object.finish
26
27let find_response method_call_id response =
28 List.find_opt
29 (fun inv -> Proto_invocation.method_call_id inv = method_call_id)
30 response.method_responses
31
32let get_response method_call_id response =
33 match find_response method_call_id response with
34 | Some inv -> inv
35 | None -> raise Not_found
36
37let is_error invocation =
38 String.equal (Proto_invocation.name invocation) "error"
39
40let get_error invocation =
41 if is_error invocation then
42 match Jsont.Json.decode' Proto_error.method_error_jsont (Proto_invocation.arguments invocation) with
43 | Ok v -> Some v
44 | Error _ -> None
45 else
46 None