@@ -18,11 +18,11 @@ module Lang = {
1818 }
1919
2020 let decode = (json ): t => {
21- open ! Json . Decode
22- switch string ( json ) {
23- | "re" => Reason
24- | "res" => Res
25- | other => throw (DecodeError (` Unknown language "${other}" ` ))
21+ open JSON
22+ switch json {
23+ | String ( "re" ) => Reason
24+ | String ( "res" ) => Res
25+ | other => throw (Failure (` Unknown language "${other-> stringify}". ${__LOC__} ` ))
2626 }
2727 }
2828}
@@ -91,14 +91,24 @@ module LocMsg = {
9191 }
9292
9393 let decode = (json ): t => {
94- open Json .Decode
95- {
96- fullMsg : json -> field ("fullMsg" , string , _ ),
97- shortMsg : json -> field ("shortMsg" , string , _ ),
98- row : json -> field ("row" , int , _ ),
99- column : json -> field ("column" , int , _ ),
100- endRow : json -> field ("endRow" , int , _ ),
101- endColumn : json -> field ("endColumn" , int , _ ),
94+ open JSON
95+ switch json {
96+ | Object (dict {
97+ "fullMsg" : String (fullMsg ),
98+ "shortMsg" : String (shortMsg ),
99+ "row" : Number (row ),
100+ "column" : Number (column ),
101+ "endRow" : Number (endRow ),
102+ "endColumn" : Number (endColumn ),
103+ }) => {
104+ fullMsg ,
105+ shortMsg ,
106+ row : row -> Float .toInt ,
107+ column : column -> Float .toInt ,
108+ endRow : endRow -> Float .toInt ,
109+ endColumn : endColumn -> Float .toInt ,
110+ }
111+ | _ => throw (Failure (` Failed to decode LocMsg. ${__LOC__}` ))
102112 }
103113 }
104114
@@ -143,12 +153,18 @@ module Warning = {
143153 | WarnErr ({warnNumber : int , details : LocMsg .t }) // Describes an erronous warning
144154
145155 let decode = (json ): t => {
146- open ! Json .Decode
147-
148- let warnNumber = field ("warnNumber" , int , json )
156+ open JSON
157+ let warnNumber = switch json {
158+ | Object (dict {"warnNumber" : Number (warnNumber )}) => warnNumber -> Float .toInt
159+ | _ => throw (Failure (` Failed to decode warn number. ${__LOC__}` ))
160+ }
149161 let details = LocMsg .decode (json )
150162
151- field ("isError" , bool , json ) ? WarnErr ({warnNumber , details }) : Warn ({warnNumber , details })
163+ switch json {
164+ | Object (dict {"isError" : Boolean (isError )}) =>
165+ isError ? WarnErr ({warnNumber , details }) : Warn ({warnNumber , details })
166+ | _ => throw (Failure (` Failed to decode warnings. ${__LOC__}` ))
167+ }
152168 }
153169
154170 // Useful for showing errors in a more compact format
@@ -178,11 +194,14 @@ module WarningFlag = {
178194 }
179195
180196 let decode = (json ): t => {
181- open Json .Decode
182- {
183- msg : field ("msg" , string , json ),
184- warn_flags : field ("warn_flags" , string , json ),
185- warn_error_flags : field ("warn_error_flags" , string , json ),
197+ open JSON
198+ switch json {
199+ | Object (dict {
200+ "msg" : String (msg ),
201+ "warn_flags" : String (warn_flags ),
202+ "warn_error_flags" : String (warn_error_flags ),
203+ }) => {msg , warn_flags , warn_error_flags }
204+ | _ => throw (Failure (` Failed to decode WarningFlag. ${__LOC__}` ))
186205 }
187206 }
188207}
@@ -206,27 +225,37 @@ module TypeHint = {
206225 | CoreType (data )
207226
208227 let decodePosition = json => {
209- open Json .Decode
210- {
211- line : field ("line" , int , json ),
212- col : field ("col" , int , json ),
228+ open JSON
229+ switch json {
230+ | Object (dict {"line" : Number (line ), "col" : Number (col )}) => {
231+ line : line -> Float .toInt ,
232+ col : col -> Float .toInt ,
233+ }
234+ | _ => throw (Failure (` Failed to decode position. ${__LOC__}` ))
213235 }
214236 }
215237
216238 let decode = (json ): t => {
217- open Json .Decode
218- let data = {
219- start : field ("start" , decodePosition , json ),
220- end : field ("end" , decodePosition , json ),
221- hint : field ("hint" , string , json ),
239+ open JSON
240+ let data = switch json {
241+ | Object (dict {"start" : startPosition , "end" : endPosition , "hint" : String (hint )}) => {
242+ start : decodePosition (startPosition ),
243+ end : decodePosition (endPosition ),
244+ hint ,
245+ }
246+ | _ => throw (Failure (` Failed to decode type hint position. ${__LOC__}` ))
222247 }
223248
224- switch field ("kind" , string , json ) {
225- | "expression" => Expression (data )
226- | "type_declaration" => TypeDeclaration (data )
227- | "binding" => Binding (data )
228- | "core_type" => CoreType (data )
229- | other => throw (DecodeError (` Unknown kind "${other}" type hint` ))
249+ switch json {
250+ | Object (dict {"kind" : String (kind )}) =>
251+ switch kind {
252+ | "expression" => Expression (data )
253+ | "type_declaration" => TypeDeclaration (data )
254+ | "binding" => Binding (data )
255+ | "core_type" => CoreType (data )
256+ | other => throw (Failure (` Unknown kind "${other}" type hint. ${__LOC__}` ))
257+ }
258+ | _ => throw (Failure (` Failed to decode type hint kind. ${__LOC__}` ))
230259 }
231260 }
232261}
@@ -242,12 +271,17 @@ module CompileSuccess = {
242271 }
243272
244273 let decode = (~time : float , json ): t => {
245- open Json .Decode
246- {
247- jsCode : field ("js_code" , string , json ),
248- warnings : field ("warnings" , array (Warning .decode , ... ), json ),
249- typeHints : withDefault ([], field ("type_hints" , array (TypeHint .decode , ... ), ... ), json ),
250- time ,
274+ open JSON
275+ switch json {
276+ | Object (dict {
277+ "js_code" : String (jsCode ),
278+ "warnings" : Array (warnings ),
279+ "type_hints" : Array (typeHints ),
280+ }) =>
281+ let warnings = warnings -> Array .map (Warning .decode )
282+ let typeHints = typeHints -> Array .map (TypeHint .decode )
283+ {jsCode , warnings , typeHints , time }
284+ | _ => throw (Failure (` Failed to decode CompileSuccess. ${__LOC__}` ))
251285 }
252286 }
253287}
@@ -260,11 +294,14 @@ module ConvertSuccess = {
260294 }
261295
262296 let decode = (json ): t => {
263- open Json .Decode
264- {
265- code : field ("code" , string , json ),
266- fromLang : field ("fromLang" , Lang .decode , json ),
267- toLang : field ("toLang" , Lang .decode , json ),
297+ open JSON
298+ switch json {
299+ | Object (dict {"code" : String (code ), "fromLang" : fromLang , "toLang" : toLang }) => {
300+ code ,
301+ fromLang : fromLang -> Lang .decode ,
302+ toLang : toLang -> Lang .decode ,
303+ }
304+ | _ => throw (Failure (` Failed to decode ConvertSuccess. ${__LOC__}` ))
268305 }
269306 }
270307}
@@ -278,28 +315,41 @@ module CompileFail = {
278315 | OtherErr (array <LocMsg .t >)
279316
280317 let decode = (json ): t => {
281- open ! Json .Decode
282-
283- switch field ("type" , string , json ) {
284- | "syntax_error" =>
285- let locMsgs = field ("errors" , array (LocMsg .decode , ... ), json )
286- // TODO: There seems to be a bug in the ReScript bundle that reports
287- // back multiple LocMsgs of the same value
288- locMsgs -> LocMsg .dedupe -> SyntaxErr
289- | "type_error" =>
290- let locMsgs = field ("errors" , array (LocMsg .decode , ... ), json )
291- TypecheckErr (locMsgs )
292- | "warning_error" =>
293- let warnings = field ("errors" , array (Warning .decode , ... ), json )
294- WarningErr (warnings )
295- | "other_error" =>
296- let locMsgs = field ("errors" , array (LocMsg .decode , ... ), json )
297- OtherErr (locMsgs )
298-
299- | "warning_flag_error" =>
300- let warningFlag = WarningFlag .decode (json )
301- WarningFlagErr (warningFlag )
302- | other => throw (DecodeError (` Unknown type "${other}" in CompileFail result` ))
318+ open JSON
319+ switch json {
320+ | String (type_ ) =>
321+ switch type_ {
322+ | "syntax_error" =>
323+ let locMsgs = switch json {
324+ | Object (dict {"erros" : Array (errors )}) => errors -> Array .map (LocMsg .decode )
325+ | _ => throw (Failure (` Failed to decode erros from syntax_error. ${__LOC__}` ))
326+ }
327+ // TODO: There seems to be a bug in the ReScript bundle that reports
328+ // back multiple LocMsgs of the same value
329+ locMsgs -> LocMsg .dedupe -> SyntaxErr
330+ | "type_error" =>
331+ let locMsgs = switch json {
332+ | Object (dict {"erros" : Array (errors )}) => errors -> Array .map (LocMsg .decode )
333+ | _ => throw (Failure (` Failed to decode erros from type_error. ${__LOC__}` ))
334+ }
335+ TypecheckErr (locMsgs )
336+ | "warning_error" =>
337+ let warnings = switch json {
338+ | Object (dict {"erros" : Array (warnings )}) => warnings -> Array .map (Warning .decode )
339+ | _ => throw (Failure (` Failed to decode errors from warning_error. ${__LOC__}` ))
340+ }
341+ WarningErr (warnings )
342+ | "other_error" =>
343+ let locMsgs = switch json {
344+ | Object (dict {"erros" : Array (errors )}) => errors -> Array .map (LocMsg .decode )
345+ | _ => throw (Failure (` Failed to decode errors from other_error. ${__LOC__}` ))
346+ }
347+ OtherErr (locMsgs )
348+
349+ | "warning_flag_error" => WarningFlagErr (WarningFlag .decode (json ))
350+ | other => throw (Failure (` Unknown type "${other}" in CompileFail result. ${__LOC__}` ))
351+ }
352+ | _ => throw (Failure (` Failed to decode CompileFail. ${__LOC__}` ))
303353 }
304354 }
305355}
@@ -313,14 +363,19 @@ module CompilationResult = {
313363
314364 // TODO: We might change this specific api completely before launching
315365 let decode = (~time : float , json : JSON .t ): t => {
316- open ! Json .Decode
317-
318- try switch field ("type" , string , json ) {
319- | "success" => Success (CompileSuccess .decode (~time , json ))
320- | "unexpected_error" => UnexpectedError (field ("msg" , string , json ))
321- | _ => Fail (CompileFail .decode (json ))
322- } catch {
323- | DecodeError (errMsg ) => Unknown (errMsg , json )
366+ open JSON
367+ switch json {
368+ | Object (dict {"type" : String (type_ )}) =>
369+ switch type_ {
370+ | "success" => Success (CompileSuccess .decode (~time , json ))
371+ | "unexpected_error" =>
372+ switch json {
373+ | Object (dict {"msg" : String (msg )}) => UnexpectedError (msg )
374+ | _ => throw (Failure (` Failed to decode msg from unexpected_error. ${__LOC__}` ))
375+ }
376+ | _ => Fail (CompileFail .decode (json ))
377+ }
378+ | _ => throw (Failure (` Failed to decode CompilationResult. ${__LOC__}` ))
324379 }
325380 }
326381}
@@ -333,16 +388,22 @@ module ConversionResult = {
333388 | Unknown (string , JSON .t )
334389
335390 let decode = (~fromLang : Lang .t , ~toLang : Lang .t , json ): t => {
336- open ! Json .Decode
337- try switch field ("type" , string , json ) {
338- | "success" => Success (ConvertSuccess .decode (json ))
339- | "unexpected_error" => UnexpectedError (field ("msg" , string , json ))
340- | "syntax_error" =>
341- let locMsgs = field ("errors" , array (LocMsg .decode , ... ), json )
342- Fail ({fromLang , toLang , details : locMsgs })
343- | other => Unknown (` Unknown conversion result type "${other}"` , json )
344- } catch {
345- | DecodeError (errMsg ) => Unknown (errMsg , json )
391+ open JSON
392+ switch json {
393+ | Object (dict {
394+ "type" : String (type_ ),
395+ "msg" : ?Some (String (msg )),
396+ "errors" : ?Some (Array (errors )),
397+ }) =>
398+ switch type_ {
399+ | "success" => Success (ConvertSuccess .decode (json ))
400+ | "unexpected_error" => msg -> UnexpectedError
401+ | "syntax_error" =>
402+ let locMsgs = errors -> Array .map (LocMsg .decode )
403+ Fail ({fromLang , toLang , details : locMsgs })
404+ | other => Unknown (` Unknown conversion result type "${other}"` , json )
405+ }
406+ | _ => throw (Failure (` Failed to decode ConversionResult. ${__LOC__}` ))
346407 }
347408 }
348409}
0 commit comments