Skip to content

Commit bf41b35

Browse files
committed
added provided method evaluate_predicate_with_reasons for trait PredicateEvaluator
1 parent d09095e commit bf41b35

File tree

3 files changed

+27
-21
lines changed

3 files changed

+27
-21
lines changed

src/internals/postfix_stack_item.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ impl<Predicate> PostfixStackItem<'_, Predicate> {
1313
) -> bool {
1414
match self {
1515
PostfixStackItem::Predicate(predicate) => {
16-
evaluator.evaluate_predicate(predicate, reasons)
16+
evaluator.evaluate_predicate_with_reasons(predicate, reasons)
1717
}
1818
PostfixStackItem::Result(result) => *result,
1919
}
@@ -34,17 +34,17 @@ mod tests {
3434
type Predicate = bool;
3535
type Reason = i32;
3636

37-
fn evaluate_predicate(
38-
&self,
39-
predicate: &Self::Predicate,
40-
_reasons: &mut Vec<Self::Reason>,
41-
) -> bool {
37+
fn evaluate_predicate(&self, predicate: &Self::Predicate) -> bool {
4238
if self.val >= 0 {
4339
*predicate
4440
} else {
4541
!*predicate
4642
}
4743
}
44+
45+
fn get_reason(&self, _predicate: &Self::Predicate) -> Self::Reason {
46+
self.val
47+
}
4848
}
4949

5050
#[test]

src/traits/predicate_evaluator.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,25 @@ pub trait PredicateEvaluator {
22
type Predicate;
33
type Reason;
44

5-
fn evaluate_predicate(
5+
fn evaluate_predicate(&self, predicate: &Self::Predicate) -> bool;
6+
7+
fn get_reason(&self, predicate: &Self::Predicate) -> Self::Reason;
8+
9+
fn evaluate_predicate_with_reasons(
610
&self,
711
predicate: &Self::Predicate,
812
reasons: &mut Vec<Self::Reason>,
9-
) -> bool;
13+
) -> bool {
14+
let res = self.evaluate_predicate(predicate);
15+
16+
if res {
17+
reasons.push(self.get_reason(predicate));
18+
} else {
19+
reasons.clear();
20+
}
21+
22+
res
23+
}
1024
}
1125

1226
// impl PredicateEvaluator for () {

tests/postfix.rs

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,25 +23,17 @@ impl PredicateEvaluator for MyInteger {
2323
type Predicate = Predicate;
2424
type Reason = i32;
2525

26-
fn evaluate_predicate(
27-
&self,
28-
predicate: &Self::Predicate,
29-
reasons: &mut Vec<Self::Reason>,
30-
) -> bool {
31-
let res = match predicate.condition {
26+
fn evaluate_predicate(&self, predicate: &Self::Predicate) -> bool {
27+
match predicate.condition {
3228
PredicateCondition::Equal => self.val == predicate.val,
3329
PredicateCondition::NotEqual => self.val != predicate.val,
3430
PredicateCondition::GreaterThan => self.val > predicate.val,
3531
PredicateCondition::LowerThan => self.val < predicate.val,
36-
};
37-
38-
if res {
39-
reasons.push(predicate.val);
40-
} else {
41-
reasons.retain(|_| false);
4232
}
33+
}
4334

44-
res
35+
fn get_reason(&self, predicate: &Self::Predicate) -> Self::Reason {
36+
predicate.val
4537
}
4638
}
4739

0 commit comments

Comments
 (0)