-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathTools_Docgen.res
238 lines (217 loc) · 5.71 KB
/
Tools_Docgen.res
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
type field = {
name: string,
docstrings: array<string>,
signature: string,
optional: bool,
deprecated?: string,
}
type constructor = {
name: string,
docstrings: array<string>,
signature: string,
deprecated?: string,
}
@tag("kind")
type detail =
| @as("record") Record({items: array<field>})
| @as("variant") Variant({items: array<constructor>})
@tag("kind")
type rec item =
| @as("value")
Value({
id: string,
docstrings: array<string>,
signature: string,
name: string,
deprecated?: string,
})
| @as("type")
Type({
id: string,
docstrings: array<string>,
signature: string,
name: string,
deprecated?: string,
/** Additional documentation for constructors and record fields, if available. */
detail?: detail,
})
| @as("module")
Module({
id: string,
docstrings: array<string>,
deprecated?: string,
name: string,
items: array<item>,
})
| @as("moduleAlias")
ModuleAlias({
id: string,
docstrings: array<string>,
name: string,
items: array<item>,
})
let decodeDocstrings = item => {
open Js.Json
switch item->Js.Dict.get("docstrings") {
| Some(Array(arr)) =>
arr->Js.Array2.map(s =>
switch s {
| String(s) => s
| _ => assert(false)
}
)
| _ => []
}
}
let decodeStringByField = (item, field) => {
open Js.Json
switch item->Js.Dict.get(field) {
| Some(String(s)) => s
| _ => assert(false)
}
}
let decodeDepreacted = item => {
open Js.Json
switch item->Js.Dict.get("deprecated") {
| Some(String(s)) => Some(s)
| _ => None
}
}
let decodeRecordFields = fields => {
open Js.Json
let items = fields->Js.Array2.map(field => {
switch field {
| Object(doc) => {
let name = doc->decodeStringByField("name")
let docstrings = doc->decodeDocstrings
let signature = doc->decodeStringByField("signature")
let deprecated = doc->decodeDepreacted
let optional = switch Js.Dict.get(doc, "optional") {
| Some(Boolean(bool)) => bool
| _ => assert(false)
}
{name, docstrings, signature, optional, ?deprecated}
}
| _ => assert(false)
}
})
Record({items: items})
}
let decodeConstructorFields = fields => {
open Js.Json
let items = fields->Js.Array2.map(field => {
switch field {
| Object(doc) => {
let name = doc->decodeStringByField("name")
let docstrings = doc->decodeDocstrings
let signature = doc->decodeStringByField("signature")
let deprecated = doc->decodeDepreacted
{name, docstrings, signature, ?deprecated}
}
| _ => assert(false)
}
})
Variant({items: items})
}
let decodeDetail = detail => {
open Js.Json
switch detail {
| Object(detail) =>
switch (detail->Js.Dict.get("kind"), detail->Js.Dict.get("items")) {
| (Some(String(kind)), Some(Array(items))) =>
switch kind {
| "record" => decodeRecordFields(items)
| "variant" => decodeConstructorFields(items)
| _ => assert(false)
}
| _ => assert(false)
}
| _ => assert(false)
}
}
let rec decodeValue = item => {
let id = item->decodeStringByField("id")
let signature = item->decodeStringByField("signature")
let name = item->decodeStringByField("name")
let deprecated = item->decodeDepreacted
let docstrings = item->decodeDocstrings
Value({id, docstrings, signature, name, ?deprecated})
}
and decodeType = item => {
let id = item->decodeStringByField("id")
let signature = item->decodeStringByField("signature")
let name = item->decodeStringByField("name")
let deprecated = item->decodeDepreacted
let docstrings = item->decodeDocstrings
let detail = switch item->Js_dict.get("detail") {
| Some(field) => decodeDetail(field)->Some
| None => None
}
Type({id, docstrings, signature, name, ?deprecated, ?detail})
}
and decodeModuleAlias = item => {
open Js.Json
let id = item->decodeStringByField("id")
let name = item->decodeStringByField("name")
let docstrings = item->decodeDocstrings
let items = switch Js.Dict.get(item, "items") {
| Some(Array(items)) => items->Js.Array2.map(item => decodeItem(item))
| _ => assert(false)
}
ModuleAlias({id, items, name, docstrings})
}
and decodeModule = item => {
open Js.Json
let id = item->decodeStringByField("id")
let name = item->decodeStringByField("name")
let deprecated = item->decodeDepreacted
let docstrings = item->decodeDocstrings
let items = switch Js.Dict.get(item, "items") {
| Some(Array(items)) => items->Js.Array2.map(item => decodeItem(item))
| _ => assert(false)
}
Module({id, name, docstrings, ?deprecated, items})
}
and decodeItem = item => {
open Js.Json
switch item {
| Object(value) =>
switch Js.Dict.get(value, "kind") {
| Some(String(kind)) =>
switch kind {
| "type" => decodeType(value)
| "value" => decodeValue(value)
| "module" => decodeModule(value)
| "moduleAlias" => decodeModuleAlias(value)
| _ => assert(false)
}
| _ => assert(false)
}
| _ => assert(false)
}
}
type doc = {
name: string,
deprecated: option<string>,
docstrings: array<string>,
items: array<item>,
}
/**
`decodeFromJson(json)` parse JSON generated from `restool doc` command
*/
let decodeFromJson = json => {
open Js.Json
switch json {
| Object(mod) => {
let name = mod->decodeStringByField("name")
let deprecated = mod->decodeDepreacted
let docstrings = mod->decodeDocstrings
let items = switch Js.Dict.get(mod, "items") {
| Some(Array(items)) => items->Js.Array2.map(item => decodeItem(item))
| _ => assert(false)
}
{name, deprecated, docstrings, items}
}
| _ => assert(false)
}
}