diff --git a/filter-parser/src/lib.rs b/filter-parser/src/lib.rs index ed36b1bf4..8b72c69ee 100644 --- a/filter-parser/src/lib.rs +++ b/filter-parser/src/lib.rs @@ -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); + } }