diff --git a/src/minutils/RPC.ml b/src/minutils/RPC.ml index 95a1b861e..ea58839e2 100644 --- a/src/minutils/RPC.ml +++ b/src/minutils/RPC.ml @@ -98,7 +98,15 @@ module Description = struct end -module Answer = RestoDirectory.Answer +module Answer = struct + + include RestoDirectory.Answer + + let answer ?(code = 200) json = { code; body = Single json } + let return ?code json = Lwt.return (answer ?code json) + +end + type step = | Static of string diff --git a/src/minutils/RPC.mli b/src/minutils/RPC.mli index d5a2e57e0..2a7344311 100644 --- a/src/minutils/RPC.mli +++ b/src/minutils/RPC.mli @@ -184,7 +184,8 @@ module Answer : sig } val ok: 'a -> 'a answer - val return: 'a -> 'a answer Lwt.t + val answer: ?code:int -> 'a -> 'a answer + val return: ?code:int -> 'a -> 'a answer Lwt.t val return_stream: 'a stream -> 'a answer Lwt.t end diff --git a/src/minutils/data_encoding.ml b/src/minutils/data_encoding.ml index 1507196b9..17cb3f981 100644 --- a/src/minutils/data_encoding.ml +++ b/src/minutils/data_encoding.ml @@ -698,6 +698,17 @@ module Encoding = struct `Variable in make @@ Mu (kind, name, self) + let result ok_enc error_enc = + union + ~tag_size:`Uint8 + [ case ~tag:1 ok_enc + (function Ok x -> Some x | Error _ -> None) + (fun x -> Ok x) ; + case ~tag:0 error_enc + (function Ok _ -> None | Error x -> Some x) + (fun x -> Error x) ; + ] + let assoc enc = let json = Json_encoding.assoc (Json.get_json enc) in let binary = list (tup2 string enc) in diff --git a/src/minutils/data_encoding.mli b/src/minutils/data_encoding.mli index 98adf1588..5066d3be4 100644 --- a/src/minutils/data_encoding.mli +++ b/src/minutils/data_encoding.mli @@ -47,6 +47,7 @@ val string : string encoding val bytes : MBytes.t encoding val float : float encoding val option : 'a encoding -> 'a option encoding +val result : 'a encoding -> 'b encoding -> ('a, 'b) result encoding val string_enum : (string * 'a) list -> 'a encoding module Fixed : sig diff --git a/src/proto/environment/RPC.mli b/src/proto/environment/RPC.mli index bbefb2eee..8c1ee6346 100644 --- a/src/proto/environment/RPC.mli +++ b/src/proto/environment/RPC.mli @@ -98,7 +98,8 @@ module Answer : sig } val ok: 'a -> 'a answer - val return: 'a -> 'a answer Lwt.t + val answer: ?code:int -> 'a -> 'a answer + val return: ?code:int -> 'a -> 'a answer Lwt.t end