Skip to content

Commit 6d0f912

Browse files
committed
flatten some decode pattern matchings
1 parent 7c6f981 commit 6d0f912

File tree

1 file changed

+26
-43
lines changed

1 file changed

+26
-43
lines changed

src/bindings/RescriptCompilerApi.res

Lines changed: 26 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -317,39 +317,29 @@ module CompileFail = {
317317
let decode = (json): t => {
318318
open JSON
319319
switch json {
320-
| Object(dict{"type": String(type_)}) =>
321-
switch type_ {
322-
| "syntax_error" =>
323-
let locMsgs = switch json {
324-
| Object(dict{"errors": Array(errors)}) => errors->Array.map(LocMsg.decode)
325-
| _ => throw(Failure(`Failed to decode errors 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{"errors": Array(errors)}) => errors->Array.map(LocMsg.decode)
333-
| _ => throw(Failure(`Failed to decode errors from type_error. ${__LOC__}`))
334-
}
335-
TypecheckErr(locMsgs)
336-
| "warning_error" =>
337-
let warnings = switch json {
338-
| Object(dict{"errors": 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{"errors": 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__}`))
320+
| Object(dict{"type": String("syntax_error"), "errors": Array(errors)}) =>
321+
let locMsgs = errors->Array.map(LocMsg.decode)
322+
// TODO: There seems to be a bug in the ReScript bundle that reports
323+
// back multiple LocMsgs of the same value
324+
locMsgs->LocMsg.dedupe->SyntaxErr
325+
| Object(dict{"type": String("type_error"), "errors": Array(errors)}) =>
326+
let locMsgs = errors->Array.map(LocMsg.decode)
327+
TypecheckErr(locMsgs)
328+
| Object(dict{"type": String("warning_error"), "errors": Array(warnings)}) =>
329+
let warnings = warnings->Array.map(Warning.decode)
330+
WarningErr(warnings)
331+
| Object(dict{"type": String("other_error"), "errors": Array(errors)}) =>
332+
let locMsgs = errors->Array.map(LocMsg.decode)
333+
OtherErr(locMsgs)
334+
| Object(dict{"type": String("warning_flag_error")}) => WarningFlagErr(WarningFlag.decode(json))
335+
| Object(dict{"type": String(other)}) =>
336+
throw(Failure(`Unknown type "${other}" in CompileFail result. ${__LOC__}`))
337+
| _ =>
338+
throw(
339+
Failure(
340+
`Failed to decode CompileFail. ${__LOC__}. Could not decode \`${json->JSON.stringify}\``,
341+
),
342+
)
353343
}
354344
}
355345
}
@@ -365,16 +355,9 @@ module CompilationResult = {
365355
let decode = (~time: float, json: JSON.t): t => {
366356
open JSON
367357
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-
}
358+
| Object(dict{"type": String("success")}) => Success(CompileSuccess.decode(~time, json))
359+
| Object(dict{"type": String("unexpected_error"), "msg": String(msg)}) => UnexpectedError(msg)
360+
| Object(dict{"type": String(_)}) => Fail(CompileFail.decode(json))
378361
| _ => throw(Failure(`Failed to decode CompilationResult. ${__LOC__}`))
379362
}
380363
}

0 commit comments

Comments
 (0)