syntree/node/siblings.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
use core::iter::FusedIterator;
use crate::flavor::Flavor;
use crate::links::Links;
use crate::node::{Node, SkipTokens};
use crate::pointer::Pointer;
/// An iterator that iterates over the [`Node::next`] elements of a node. This is
/// typically used for iterating over the children of a tree.
///
/// Note that this iterator also implements [Default], allowing it to
/// effectively create an empty iterator in case a particular sibling is not
/// available:
///
/// ```
/// let mut tree = syntree::tree! {
/// "root" => {
/// "child1" => {
/// "child2" => {
/// "token1"
/// }
/// },
/// "child3" => {
/// "token2"
/// }
/// }
/// };
///
/// let mut it = tree.first().and_then(|n| n.next()).map(|n| n.siblings()).unwrap_or_default();
/// assert_eq!(it.next().map(|n| n.value()), None);
/// # Ok::<_, Box<dyn core::error::Error>>(())
/// ```
///
/// See [`Node::siblings`].
///
/// # Examples
///
/// ```
/// let mut tree = syntree::tree! {
/// "root" => {
/// "child1" => {
/// "child2" => {
/// "token1"
/// }
/// },
/// "child3" => {
/// "token2"
/// }
/// },
/// "root2" => {
/// "child4" => {
/// "token3"
/// }
/// }
/// };
///
/// let root = tree.first().ok_or("missing root")?;
///
/// assert_eq!(
/// root.siblings().map(|n| n.value()).collect::<Vec<_>>(),
/// ["root", "root2"]
/// );
/// # Ok::<_, Box<dyn core::error::Error>>(())
/// ```
pub struct Siblings<'a, T, F>
where
T: Copy,
F: Flavor,
{
tree: &'a [Links<T, F::Index, F::Pointer>],
links: Option<&'a Links<T, F::Index, F::Pointer>>,
}
impl<'a, T, F> Siblings<'a, T, F>
where
T: Copy,
F: Flavor,
{
/// Construct a new child iterator.
#[inline]
pub(crate) const fn new(
tree: &'a [Links<T, F::Index, F::Pointer>],
links: &'a Links<T, F::Index, F::Pointer>,
) -> Self {
Self {
tree,
links: Some(links),
}
}
/// Construct a [`SkipTokens`] iterator from the remainder of this iterator.
/// This filters out childless nodes, also known as tokens.
///
/// See [`SkipTokens`] for documentation.
#[must_use]
pub const fn skip_tokens(self) -> SkipTokens<Self> {
SkipTokens::new(self)
}
/// Get the next node from the iterator. This advances past all non-node
/// data.
///
/// # Examples
///
/// ```
/// let tree = syntree::tree! {
/// ("token1", 1),
/// "child1" => {
/// "token2"
/// },
/// ("token3", 1),
/// "child2" => {
/// "token4"
/// },
/// ("token5", 1),
/// "child3" => {
/// "token6"
/// },
/// ("token7", 1)
/// };
///
/// let first = tree.first().ok_or("missing first")?;
///
/// let mut it = first.siblings();
/// let mut out = Vec::new();
///
/// while let Some(n) = it.next_node() {
/// out.push(n.value());
/// }
///
/// assert_eq!(out, ["child1", "child2", "child3"]);
///
/// let mut it = first.siblings();
///
/// let c1 = it.next_node().ok_or("missing child1")?;
/// let c2 = it.next_node().ok_or("missing child2")?;
/// let c3 = it.next_node().ok_or("missing child3")?;
///
/// assert_eq!([c1.value(), c2.value(), c3.value()], ["child1", "child2", "child3"]);
/// # Ok::<_, Box<dyn core::error::Error>>(())
/// ```
#[inline]
pub fn next_node(&mut self) -> Option<Node<'a, T, F>> {
self.find(|n| n.has_children())
}
}
impl<'a, T, F> Iterator for Siblings<'a, T, F>
where
T: Copy,
F: Flavor,
{
type Item = Node<'a, T, F>;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
let links = self.links.take()?;
self.links = links.next.and_then(|id| self.tree.get(id.get()));
Some(Node::new(links, self.tree))
}
}
impl<T, F> FusedIterator for Siblings<'_, T, F>
where
T: Copy,
F: Flavor,
{
}
impl<T, F> Clone for Siblings<'_, T, F>
where
T: Copy,
F: Flavor,
{
#[inline]
fn clone(&self) -> Self {
Self {
tree: self.tree,
links: self.links,
}
}
}
impl<T, F> Default for Siblings<'_, T, F>
where
T: Copy,
F: Flavor,
{
#[inline]
fn default() -> Self {
Self {
tree: &[],
links: None,
}
}
}