blob: 00e003a4f54032b50737532dd3c0cc43e421a515 (
plain)
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
|
module Decoder : sig
type 'a decoder
val decode : 'a decoder -> Yojson.Safe.t -> 'a option
val return : 'a -> 'a decoder
val fail : 'a decoder
val bind : 'a decoder -> ('a -> 'b decoder) -> 'b decoder
val map : ('a -> 'b) -> 'a decoder -> 'b decoder
val string : string decoder
val literal : string -> string decoder
val int : int decoder
val field : string -> 'a decoder -> 'a decoder
val list : 'a decoder -> 'a list decoder
val one_of : 'a decoder list -> 'a decoder
module Syntax : sig
val ( *> ) : 'a decoder -> 'b decoder -> 'b decoder
val ( <* ) : 'a decoder -> 'b decoder -> 'a decoder
val ( >>= ) : 'a decoder -> ('a -> 'b decoder) -> 'b decoder
val ( >>| ) : 'a decoder -> ('a -> 'b) -> 'b decoder
val ( <*> ) : ('a -> 'b) decoder -> 'a decoder -> 'b decoder
val ( <$> ) : ('a -> 'b) -> 'a decoder -> 'b decoder
val ( <: ) : string -> 'a decoder -> 'a decoder
val ( <|> ) : 'a decoder -> 'a decoder -> 'a decoder
end
end = struct
type 'a decoder = Yojson.Safe.t -> 'a option
let decode decoder json = decoder json
let return v _ = Some v
let fail _ = None
let map f decoder json =
match decoder json with
| Some d -> Some (f d)
| None -> None
;;
let bind decoder f json =
match decoder json with
| Some x -> f x `Null
| None -> None
;;
let rec one_of decoders json =
match decoders with
| [] -> None
| decoder :: tl ->
(match decoder json with
| Some _ as x -> x
| None -> one_of tl json)
;;
let string = function
| `String str -> Some str
| _ -> None
;;
let literal str = function
| `String s when String.equal s str -> Some s
| _ -> None
;;
let int = function
| `Int int -> Some int
| _ -> None
;;
let field key decoder json =
try decoder @@ Yojson.Safe.Util.member key json with
| _ -> None
;;
let list decoder json =
let rec helper acc = function
| [] -> Some []
| hd :: tl ->
(match decoder hd with
| Some _ as x -> helper (x :: acc) tl
| None -> None)
in
match json with
| `List list -> Option.map List.rev (helper [] list)
| _ -> None
;;
module Syntax = struct
let ( *> ) decoder_a decoder_b yojson =
match decoder_a yojson with
| Some _ -> decoder_b yojson
| None -> None
;;
let ( <* ) decoder_a decoder_b yojson =
match decoder_a yojson with
| Some value -> Option.map (fun _ -> value) (decoder_b yojson)
| None -> None
;;
let ( >>= ) = bind
let ( >>| ) decoder f yojson = map f decoder yojson
let ( <*> ) decoder_a decoder_b input =
match decoder_a input with
| Some f ->
(match decoder_b input with
| Some x -> Some (f x)
| None -> None)
| None -> None
;;
let ( <$> ) f decoder = decoder >>| f
let ( <: ) = field
let ( <|> ) decoder_a decoder_b input =
match decoder_a input with
| Some _ as value -> value
| None -> decoder_b input
;;
end
end
|