Skip to content
This repository has been archived by the owner on Apr 4, 2023. It is now read-only.

Commit

Permalink
Change the depth function to return the token depth
Browse files Browse the repository at this point in the history
  • Loading branch information
Kerollmops committed Dec 7, 2021
1 parent 57502fc commit 1eeb61f
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions filter-parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,18 @@ pub enum FilterCondition<'a> {
}

impl<'a> FilterCondition<'a> {
pub fn depth(&self) -> usize {
pub fn token_at_depth(&self, depth: usize) -> Option<&Token> {
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,
FilterCondition::Condition { fid, .. } if depth == 0 => Some(fid),
FilterCondition::Or(left, right) => {
left.token_at_depth(depth - 1).or_else(|| right.token_at_depth(depth - 1))
}
FilterCondition::And(left, right) => {
left.token_at_depth(depth - 1).or_else(|| right.token_at_depth(depth - 1))
}
FilterCondition::GeoLowerThan { point: [point, _], .. } if depth == 0 => Some(point),
FilterCondition::GeoGreaterThan { point: [point, _], .. } if depth == 0 => Some(point),
_ => None,
}
}

Expand Down Expand Up @@ -599,6 +603,6 @@ pub mod tests {
#[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);
assert!(filter.token_at_depth(5).is_some());
}
}

0 comments on commit 1eeb61f

Please sign in to comment.