syntree/node/skip_tokens.rs
1use core::fmt;
2use core::iter::FusedIterator;
3
4use crate::flavor::Flavor;
5use crate::node::{Element, Node};
6
7/// Wrapped around an iterator that excludes tokens, yielding only interior
8/// [`Node`]s.
9///
10/// Interior nodes are kept even when they are empty, since they were opened
11/// as nodes.
12///
13/// See [`Children::skip_tokens`], [`Siblings::skip_tokens`],
14/// [`Ancestors::skip_tokens`] or [`Walk::skip_tokens`].
15///
16/// [`Children::skip_tokens`]: crate::node::Children::skip_tokens
17/// [`Siblings::skip_tokens`]: crate::node::Siblings::skip_tokens
18/// [`Ancestors::skip_tokens`]: crate::node::Ancestors::skip_tokens
19/// [`Walk::skip_tokens`]: crate::node::Walk::skip_tokens
20///
21/// # Examples
22///
23/// Filtering tokens from a [`Children`] iterator:
24///
25/// ```
26/// let tree = syntree::tree! {
27/// ("token1", 1),
28/// "child1" => {
29/// "token2"
30/// },
31/// ("token3", 1),
32/// "child2" => {
33/// "token4"
34/// },
35/// ("token5", 1),
36/// "child3" => {
37/// "token6"
38/// },
39/// ("token7", 1)
40/// };
41///
42/// let it = tree.children().skip_tokens();
43///
44/// assert_eq!(
45/// it.map(|n| n.value()).collect::<Vec<_>>(),
46/// ["child1", "child2", "child3"]
47/// );
48/// # Ok::<_, Box<dyn core::error::Error>>(())
49/// ```
50///
51/// Filtering tokens from a [`Walk`] iterator:
52///
53/// ```
54/// let tree = syntree::tree! {
55/// "child1" => {
56/// "child2" => {
57/// "token1"
58/// },
59/// ("token2", 1),
60/// "child3" => {
61/// "token3"
62/// },
63/// },
64/// "child4" => {
65/// ("token4", 1)
66/// }
67/// };
68///
69/// let it = tree.walk().skip_tokens();
70///
71/// assert_eq!(
72/// it.map(|n| n.value()).collect::<Vec<_>>(),
73/// ["child1", "child2", "child3", "child4"]
74/// );
75/// # Ok::<_, Box<dyn core::error::Error>>(())
76/// ```
77///
78/// [`Children`]: crate::node::Children
79/// [`Walk`]: crate::node::Walk
80pub struct SkipTokens<U> {
81 iter: U,
82}
83
84impl<U> SkipTokens<U> {
85 #[inline]
86 pub(crate) const fn new(iter: U) -> Self {
87 Self { iter }
88 }
89}
90
91impl<'a, U, T: 'a, F: 'a> Iterator for SkipTokens<U>
92where
93 T: Copy,
94 F: Flavor,
95 U: Iterator<Item = Element<'a, T, F>>,
96{
97 type Item = Node<'a, T, F>;
98
99 #[inline]
100 fn next(&mut self) -> Option<Self::Item> {
101 self.iter.find_map(Element::into_node)
102 }
103
104 #[inline]
105 fn size_hint(&self) -> (usize, Option<usize>) {
106 let (_, upper) = self.iter.size_hint();
107 (0, upper)
108 }
109
110 #[inline]
111 fn find<P>(&mut self, mut predicate: P) -> Option<Self::Item>
112 where
113 Self: Sized,
114 P: FnMut(&Self::Item) -> bool,
115 {
116 self.iter
117 .find_map(move |e| e.into_node().filter(|n| predicate(n)))
118 }
119}
120
121impl<'a, U, T: 'a, F: 'a> DoubleEndedIterator for SkipTokens<U>
122where
123 T: Copy,
124 F: Flavor,
125 U: DoubleEndedIterator<Item = Element<'a, T, F>>,
126{
127 #[inline]
128 fn next_back(&mut self) -> Option<Self::Item> {
129 self.iter.by_ref().rev().find_map(Element::into_node)
130 }
131
132 #[inline]
133 fn rfind<P>(&mut self, mut predicate: P) -> Option<Self::Item>
134 where
135 Self: Sized,
136 P: FnMut(&Self::Item) -> bool,
137 {
138 self.iter
139 .by_ref()
140 .rev()
141 .find_map(move |e| e.into_node().filter(|n| predicate(n)))
142 }
143}
144
145impl<'a, U, T: 'a, F: 'a> FusedIterator for SkipTokens<U>
146where
147 T: Copy,
148 F: Flavor,
149 U: FusedIterator<Item = Element<'a, T, F>>,
150{
151}
152
153impl<U> Clone for SkipTokens<U>
154where
155 U: Clone,
156{
157 #[inline]
158 fn clone(&self) -> Self {
159 Self {
160 iter: self.iter.clone(),
161 }
162 }
163}
164
165impl<U> Default for SkipTokens<U>
166where
167 U: Default,
168{
169 #[inline]
170 fn default() -> Self {
171 Self {
172 iter: Default::default(),
173 }
174 }
175}
176
177impl<U> fmt::Debug for SkipTokens<U>
178where
179 U: fmt::Debug,
180{
181 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
182 f.debug_struct("SkipTokens")
183 .field("iter", &self.iter)
184 .finish()
185 }
186}