Skip to content

Commit

Permalink
added provided method evaluate_predicate_with_reasons for trait Predi…
Browse files Browse the repository at this point in the history
…cateEvaluator
  • Loading branch information
GyulyVGC committed Sep 24, 2024
1 parent d09095e commit bf41b35
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 21 deletions.
12 changes: 6 additions & 6 deletions src/internals/postfix_stack_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ impl<Predicate> PostfixStackItem<'_, Predicate> {
) -> bool {
match self {
PostfixStackItem::Predicate(predicate) => {
evaluator.evaluate_predicate(predicate, reasons)
evaluator.evaluate_predicate_with_reasons(predicate, reasons)
}
PostfixStackItem::Result(result) => *result,
}
Expand All @@ -34,17 +34,17 @@ mod tests {
type Predicate = bool;
type Reason = i32;

fn evaluate_predicate(
&self,
predicate: &Self::Predicate,
_reasons: &mut Vec<Self::Reason>,
) -> bool {
fn evaluate_predicate(&self, predicate: &Self::Predicate) -> bool {
if self.val >= 0 {
*predicate
} else {
!*predicate
}
}

fn get_reason(&self, _predicate: &Self::Predicate) -> Self::Reason {
self.val
}
}

#[test]
Expand Down
18 changes: 16 additions & 2 deletions src/traits/predicate_evaluator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,25 @@ pub trait PredicateEvaluator {
type Predicate;
type Reason;

fn evaluate_predicate(
fn evaluate_predicate(&self, predicate: &Self::Predicate) -> bool;

fn get_reason(&self, predicate: &Self::Predicate) -> Self::Reason;

fn evaluate_predicate_with_reasons(
&self,
predicate: &Self::Predicate,
reasons: &mut Vec<Self::Reason>,
) -> bool;
) -> bool {
let res = self.evaluate_predicate(predicate);

if res {
reasons.push(self.get_reason(predicate));
} else {
reasons.clear();
}

res
}
}

// impl PredicateEvaluator for () {
Expand Down
18 changes: 5 additions & 13 deletions tests/postfix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,17 @@ impl PredicateEvaluator for MyInteger {
type Predicate = Predicate;
type Reason = i32;

fn evaluate_predicate(
&self,
predicate: &Self::Predicate,
reasons: &mut Vec<Self::Reason>,
) -> bool {
let res = match predicate.condition {
fn evaluate_predicate(&self, predicate: &Self::Predicate) -> bool {
match predicate.condition {
PredicateCondition::Equal => self.val == predicate.val,
PredicateCondition::NotEqual => self.val != predicate.val,
PredicateCondition::GreaterThan => self.val > predicate.val,
PredicateCondition::LowerThan => self.val < predicate.val,
};

if res {
reasons.push(predicate.val);
} else {
reasons.retain(|_| false);
}
}

res
fn get_reason(&self, predicate: &Self::Predicate) -> Self::Reason {
predicate.val
}
}

Expand Down

0 comments on commit bf41b35

Please sign in to comment.