Introduce the depth method on FilterCondition

This commit is contained in:
Clément Renault 2021-12-06 17:35:20 +01:00
parent c83b77304a
commit 57502fcf6a
No known key found for this signature in database
GPG Key ID: 92ADA4E935E71FA4

View File

@ -113,6 +113,17 @@ pub enum FilterCondition<'a> {
}
impl<'a> FilterCondition<'a> {
pub fn depth(&self) -> usize {
match self {
FilterCondition::Condition { .. } => 1,
FilterCondition::Or(left, right) => 1 + left.depth().max(right.depth()),
FilterCondition::And(left, right) => 1 + left.depth().max(right.depth()),
FilterCondition::GeoLowerThan { .. } => 1,
FilterCondition::GeoGreaterThan { .. } => 1,
FilterCondition::Empty => 0,
}
}
pub fn negate(self) -> FilterCondition<'a> {
use FilterCondition::*;
@ -584,4 +595,10 @@ pub mod tests {
assert!(filter.starts_with(expected), "Filter `{:?}` was supposed to return the following error:\n{}\n, but instead returned\n{}\n.", input, expected, filter);
}
}
#[test]
fn depth() {
let filter = FilterCondition::parse("account_ids=1 OR account_ids=2 OR account_ids=3 OR account_ids=4 OR account_ids=5 OR account_ids=6").unwrap();
assert_eq!(filter.depth(), 6);
}
}