Skip to content

Commit 8112f80

Browse files
author
Guillaume Petiot
authored
Functorize ocamlformat-rpc-lib over the IO (ocaml-ppx#1975)
1 parent c1eaa46 commit 8112f80

File tree

11 files changed

+394
-235
lines changed

11 files changed

+394
-235
lines changed

CHANGES.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@
1616

1717
#### New features
1818

19+
#### RPC
20+
21+
+ ocamlformat-rpc-lib is now functorized over the IO (#1975, @gpetiot).
22+
Now handles `Csexp.t` types instead of `Sexplib0.Sexp.t`.
23+
1924
### 0.20.1 (2021-12-13)
2025

2126
#### New features

bin/ocamlformat-rpc/main.ml

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,28 @@ Caml.at_exit (Format.pp_print_flush Format.err_formatter);;
1818

1919
Caml.at_exit (Format_.pp_print_flush Format_.err_formatter)
2020

21+
module IO = struct
22+
type 'a t = 'a
23+
24+
type ic = In_channel.t
25+
26+
type oc = Out_channel.t
27+
28+
let ( >>= ) x f = f x
29+
30+
let return x = x
31+
32+
let read ic =
33+
match Csexp.input ic with
34+
| Ok x -> return (Some x)
35+
| Error _ -> return None
36+
37+
let write oc lx =
38+
List.iter lx ~f:(Csexp.to_channel oc) ;
39+
Out_channel.flush oc ;
40+
return ()
41+
end
42+
2143
module V = struct
2244
type t = V1
2345

@@ -30,6 +52,8 @@ end
3052

3153
type state = Waiting_for_version | Version_defined of (V.t * Conf.t)
3254

55+
include Make (IO)
56+
3357
let format fg conf source =
3458
let input_name = "<rpc input>" in
3559
let opts = Conf.{debug= false; margin_check= false} in
@@ -145,7 +169,9 @@ let info =
145169
"Once the client and the server agree on a common version, the \
146170
requests you can send may differ from one version to another."
147171
; `P "On version $(b,v1), the supported RPC commands are:"
148-
; `P "- $(b,Halt) to close the connection to the RPC"
172+
; `P
173+
"- $(b,Halt) to end the communication with the RPC server. The \
174+
caller must close the input and output channels."
149175
; `P
150176
"- $(b,Config) $(i,CSEXP): submits a list of (key, value) pairs (as \
151177
a canonical s-expression) to update OCamlFormat's configuration \

dune-project

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,5 +161,4 @@
161161
(and
162162
(>= 4.08)
163163
(< 4.15)))
164-
csexp
165-
sexplib0))
164+
csexp))

lib-rpc/IO.ml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
(**************************************************************************)
2+
(* *)
3+
(* OCamlFormat *)
4+
(* *)
5+
(* Copyright (c) Facebook, Inc. and its affiliates. *)
6+
(* *)
7+
(* This source code is licensed under the MIT license found in *)
8+
(* the LICENSE file in the root directory of this source tree. *)
9+
(* *)
10+
(**************************************************************************)
11+
12+
(** The [IO] module defines the blocking interface for reading and writing to
13+
Cohttp streams *)
14+
15+
module type S = sig
16+
(** ['a t] represents a blocking monad state *)
17+
type +'a t
18+
19+
val ( >>= ) : 'a t -> ('a -> 'b t) -> 'b t
20+
(** [a >>= b] will pass the result of [a] to the [b] function. This is a
21+
monadic [bind]. *)
22+
23+
val return : 'a -> 'a t
24+
(** [return a] will construct a constant IO value. *)
25+
26+
(** [ic] represents an input channel *)
27+
type ic
28+
29+
(** [oc] represents an output channel *)
30+
type oc
31+
32+
val read : ic -> Csexp.t option t
33+
34+
val write : oc -> Csexp.t list -> unit t
35+
end

lib-rpc/dune

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
(library
22
(name ocamlformat_rpc_lib)
33
(public_name ocamlformat-rpc-lib)
4-
(libraries csexp sexplib0))
4+
(libraries csexp))

0 commit comments

Comments
 (0)