Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 1 addition & 19 deletions .ocamlformat
Original file line number Diff line number Diff line change
@@ -1,21 +1,3 @@
version=0.26.2
profile=conventional
profile=janestreet
ocaml-version=4.14.0
break-separators=before
dock-collection-brackets=false
break-sequences=true
doc-comments=before
field-space=loose
let-and=sparse
sequence-style=terminator
type-decl=sparse
let-and=sparse
space-around-records
space-around-lists
space-around-arrays
cases-exp-indent=2
break-cases=fit-or-vertical
indicate-nested-or-patterns=unsafe-no
parse-docstrings=true
module-item-spacing=sparse
wrap-fun-args=false
26 changes: 12 additions & 14 deletions fiber-test/fiber_test.ml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
open Stdune

let print pp = Format.printf "%a@." Pp.to_fmt pp

let print_dyn dyn = print (Dyn.pp dyn)

module Scheduler : sig
Expand All @@ -10,26 +9,25 @@ module Scheduler : sig
exception Never

val create : unit -> t

val run : t -> 'a Fiber.t -> 'a
end = struct
type t = unit Fiber.Ivar.t Queue.t

let t_var = Fiber.Var.create ()

let create () = Queue.create ()

exception Never

let run t fiber =
let fiber = Fiber.Var.set t_var t (fun () -> fiber) in
Fiber.run fiber ~iter:(fun () ->
let next =
match Queue.pop t with
| None -> raise Never
| Some e -> Fiber.Fill (e, ())
in
Nonempty_list.[ next ])
let next =
match Queue.pop t with
| None -> raise Never
| Some e -> Fiber.Fill (e, ())
in
Nonempty_list.[ next ])
;;
end

let test ?(expect_never = false) to_dyn f =
Expand All @@ -41,14 +39,14 @@ let test ?(expect_never = false) to_dyn f =
in
Fiber.with_error_handler f ~on_error
in
(try Scheduler.run (Scheduler.create ()) f |> to_dyn |> print_dyn
with Scheduler.Never -> never_raised := true);
match (!never_raised, expect_never) with
(try Scheduler.run (Scheduler.create ()) f |> to_dyn |> print_dyn with
| Scheduler.Never -> never_raised := true);
match !never_raised, expect_never with
| false, false ->
(* We don't raise in this case b/c we assume something else is being
tested *)
()
| true, true -> print_endline "[PASS] Never raised as expected"
| false, true ->
print_endline "[FAIL] expected Never to be raised but it wasn't"
| false, true -> print_endline "[FAIL] expected Never to be raised but it wasn't"
| true, false -> print_endline "[FAIL] unexpected Never raised"
;;
32 changes: 16 additions & 16 deletions jsonrpc-fiber/src/import.ml
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ module Json = struct
type t = Ppx_yojson_conv_lib.Yojson.Safe.t

let to_pretty_string (t : t) = Yojson.Safe.pretty_to_string ~std:false t

let error = Ppx_yojson_conv_lib.Yojson_conv.of_yojson_error

let pp ppf (t : t) = Yojson.Safe.pretty_print ppf t

let rec of_dyn (t : Dyn.t) : t =
Expand All @@ -44,16 +42,15 @@ module Json = struct
| List xs -> `List (List.map ~f:of_dyn xs)
| Array xs -> `List (List.map ~f:of_dyn (Array.to_list xs))
| Tuple xs -> `List (List.map ~f:of_dyn xs)
| Record r -> `Assoc (List.map r ~f:(fun (k, v) -> (k, of_dyn v)))
| Variant (name, args) -> `Assoc [ (name, of_dyn (List args)) ]
| Record r -> `Assoc (List.map r ~f:(fun (k, v) -> k, of_dyn v))
| Variant (name, args) -> `Assoc [ name, of_dyn (List args) ]
| Set xs -> `List (List.map ~f:of_dyn xs)
| Map map ->
`List (List.map map ~f:(fun (k, v) -> `List [ of_dyn k; of_dyn v ]))
| Map map -> `List (List.map map ~f:(fun (k, v) -> `List [ of_dyn k; of_dyn v ]))
;;
end

module Log = struct
let level : (string option -> bool) ref = ref (fun _ -> false)

let out = ref Format.err_formatter

type message =
Expand All @@ -64,22 +61,25 @@ module Log = struct
let msg message payload = { message; payload }

let log ?section k =
if !level section then (
if !level section
then (
let message = k () in
(match section with
| None -> Format.fprintf !out "%s@." message.message
| Some section -> Format.fprintf !out "[%s] %s@." section message.message);
| None -> Format.fprintf !out "%s@." message.message
| Some section -> Format.fprintf !out "[%s] %s@." section message.message);
(match message.payload with
| [] -> ()
| fields -> Format.fprintf !out "%a@." Json.pp (`Assoc fields));
| [] -> ()
| fields -> Format.fprintf !out "%a@." Json.pp (`Assoc fields));
Format.pp_print_flush !out ())
;;
end

let sprintf = Printf.sprintf

let () =
Printexc.register_printer (function
| Jsonrpc.Response.Error.E t ->
let json = Jsonrpc.Response.Error.yojson_of_t t in
Some ("jsonrpc response error " ^ Json.to_pretty_string (json :> Json.t))
| _ -> None)
| Jsonrpc.Response.Error.E t ->
let json = Jsonrpc.Response.Error.yojson_of_t t in
Some ("jsonrpc response error " ^ Json.to_pretty_string (json :> Json.t))
| _ -> None)
;;
Loading