use core::iter::FusedIterator;
use crate::flavor::Flavor;
use crate::node::{Node, SkipTokens};
pub struct Ancestors<'a, T, F>
where
T: Copy,
F: Flavor,
{
node: Option<Node<'a, T, F>>,
}
impl<'a, T, F> Ancestors<'a, T, F>
where
T: Copy,
F: Flavor,
{
#[inline]
pub(crate) const fn new(node: Option<Node<'a, T, F>>) -> Self {
Self { node }
}
#[inline]
#[must_use]
pub const fn skip_tokens(self) -> SkipTokens<Self> {
SkipTokens::new(self)
}
#[inline]
pub fn next_node(&mut self) -> Option<Node<'a, T, F>> {
self.find(|n| n.has_children())
}
}
impl<'a, T, F> Iterator for Ancestors<'a, T, F>
where
T: Copy,
F: Flavor,
{
type Item = Node<'a, T, F>;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
let node = self.node.take()?;
self.node = node.parent();
Some(node)
}
}
impl<T, F> FusedIterator for Ancestors<'_, T, F>
where
T: Copy,
F: Flavor,
{
}
impl<T, F> Clone for Ancestors<'_, T, F>
where
T: Copy,
F: Flavor,
{
#[inline]
fn clone(&self) -> Self {
Self { node: self.node }
}
}
impl<T, F> Default for Ancestors<'_, T, F>
where
T: Copy,
F: Flavor,
{
#[inline]
fn default() -> Self {
Self { node: None }
}
}