Skip to content

Commit eb74442

Browse files
committed
front: Dump compare output as json
1 parent cb7efa4 commit eb74442

File tree

4 files changed

+84
-13
lines changed

4 files changed

+84
-13
lines changed

src/ast/printer.rs

+16-4
Original file line numberDiff line numberDiff line change
@@ -217,11 +217,8 @@ impl<'d> ToDisplayTree<'d> for TypingResult<'_> {
217217
.to_display_tree(a)
218218
.sep_then(a, ", ", format!("\"{s:?}\""))
219219
.surrounded(a, "BorrowError(", ")"),
220-
TypingResult::TypeError(TypeError::External(e)) => format!("{e}")
221-
.to_display_tree(a)
222-
.surrounded(a, "TypeError(\"", "\")"),
223220
TypingResult::TypeError(e) => {
224-
format!("{e:?}")
221+
format!("{e}")
225222
.to_display_tree(a)
226223
.surrounded(a, "TypeError(\"", "\")")
227224
}
@@ -460,6 +457,21 @@ impl Display for TypingResult<'_> {
460457
}
461458
}
462459

460+
impl Display for BorrowCheckError {
461+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
462+
write!(f, "{self:?}")
463+
}
464+
}
465+
466+
impl Display for TypeError {
467+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
468+
match self {
469+
TypeError::External(e) => write!(f, "{e}"),
470+
_ => write!(f, "{self:?}"),
471+
}
472+
}
473+
}
474+
463475
impl Debug for Mutability {
464476
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
465477
write!(f, "{self}")

src/wasm.rs

+58-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use bincode::{Decode, Encode};
33
use gloo_utils::format::JsValueSerdeExt;
44
use serde::Serialize;
55
use std::cmp::Ordering;
6+
use std::collections::BTreeMap;
67
use std::fmt::Write;
78
use wasm_bindgen::prelude::*;
89

@@ -360,12 +361,58 @@ pub fn compare_rulesets_js(
360361
req: String,
361362
left: String,
362363
right: String,
364+
structured: StructuredCompareOutput,
365+
}
366+
#[derive(Debug, Clone, Serialize)]
367+
pub struct StructuredCompareOutput {
368+
query: StructuredCompareOutputQuery,
369+
left: StructuredCompareOutputResult,
370+
right: StructuredCompareOutputResult,
371+
}
372+
#[derive(Debug, Clone, Serialize)]
373+
pub struct StructuredCompareOutputQuery {
374+
pattern: String,
375+
#[serde(rename = "type")]
376+
type_: String,
377+
}
378+
#[derive(Debug, Clone, Serialize)]
379+
#[serde(tag = "kind")]
380+
pub enum StructuredCompareOutputResult {
381+
Success {
382+
bindings: BTreeMap<String, String>,
383+
},
384+
BorrowError {
385+
name: String,
386+
bindings: BTreeMap<String, String>,
387+
},
388+
TypeError {
389+
name: String,
390+
},
363391
}
364392

365393
assert!(direction.abs() <= 1);
366394
let direction: Ordering = unsafe { std::mem::transmute(direction) };
367395

368396
let a = &Arenas::default();
397+
let bindings_to_strings = |bindings: &BindingAssignments| {
398+
bindings
399+
.assignments
400+
.iter()
401+
.map(|(var, ty)| (var.to_string(), ty.to_string()))
402+
.collect()
403+
};
404+
let ty_res_to_structured = |res: &TypingResult| match res {
405+
TypingResult::Success(bindings) => StructuredCompareOutputResult::Success {
406+
bindings: bindings_to_strings(bindings),
407+
},
408+
TypingResult::BorrowError(bindings, e) => StructuredCompareOutputResult::BorrowError {
409+
name: e.to_string(),
410+
bindings: bindings_to_strings(bindings),
411+
},
412+
TypingResult::TypeError(e) => StructuredCompareOutputResult::TypeError {
413+
name: e.to_string(),
414+
},
415+
};
369416
compare_rulesets(
370417
a,
371418
pat_depth,
@@ -376,11 +423,21 @@ pub fn compare_rulesets_js(
376423
)
377424
.into_iter()
378425
.map(|(req, left, right)| {
426+
let structured = StructuredCompareOutput {
427+
query: StructuredCompareOutputQuery {
428+
pattern: req.pat.to_string(),
429+
type_: req.ty.to_string(),
430+
},
431+
left: ty_res_to_structured(&left),
432+
right: ty_res_to_structured(&right),
433+
};
379434
let (left, right) = left.display_diffed(&right);
435+
let req = req.to_string();
380436
CompareOutput {
381-
req: req.to_string(),
437+
req,
382438
left,
383439
right,
440+
structured,
384441
}
385442
})
386443
.map(|out| JsValue::from_serde(&out).unwrap())

web/src/components/Solver.jsx

+10-1
Original file line numberDiff line numberDiff line change
@@ -415,10 +415,19 @@ export function CompareDisplay({
415415
const worker = new Worker(new URL("./worker.ts", import.meta.url), {
416416
type: "module",
417417
});
418+
const truncateAt = 300;
418419
worker.onmessage = function (event) {
419420
switch (event.data.type) {
420421
case "compare":
421-
setOutput(event.data.output);
422+
var output = event.data.output;
423+
const structured_compare = output.map(x => x.structured);
424+
console.log(JSON.stringify(structured_compare));
425+
if (output.length > truncateAt) {
426+
const diff = output.length - truncateAt;
427+
output.length = truncateAt;
428+
output.push({ req: `and ${diff} more...` })
429+
}
430+
setOutput(output);
422431
break;
423432
case "loaded":
424433
if (showCompare) {

web/src/components/worker.ts

-7
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ import init, { RuleSetJs, compare_rulesets_js } from "../../typing_rust_patterns
33
(async () => {
44
await init({});
55

6-
const truncateAt = 300;
7-
86
addEventListener("message", async (event) => {
97
const data = event.data;
108
switch (data.type) {
@@ -16,11 +14,6 @@ import init, { RuleSetJs, compare_rulesets_js } from "../../typing_rust_patterns
1614
data.tyDepth,
1715
data.compareDirection
1816
);
19-
if (output.length > truncateAt) {
20-
const diff = output.length - truncateAt;
21-
output.length = truncateAt;
22-
output.push({ req: `and ${diff} more...` })
23-
}
2417
postMessage({ type: "compare", output });
2518
break;
2619
}

0 commit comments

Comments
 (0)