Skip to content

Commit

Permalink
Handle errors in evaluate
Browse files Browse the repository at this point in the history
  • Loading branch information
ligustah committed Apr 3, 2024
1 parent 30d1631 commit 9f8738e
Showing 1 changed file with 19 additions and 7 deletions.
26 changes: 19 additions & 7 deletions src/rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,18 +65,30 @@ impl RulesEngine {
self.rules.push(Box::new(rule));
}

pub async fn evaluate(&self, item: PremintTypes, context: RuleContext) -> bool {
pub async fn evaluate(&self, item: PremintTypes, context: RuleContext) -> eyre::Result<bool> {
let results: Vec<_> = self
.rules
.iter()
.map(|rule| rule.check(item.clone(), context.clone()))
.collect();
let all_checks = join_all(results).await;

// TODO: handle errors
all_checks
.iter()
.all(|check| check.is_ok() && check.as_ref().unwrap().clone())
// TODO: ideally we'd want to return a list of all errors
// so that a caller could determine which rules failed and why
for error in all_checks.into_iter() {
match error {
Err(e) => {
return Err(e);
}
Ok(pass) => {
if !pass {
return Ok(false)
}
}
}
}

Ok(true)
}
}

Expand Down Expand Up @@ -130,7 +142,7 @@ mod test {
.evaluate(PremintTypes::Simple(Default::default()), context)
.await;

assert!(result);
assert!(result.unwrap());
}

#[tokio::test]
Expand All @@ -151,6 +163,6 @@ mod test {
.evaluate(PremintTypes::Simple(Default::default()), context)
.await;

assert!(result);
assert!(result.unwrap());
}
}

0 comments on commit 9f8738e

Please sign in to comment.