zrx_id/id/filter/terms.rs
1// Copyright (c) 2025-2026 Zensical and contributors
2
3// SPDX-License-Identifier: MIT
4// All contributions are certified under the DCO
5
6// Permission is hereby granted, free of charge, to any person obtaining a copy
7// of this software and associated documentation files (the "Software"), to
8// deal in the Software without restriction, including without limitation the
9// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10// sell copies of the Software, and to permit persons to whom the Software is
11// furnished to do so, subject to the following conditions:
12
13// The above copyright notice and this permission notice shall be included in
14// all copies or substantial portions of the Software.
15
16// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18// FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
19// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22// IN THE SOFTWARE.
23
24// ----------------------------------------------------------------------------
25
26//! Iterator over terms.
27
28use slab::Iter;
29
30use crate::id::expression::Term;
31
32use super::condition::Condition;
33use super::Filter;
34
35// ----------------------------------------------------------------------------
36// Structs
37// ----------------------------------------------------------------------------
38
39/// Iterator over terms.
40pub struct Terms<'a> {
41 /// Condition set, built from expressions.
42 conditions: Iter<'a, Condition>,
43 /// Current set of extracted terms.
44 inner: &'a [Term],
45}
46
47// ----------------------------------------------------------------------------
48// Implementations
49// ----------------------------------------------------------------------------
50
51impl Filter {
52 /// Creates an iterator over the terms.
53 ///
54 /// # Examples
55 ///
56 /// ```
57 /// # use std::error::Error;
58 /// # fn main() -> Result<(), Box<dyn Error>> {
59 /// use zrx_id::{selector, Expression, Filter};
60 ///
61 /// // Create filter builder and insert expression
62 /// let mut builder = Filter::builder();
63 /// builder.insert(Expression::any(|expr| {
64 /// expr.with(selector!(location = "**/*.jpg")?)?
65 /// .with(selector!(location = "**/*.png")?)
66 /// })?);
67 ///
68 /// // Create filter from builder
69 /// let filter = builder.build()?;
70 ///
71 /// // Create iterator over terms
72 /// for term in filter.terms() {
73 /// println!("{term:?}");
74 /// }
75 /// # Ok(())
76 /// # }
77 /// ```
78 #[inline]
79 #[must_use]
80 pub fn terms(&self) -> Terms<'_> {
81 Terms {
82 conditions: self.conditions.iter(),
83 inner: &[],
84 }
85 }
86}
87
88// ----------------------------------------------------------------------------
89// Trait implementations
90// ----------------------------------------------------------------------------
91
92impl<'a> Iterator for Terms<'a> {
93 type Item = &'a Term;
94
95 /// Returns the next term.
96 fn next(&mut self) -> Option<Self::Item> {
97 loop {
98 // Check if we have terms left in the current condition
99 if let Some(term) = self.inner.first() {
100 self.inner = &self.inner[1..];
101 return Some(term);
102 }
103
104 // Fetch next condition and extract its terms
105 let (_, condition) = self.conditions.next()?;
106 self.inner = condition.terms();
107 }
108 }
109}