Skip to content

Commit

Permalink
front: Dump compare output as json
Browse files Browse the repository at this point in the history
  • Loading branch information
Nadrieril committed Jan 27, 2025
1 parent cb7efa4 commit eb74442
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 13 deletions.
20 changes: 16 additions & 4 deletions src/ast/printer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,11 +217,8 @@ impl<'d> ToDisplayTree<'d> for TypingResult<'_> {
.to_display_tree(a)
.sep_then(a, ", ", format!("\"{s:?}\""))
.surrounded(a, "BorrowError(", ")"),
TypingResult::TypeError(TypeError::External(e)) => format!("{e}")
.to_display_tree(a)
.surrounded(a, "TypeError(\"", "\")"),
TypingResult::TypeError(e) => {
format!("{e:?}")
format!("{e}")
.to_display_tree(a)
.surrounded(a, "TypeError(\"", "\")")
}
Expand Down Expand Up @@ -460,6 +457,21 @@ impl Display for TypingResult<'_> {
}
}

impl Display for BorrowCheckError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{self:?}")
}
}

impl Display for TypeError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
TypeError::External(e) => write!(f, "{e}"),
_ => write!(f, "{self:?}"),
}
}
}

impl Debug for Mutability {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{self}")
Expand Down
59 changes: 58 additions & 1 deletion src/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use bincode::{Decode, Encode};
use gloo_utils::format::JsValueSerdeExt;
use serde::Serialize;
use std::cmp::Ordering;
use std::collections::BTreeMap;
use std::fmt::Write;
use wasm_bindgen::prelude::*;

Expand Down Expand Up @@ -360,12 +361,58 @@ pub fn compare_rulesets_js(
req: String,
left: String,
right: String,
structured: StructuredCompareOutput,
}
#[derive(Debug, Clone, Serialize)]
pub struct StructuredCompareOutput {
query: StructuredCompareOutputQuery,
left: StructuredCompareOutputResult,
right: StructuredCompareOutputResult,
}
#[derive(Debug, Clone, Serialize)]
pub struct StructuredCompareOutputQuery {
pattern: String,
#[serde(rename = "type")]
type_: String,
}
#[derive(Debug, Clone, Serialize)]
#[serde(tag = "kind")]
pub enum StructuredCompareOutputResult {
Success {
bindings: BTreeMap<String, String>,
},
BorrowError {
name: String,
bindings: BTreeMap<String, String>,
},
TypeError {
name: String,
},
}

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

let a = &Arenas::default();
let bindings_to_strings = |bindings: &BindingAssignments| {
bindings
.assignments
.iter()
.map(|(var, ty)| (var.to_string(), ty.to_string()))
.collect()
};
let ty_res_to_structured = |res: &TypingResult| match res {
TypingResult::Success(bindings) => StructuredCompareOutputResult::Success {
bindings: bindings_to_strings(bindings),
},
TypingResult::BorrowError(bindings, e) => StructuredCompareOutputResult::BorrowError {
name: e.to_string(),
bindings: bindings_to_strings(bindings),
},
TypingResult::TypeError(e) => StructuredCompareOutputResult::TypeError {
name: e.to_string(),
},
};
compare_rulesets(
a,
pat_depth,
Expand All @@ -376,11 +423,21 @@ pub fn compare_rulesets_js(
)
.into_iter()
.map(|(req, left, right)| {
let structured = StructuredCompareOutput {
query: StructuredCompareOutputQuery {
pattern: req.pat.to_string(),
type_: req.ty.to_string(),
},
left: ty_res_to_structured(&left),
right: ty_res_to_structured(&right),
};
let (left, right) = left.display_diffed(&right);
let req = req.to_string();
CompareOutput {
req: req.to_string(),
req,
left,
right,
structured,
}
})
.map(|out| JsValue::from_serde(&out).unwrap())
Expand Down
11 changes: 10 additions & 1 deletion web/src/components/Solver.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -415,10 +415,19 @@ export function CompareDisplay({
const worker = new Worker(new URL("./worker.ts", import.meta.url), {
type: "module",
});
const truncateAt = 300;
worker.onmessage = function (event) {
switch (event.data.type) {
case "compare":
setOutput(event.data.output);
var output = event.data.output;
const structured_compare = output.map(x => x.structured);
console.log(JSON.stringify(structured_compare));
if (output.length > truncateAt) {
const diff = output.length - truncateAt;
output.length = truncateAt;
output.push({ req: `and ${diff} more...` })
}
setOutput(output);
break;
case "loaded":
if (showCompare) {
Expand Down
7 changes: 0 additions & 7 deletions web/src/components/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import init, { RuleSetJs, compare_rulesets_js } from "../../typing_rust_patterns
(async () => {
await init({});

const truncateAt = 300;

addEventListener("message", async (event) => {
const data = event.data;
switch (data.type) {
Expand All @@ -16,11 +14,6 @@ import init, { RuleSetJs, compare_rulesets_js } from "../../typing_rust_patterns
data.tyDepth,
data.compareDirection
);
if (output.length > truncateAt) {
const diff = output.length - truncateAt;
output.length = truncateAt;
output.push({ req: `and ${diff} more...` })
}
postMessage({ type: "compare", output });
break;
}
Expand Down

0 comments on commit eb74442

Please sign in to comment.