Skip to content

Commit fad631d

Browse files
committed
Use semantic diff in comparison output too
1 parent d1871ba commit fad631d

File tree

3 files changed

+71
-30
lines changed

3 files changed

+71
-30
lines changed

src/ast/printer.rs

+60-23
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,64 @@ impl<'d> ToDisplayTree<'d> for Type<'_> {
123123
}
124124
}
125125

126+
impl<'d> ToDisplayTree<'d> for BindingAssignments<'_> {
127+
fn to_display_tree(&self, a: &'d Arenas<'d>) -> DisplayTree<'d> {
128+
DisplayTree::sep_by(
129+
a,
130+
", ",
131+
self.assignments
132+
.iter()
133+
.map(|(name, ty)| name.to_display_tree(a).sep_then(a, ": ", ty)),
134+
)
135+
}
136+
}
137+
138+
impl<'d> ToDisplayTree<'d> for TypingResult<'_> {
139+
fn to_display_tree(&self, a: &'d Arenas<'d>) -> DisplayTree<'d> {
140+
match self {
141+
TypingResult::Success(bindings) => {
142+
bindings.to_display_tree(a).surrounded(a, "Success(", ")")
143+
}
144+
TypingResult::BorrowError(bindings, s) => bindings
145+
.to_display_tree(a)
146+
.sep_then(a, ", ", format!("\"{s:?}\""))
147+
.surrounded(a, "BorrowError(", ")"),
148+
TypingResult::TypeError(TypeError::External(e)) => format!("{e}")
149+
.to_display_tree(a)
150+
.surrounded(a, "TypeError(\"", "\")"),
151+
TypingResult::TypeError(e) => {
152+
format!("{e:?}")
153+
.to_display_tree(a)
154+
.surrounded(a, "TypeError(\"", "\")")
155+
}
156+
}
157+
}
158+
}
159+
160+
impl TypingResult<'_> {
161+
pub fn display(&self) -> String {
162+
let a = &Arenas::default();
163+
let out = self.to_display_tree(a).to_string();
164+
match self {
165+
TypingResult::Success(..) => out.green(),
166+
TypingResult::BorrowError(..) | TypingResult::TypeError(..) => out.red(),
167+
}
168+
}
169+
170+
/// Display two typing results, adjusting colors to highlight differences.
171+
pub fn display_diffed(&self, other: &Self) -> (String, String) {
172+
let a = &Arenas::default();
173+
match (self, other) {
174+
(TypingResult::Success(..), TypingResult::Success(..)) => {
175+
let left = self.to_display_tree(a);
176+
let right = other.to_display_tree(a);
177+
left.diff_display(&right)
178+
}
179+
_ => (self.to_string(), other.to_string()),
180+
}
181+
}
182+
}
183+
126184
impl<'a> TypingPredicate<'a> {
127185
/// Display as `let ...`.
128186
pub fn display_as_let(&self) -> String {
@@ -359,30 +417,9 @@ impl Display for PredicateStyle {
359417
}
360418
}
361419

362-
impl std::fmt::Display for BindingAssignments<'_> {
420+
impl Display for TypingResult<'_> {
363421
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
364-
write!(
365-
f,
366-
"{}",
367-
self.assignments
368-
.iter()
369-
.map(|(name, ty)| format!("{name}: {ty}"))
370-
.format(", ")
371-
)
372-
}
373-
}
374-
375-
impl std::fmt::Display for TypingResult<'_> {
376-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
377-
let out = match self {
378-
TypingResult::Success(bindings) => format!("Success({bindings})").green(),
379-
TypingResult::BorrowError(bindings, s) => {
380-
format!("BorrowError({bindings}, \"{s:?}\")").red()
381-
}
382-
TypingResult::TypeError(TypeError::External(e)) => format!("TypeError(\"{e}\")").red(),
383-
TypingResult::TypeError(e) => format!("TypeError(\"{e:?}\")").red(),
384-
};
385-
write!(f, "{}", out)
422+
write!(f, "{}", self.display())
386423
}
387424
}
388425

src/cli.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -234,11 +234,12 @@ impl CliState {
234234
of depth <= 3 and types of depth <= 4"
235235
);
236236
} else {
237-
for (test_case, left_res, right_res) in differences {
237+
for (test_case, left, right) in differences {
238+
let (left, right) = left.display_diffed(&right);
238239
let test_case_str = test_case.to_string();
239240
println!("Difference on `{test_case_str}`:");
240-
println!(" saved returned: {left_res}");
241-
println!(" current returned: {right_res}");
241+
println!(" saved returned: {left}");
242+
println!(" current returned: {right}");
242243
}
243244
}
244245
}

src/wasm.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -322,10 +322,13 @@ pub fn compare_rulesets_js(
322322
right_ruleset.as_ruleset(),
323323
)
324324
.into_iter()
325-
.map(|(req, left, right)| CompareOutput {
326-
req: req.to_string(),
327-
left: left.to_string(),
328-
right: right.to_string(),
325+
.map(|(req, left, right)| {
326+
let (left, right) = left.display_diffed(&right);
327+
CompareOutput {
328+
req: req.to_string(),
329+
left,
330+
right,
331+
}
329332
})
330333
.map(|out| JsValue::from_serde(&out).unwrap())
331334
.collect()

0 commit comments

Comments
 (0)