Skip to content

Commit

Permalink
Use Rresult rather than Osc_result
Browse files Browse the repository at this point in the history
Signed-off-by: John Else <john.else@citrix.com>
  • Loading branch information
johnelse committed Nov 13, 2015
1 parent 724b16b commit bd98b47
Show file tree
Hide file tree
Showing 12 changed files with 40 additions and 56 deletions.
4 changes: 2 additions & 2 deletions _oasis
Expand Up @@ -18,9 +18,9 @@ Flag unix

Library osc
Path: lib
BuildDepends: bytes, ocplib-endian
BuildDepends: bytes, ocplib-endian, rresult
Findlibname: osc
Modules: Osc, Osc_result, Osc_string, Osc_transport
Modules: Osc, Osc_string, Osc_transport

Library osc_lwt
Build$: flag(lwt)
Expand Down
22 changes: 0 additions & 22 deletions lib/osc_result.ml

This file was deleted.

20 changes: 10 additions & 10 deletions lib/osc_string.ml
@@ -1,4 +1,4 @@
open Osc_result
open Rresult

module Input = struct
type t = {
Expand Down Expand Up @@ -59,15 +59,15 @@ module Decode = struct
result

let argument input = function
| 'f' -> return (Osc.Float32 (float32 input))
| 'i' -> return (Osc.Int32 (int32 input))
| 's' -> return (Osc.String (string input))
| 'b' -> return (Osc.Blob (blob input))
| typetag -> fail (`Unsupported_typetag typetag)
| 'f' -> Ok (Osc.Float32 (float32 input))
| 'i' -> Ok (Osc.Int32 (int32 input))
| 's' -> Ok (Osc.String (string input))
| 'b' -> Ok (Osc.Blob (blob input))
| typetag -> Error (`Unsupported_typetag typetag)

let arguments input =
if current_char input <> ','
then fail `Missing_typetag_string
then Error `Missing_typetag_string
else begin
(* Decode the typetag string. *)
let typetag_string = string input in
Expand All @@ -78,12 +78,12 @@ module Decode = struct
* string. *)
let rec decode typetag_position acc =
if typetag_position > typetag_count
then return acc
then (Ok acc)
else
argument input typetag_string.[typetag_position]
>>= (fun arg -> decode (typetag_position + 1) (arg :: acc))
in
decode 1 [] >|= List.rev
decode 1 [] >>| List.rev
end

let timetag input =
Expand All @@ -98,7 +98,7 @@ module Decode = struct
| "#bundle" -> raise Not_implemented
| address ->
arguments input >>=
(fun args -> return Osc.(Message {address = address; arguments = args}))
(fun args -> Ok (Osc.(Message {address = address; arguments = args})))
end

module Encode = struct
Expand Down
2 changes: 1 addition & 1 deletion lib/osc_string.mli
Expand Up @@ -8,5 +8,5 @@ val to_packet :
(Osc.packet, [
| `Missing_typetag_string
| `Unsupported_typetag of char
]) Osc_result.t
]) Rresult.result
(** Attempt to deserialise a string into an OSC packet. *)
6 changes: 4 additions & 2 deletions lib/osc_transport.ml
@@ -1,3 +1,5 @@
open Rresult

module type TRANSPORT = sig
module Io : sig
type 'a t
Expand Down Expand Up @@ -47,8 +49,8 @@ module Make(T : TRANSPORT) = struct

let recv server =
T.Server.recv_string server
>|= (fun (data, addr) -> Osc_result.map
>|= (fun (data, addr) ->
(Osc_string.to_packet data)
(fun packet -> (packet, addr)))
>>| (fun packet -> (packet, addr)))
end
end
2 changes: 1 addition & 1 deletion lib/osc_transport.mli
Expand Up @@ -93,7 +93,7 @@ module Make : functor (T : TRANSPORT) -> sig
((Osc.packet * T.sockaddr, [
| `Missing_typetag_string
| `Unsupported_typetag of char
]) Osc_result.t) T.Io.t
]) Rresult.result) T.Io.t
(** Retrieve a packet sent to the server, as well as the sending client's
address. *)
end
Expand Down
2 changes: 1 addition & 1 deletion lwt/osc_lwt.mli
Expand Up @@ -21,6 +21,6 @@ module Udp : sig
((Osc.packet * Lwt_unix.sockaddr, [
| `Missing_typetag_string
| `Unsupported_typetag of char
]) Osc_result.t) Lwt.t
]) Rresult.result) Lwt.t
end
end
7 changes: 4 additions & 3 deletions test/test_interop_sclang.ml
@@ -1,4 +1,5 @@
open OUnit
open Rresult

type test_config = {
ml_port: int;
Expand Down Expand Up @@ -45,11 +46,11 @@ let ping_sclang config packet =
let result = Server.recv server in
Printf.printf "ocaml: packet received\n%!";
match result with
| `Ok (received_packet, _) ->
| Ok (received_packet, _) ->
Test_common.assert_packets_equal sent_packet received_packet
| `Error `Missing_typetag_string ->
| Error `Missing_typetag_string ->
failwith "Missing typetag string"
| `Error (`Unsupported_typetag tag) ->
| Error (`Unsupported_typetag tag) ->
failwith (Printf.sprintf "Unsupported typetag: %c" tag))
(fun (child_pid, client, server) ->
Printf.printf "ocaml: killing sclang\n%!";
Expand Down
7 changes: 4 additions & 3 deletions test/test_lwt.ml
@@ -1,4 +1,5 @@
open OUnit
open Rresult

let test_udp_send_recv packet =
let open Lwt in
Expand All @@ -22,14 +23,14 @@ let test_udp_send_recv packet =
Client.send client addr packet
>>= (fun () -> Lwt_mvar.take mvar
>>= (function
| `Ok (received_packet, _) ->
| Ok (received_packet, _) ->
Test_common.assert_packets_equal
packet
received_packet;
return ()
| `Error `Missing_typetag_string ->
| Error `Missing_typetag_string ->
Lwt.fail (Failure "Missing typetag string")
| `Error (`Unsupported_typetag tag) ->
| Error (`Unsupported_typetag tag) ->
Lwt.fail (Failure (Printf.sprintf "Unsupported typetag: %c" tag))))))
(fun (client, server) ->
Lwt_main.run
Expand Down
15 changes: 8 additions & 7 deletions test/test_string.ml
@@ -1,17 +1,18 @@
open OUnit
open Rresult

(* Serialise a packet to a string; read it back from the string; check that the
* resulting packet equals the one we started with. *)
let test_message_encode_decode packet =
let data = Osc_string.of_packet packet in
match Osc_string.to_packet data with
| `Ok received_packet ->
| Ok received_packet ->
Test_common.assert_packets_equal
packet
received_packet
| `Error `Missing_typetag_string ->
| Error `Missing_typetag_string ->
failwith "Missing typetag string"
| `Error (`Unsupported_typetag tag) ->
| Error (`Unsupported_typetag tag) ->
failwith (Printf.sprintf "Unsupported typetag: %c" tag)

let test_message_encode_decode_suite =
Expand All @@ -28,23 +29,23 @@ let test_data =
(* A packet which we expect to decode successfully. *)
"message_ok",
"/foo/bar\000\000\000\000,is\000\000\000\000{hi\000\000",
`Ok (Message {
Ok (Message {
address = "/foo/bar";
arguments = [Int32 123l; String "hi"];
});
(* A packet which is missing a typetag string. *)
"message_missing_typetag_string",
"/foo/bar\000\000\000\000\000\000\000{hi\000\000",
`Error `Missing_typetag_string;
Error `Missing_typetag_string;
(* A packet which contains an unsupported typetag. *)
"message_unsupported_typetag",
"/foo/bar\000\000\000\000,iz\000\000\000\000{hi\000\000",
`Error (`Unsupported_typetag 'z');
Error (`Unsupported_typetag 'z');
]

let test_message_decode data expected_result =
match (Osc_string.to_packet data, expected_result) with
| `Ok decoded_packet, `Ok expected_packet ->
| Ok decoded_packet, Ok expected_packet ->
Test_common.assert_packets_equal
decoded_packet expected_packet
| result, expected_result ->
Expand Down
7 changes: 4 additions & 3 deletions test/test_unix.ml
@@ -1,4 +1,5 @@
open OUnit
open Rresult

(* Start a UDP server listening on localhost; send a packet to localhost and
* check that the server receives the same packet. *)
Expand All @@ -22,13 +23,13 @@ let test_udp_send_recv packet =
let (_: Thread.t) = Thread.create server_receive_thread channel in
Client.send client addr packet;
match Event.sync (Event.receive channel) with
| `Ok (received_packet, _) ->
| Ok (received_packet, _) ->
Test_common.assert_packets_equal
packet
received_packet
| `Error `Missing_typetag_string ->
| Error `Missing_typetag_string ->
failwith "Missing typetag string"
| `Error (`Unsupported_typetag tag) ->
| Error (`Unsupported_typetag tag) ->
failwith (Printf.sprintf "Unsupported typetag: %c" tag))
(fun (client, server) ->
Client.destroy client;
Expand Down
2 changes: 1 addition & 1 deletion unix/osc_unix.mli
Expand Up @@ -21,6 +21,6 @@ module Udp : sig
(Osc.packet * Unix.sockaddr, [
| `Missing_typetag_string
| `Unsupported_typetag of char
]) Osc_result.t
]) Rresult.result
end
end

0 comments on commit bd98b47

Please sign in to comment.