From 57502fcf6a8f7b3c846502e20ad9785b5ab12a52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Renault?= Date: Mon, 6 Dec 2021 17:35:20 +0100 Subject: [PATCH] Introduce the depth method on FilterCondition --- filter-parser/src/lib.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) 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); + } }