Skip to content

Commit b221daf

Browse files
committed
front: Diff rules more precisely
1 parent f456db6 commit b221daf

File tree

2 files changed

+61
-18
lines changed

2 files changed

+61
-18
lines changed

src/analyses/compute_rules.rs

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ impl RenderablePredicate<'_> {
332332
}
333333

334334
/// Intermediate representation used in the display process.
335-
struct RenderableTypingRule<'a> {
335+
pub(crate) struct RenderableTypingRule<'a> {
336336
name: Rule,
337337
preconditions: Vec<RenderablePredicate<'a>>,
338338
postconditions: Vec<RenderablePredicate<'a>>,
@@ -348,7 +348,7 @@ impl<'a> TypingRule<'a> {
348348
cstrs
349349
}
350350

351-
fn make_renderable(
351+
pub(crate) fn make_renderable(
352352
&'a self,
353353
_a: &'a Arenas<'a>,
354354
style: PredicateStyle,
@@ -442,12 +442,26 @@ impl<'a> TypingRule<'a> {
442442

443443
pub fn display(&self, style: PredicateStyle) -> Result<String, IncompatibleStyle> {
444444
let a = &Arenas::default();
445-
self.make_renderable(a, style)?.display(style)
445+
Ok(self.make_renderable(a, style)?.display(style))
446446
}
447447
}
448448

449449
impl<'a> RenderableTypingRule<'a> {
450-
pub fn display(&self, style: PredicateStyle) -> Result<String, IncompatibleStyle> {
450+
pub fn display(&self, style: PredicateStyle) -> String {
451+
Self::assemble_pieces(self.display_piecewise(style))
452+
}
453+
454+
pub fn assemble_pieces(
455+
[preconditions_str, bar, name, postconditions_str]: [String; 4],
456+
) -> String {
457+
let mut out = String::new();
458+
let _ = write!(&mut out, "{preconditions_str}\n");
459+
let _ = write!(&mut out, "{bar} \"{name}\"\n");
460+
let _ = write!(&mut out, "{postconditions_str}");
461+
out
462+
}
463+
464+
pub fn display_piecewise(&self, style: PredicateStyle) -> [String; 4] {
451465
let postconditions_str = self
452466
.postconditions
453467
.iter()
@@ -487,11 +501,8 @@ impl<'a> RenderableTypingRule<'a> {
487501
display_len(&postconditions_str),
488502
);
489503
let bar = "-".repeat(len);
490-
let mut out = String::new();
491-
let _ = write!(&mut out, "{preconditions_str}\n");
492-
let _ = write!(&mut out, "{bar} \"{}\"\n", self.name.display(self.options));
493-
let _ = write!(&mut out, "{postconditions_str}");
494-
Ok(out)
504+
let name = self.name.display(self.options);
505+
[preconditions_str, bar, name, postconditions_str]
495506
}
496507
}
497508

src/wasm.rs

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::*;
22
use bincode::{Decode, Encode};
33
use gloo_utils::format::JsValueSerdeExt;
4+
use itertools::EitherOrBoth;
45
use serde::Serialize;
56
use std::cmp::Ordering;
67
use std::fmt::Write;
@@ -271,18 +272,49 @@ pub fn display_joint_rules_js(
271272

272273
assert!(left.this_solver);
273274
assert!(right.this_solver);
274-
let arenas = &Arenas::default();
275+
let a = &Arenas::default();
275276
let type_of_interest = style.type_of_interest();
276-
compute_joint_rules(arenas, type_of_interest, left.ty_based, right.ty_based)
277+
compute_joint_rules(a, type_of_interest, left.ty_based, right.ty_based)
277278
.into_iter()
278279
.map(|joint_rule| {
279-
let (left, right) = joint_rule.left_and_right();
280-
let mut left = left.map(|r| r.display(style).unwrap()).unwrap_or_default();
281-
let mut right = right.map(|r| r.display(style).unwrap()).unwrap_or_default();
282-
if left != right {
283-
left = left.red();
284-
right = right.green();
285-
}
280+
let joint_rule = joint_rule
281+
.as_ref()
282+
.map_left(|r| r.make_renderable(a, style).unwrap())
283+
.map_right(|r| r.make_renderable(a, style).unwrap());
284+
let (left, right) = match joint_rule {
285+
EitherOrBoth::Left(left) => {
286+
let left = left.display(style).red();
287+
(left, String::new())
288+
}
289+
EitherOrBoth::Right(right) => {
290+
let right = right.display(style).green();
291+
(String::new(), right)
292+
}
293+
EitherOrBoth::Both(left, right) => {
294+
let [mut lpreconditions_str, lbar, lname, lpostconditions_str] =
295+
left.display_piecewise(style);
296+
let [mut rpreconditions_str, rbar, rname, rpostconditions_str] =
297+
right.display_piecewise(style);
298+
if lpreconditions_str != rpreconditions_str {
299+
lpreconditions_str = lpreconditions_str.red();
300+
rpreconditions_str = rpreconditions_str.green();
301+
}
302+
assert_eq!(lpostconditions_str, rpostconditions_str);
303+
let left = RenderableTypingRule::assemble_pieces([
304+
lpreconditions_str,
305+
lbar,
306+
lname,
307+
lpostconditions_str,
308+
]);
309+
let right = RenderableTypingRule::assemble_pieces([
310+
rpreconditions_str,
311+
rbar,
312+
rname,
313+
rpostconditions_str,
314+
]);
315+
(left, right)
316+
}
317+
};
286318
JointDisplayOutput { left, right }
287319
})
288320
.map(|out| JsValue::from_serde(&out).unwrap())

0 commit comments

Comments
 (0)