aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThomas Letan <lthms@soap.coffee>2022-08-21 15:43:10 +0200
committerThomas Letan <lthms@soap.coffee>2022-08-21 15:43:10 +0200
commit5ad55e4d81a3193df78d2cf80e2bd4f64a669058 (patch)
treeb70c18e0ddb40760bf0beb46df936ed90baacbc5
parentLicense the source of this repository under the MPL 2.0 (diff)
Turn Json_decoder.of_string exception-safe
-rw-r--r--lib/json_decoder/json_decoder.ml3
-rw-r--r--lib/sway_ipc/sway_ipc.ml2
-rw-r--r--lib/sway_ipc_types/event.ml2
3 files changed, 4 insertions, 3 deletions
diff --git a/lib/json_decoder/json_decoder.ml b/lib/json_decoder/json_decoder.ml
index 1f84820..acedc76 100644
--- a/lib/json_decoder/json_decoder.ml
+++ b/lib/json_decoder/json_decoder.ml
@@ -45,4 +45,5 @@ let float = Ezjsonm.get_float
let rec mu : ('a t -> 'a t) -> 'a t =
fun f_enc value -> (f_enc (mu f_enc)) value
-let of_string dec str = Ezjsonm.value_from_string str |> dec
+let of_string_exn dec str = Ezjsonm.value_from_string str |> dec
+let of_string dec str = try Some (of_string_exn dec str) with _ -> None
diff --git a/lib/sway_ipc/sway_ipc.ml b/lib/sway_ipc/sway_ipc.ml
index ccf0956..5ddf41c 100644
--- a/lib/sway_ipc/sway_ipc.ml
+++ b/lib/sway_ipc/sway_ipc.ml
@@ -28,7 +28,7 @@ let send_command ?socket cmd =
let* () = Socket.write_raw_message socket raw in
let* op', payload = Socket.read_raw_message socket in
assert (op = op');
- Lwt.return @@ Json_decoder.of_string (Message.reply_decoder cmd) payload
+ Lwt.return @@ Json_decoder.of_string_exn (Message.reply_decoder cmd) payload
let subscribe ?socket events =
let open Lwt.Syntax in
diff --git a/lib/sway_ipc_types/event.ml b/lib/sway_ipc_types/event.ml
index 1176a29..ffaab70 100644
--- a/lib/sway_ipc_types/event.ml
+++ b/lib/sway_ipc_types/event.ml
@@ -160,4 +160,4 @@ let decoder (code : event_type) =
let event_of_raw_message (opc, payload) =
let ev = event_type_of_code opc in
- Json_decoder.of_string (decoder ev) payload
+ Json_decoder.of_string_exn (decoder ev) payload