Skip to content

Commit

Permalink
Use semantic diff in comparison output too
Browse files Browse the repository at this point in the history
  • Loading branch information
Nadrieril committed Jan 2, 2025
1 parent d1871ba commit fad631d
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 30 deletions.
83 changes: 60 additions & 23 deletions src/ast/printer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,64 @@ impl<'d> ToDisplayTree<'d> for Type<'_> {
}
}

impl<'d> ToDisplayTree<'d> for BindingAssignments<'_> {
fn to_display_tree(&self, a: &'d Arenas<'d>) -> DisplayTree<'d> {
DisplayTree::sep_by(
a,
", ",
self.assignments
.iter()
.map(|(name, ty)| name.to_display_tree(a).sep_then(a, ": ", ty)),
)
}
}

impl<'d> ToDisplayTree<'d> for TypingResult<'_> {
fn to_display_tree(&self, a: &'d Arenas<'d>) -> DisplayTree<'d> {
match self {
TypingResult::Success(bindings) => {
bindings.to_display_tree(a).surrounded(a, "Success(", ")")
}
TypingResult::BorrowError(bindings, s) => bindings
.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:?}")
.to_display_tree(a)
.surrounded(a, "TypeError(\"", "\")")
}
}
}
}

impl TypingResult<'_> {
pub fn display(&self) -> String {
let a = &Arenas::default();
let out = self.to_display_tree(a).to_string();
match self {
TypingResult::Success(..) => out.green(),
TypingResult::BorrowError(..) | TypingResult::TypeError(..) => out.red(),
}
}

/// Display two typing results, adjusting colors to highlight differences.
pub fn display_diffed(&self, other: &Self) -> (String, String) {
let a = &Arenas::default();
match (self, other) {
(TypingResult::Success(..), TypingResult::Success(..)) => {
let left = self.to_display_tree(a);
let right = other.to_display_tree(a);
left.diff_display(&right)
}
_ => (self.to_string(), other.to_string()),
}
}
}

impl<'a> TypingPredicate<'a> {
/// Display as `let ...`.
pub fn display_as_let(&self) -> String {
Expand Down Expand Up @@ -359,30 +417,9 @@ impl Display for PredicateStyle {
}
}

impl std::fmt::Display for BindingAssignments<'_> {
impl Display for TypingResult<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{}",
self.assignments
.iter()
.map(|(name, ty)| format!("{name}: {ty}"))
.format(", ")
)
}
}

impl std::fmt::Display for TypingResult<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let out = match self {
TypingResult::Success(bindings) => format!("Success({bindings})").green(),
TypingResult::BorrowError(bindings, s) => {
format!("BorrowError({bindings}, \"{s:?}\")").red()
}
TypingResult::TypeError(TypeError::External(e)) => format!("TypeError(\"{e}\")").red(),
TypingResult::TypeError(e) => format!("TypeError(\"{e:?}\")").red(),
};
write!(f, "{}", out)
write!(f, "{}", self.display())
}
}

Expand Down
7 changes: 4 additions & 3 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,11 +234,12 @@ impl CliState {
of depth <= 3 and types of depth <= 4"
);
} else {
for (test_case, left_res, right_res) in differences {
for (test_case, left, right) in differences {
let (left, right) = left.display_diffed(&right);
let test_case_str = test_case.to_string();
println!("Difference on `{test_case_str}`:");
println!(" saved returned: {left_res}");
println!(" current returned: {right_res}");
println!(" saved returned: {left}");
println!(" current returned: {right}");
}
}
}
Expand Down
11 changes: 7 additions & 4 deletions src/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,10 +322,13 @@ pub fn compare_rulesets_js(
right_ruleset.as_ruleset(),
)
.into_iter()
.map(|(req, left, right)| CompareOutput {
req: req.to_string(),
left: left.to_string(),
right: right.to_string(),
.map(|(req, left, right)| {
let (left, right) = left.display_diffed(&right);
CompareOutput {
req: req.to_string(),
left,
right,
}
})
.map(|out| JsValue::from_serde(&out).unwrap())
.collect()
Expand Down

0 comments on commit fad631d

Please sign in to comment.