1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
module IO = struct
type _ io =
| Empty : unit io
| Json : 'a Json_encoding.encoding -> 'a io
| Raw : Mime.t list -> string io
let to_string :
type a. a io -> a -> string =
fun io a -> match io with
| Empty -> ""
| Raw _ -> a
| Json enc -> EzEncoding.construct enc a
let from_string :
type a. a io -> (a -> 'b) -> string -> ('b, [> EzEncoding.destruct_error ]) result =
fun io f s -> match io with
| Empty -> Ok (f ())
| Raw _ -> Ok (f s)
| Json enc -> match EzEncoding.destruct_res enc s with
| Error e -> Error e
| Ok a -> Ok (f a)
let res_from_string :
type a. a io -> (a Json_encoding.encoding -> (a, 'e) result Json_encoding.encoding) ->
((a, 'e) result -> 'b) -> string ->
('b, [> EzEncoding.destruct_error ]) result =
fun io fenc f s -> match io with
| Empty -> Ok (f (Ok ()))
| Raw _ -> Ok (f (Ok s))
| Json enc -> match EzEncoding.destruct_res (fenc enc) s with
| Error e -> Error e
| Ok a -> Ok (f a)
end
type ('args, 'input, 'output, 'error, 'security) t = {
path : (Req.t, 'args) Path.t;
input : 'input IO.io;
output : 'output IO.io;
errors : 'error Err.case list;
meth : Meth.t;
params : Param.t list;
security: ([< Security.scheme ] as 'security) list;
}
let make ?(meth : Meth.t =`GET) ?(params=[]) ?(security=[]) ?(errors=[]) ~input ~output path =
{ path ; input ; output; errors; meth; params; security }
let input s = s.input
let output s = s.output
let errors s = s.errors
let errors_encoding s =
Json_encoding.union @@
List.map (function Err.Case { encoding; select; deselect; _} ->
Json_encoding.case encoding select deselect
) (s.errors @ [Err.catch_all_error_case ()])
let meth s = s.meth
let path s = s.path
let security s = s.security
let params s = s.params
let error s ~code = Err.get ~code s.errors