Skip to main content

NodeOrToken

Enum NodeOrToken 

Source
pub enum NodeOrToken<N, K> {
    Node(N),
    Token(K),
}
Expand description

Either a node or a token.

Iterators which visit both nodes and tokens yield the Element instance of this. Its Debug output is that of the node or token it holds.

Variants§

§

Node(N)

An interior node.

§

Token(K)

A token.

Implementations§

Source§

impl<N, K> NodeOrToken<N, K>

Source

pub fn into_node(self) -> Option<N>

Get the node, if this is one.

Source

pub fn into_token(self) -> Option<K>

Get the token, if this is one.

Source

pub const fn as_node(&self) -> Option<&N>

Get a reference to the node, if this is one.

Source

pub const fn as_token(&self) -> Option<&K>

Get a reference to the token, if this is one.

Source

pub const fn is_node(&self) -> bool

Test if this is a node.

Source

pub const fn is_token(&self) -> bool

Test if this is a token.

Source§

impl<'a, T, F> NodeOrToken<Node<'a, T, F>, Token<'a, T, F>>
where T: Copy, F: Flavor,

Source

pub fn value(&self) -> T

Access the data associated with the element.

Source

pub fn replace(&self, value: T) -> T

Replace the value of the element with a new one, returning the old value.

Source

pub fn span(&self) -> Span<F::Index>

Get the span of the element.

Source

pub fn range(&self) -> Range<usize> ⓘ

Access the Span of the element as a Range.

Source

pub fn parent(&self) -> Option<Node<'a, T, F>>

Get the parent node, None at the root of the tree.

Source

pub fn prev(&self) -> Option<Element<'a, T, F>>

Get the previous sibling.

Source

pub fn next(&self) -> Option<Element<'a, T, F>>

Get the next sibling.

Source

pub fn is_trivia(&self) -> bool

Test if the element is a trivia token, see Token::is_trivia. A node is never trivia.

Source

pub fn has_children(&self) -> bool

Check if the element has children, which a token never has.

Source

pub fn first(&self) -> Option<Element<'a, T, F>>

Get the first child, None for a token. See Node::first.

Source

pub fn last(&self) -> Option<Element<'a, T, F>>

Get the last child, None for a token. See Node::last.

Source

pub fn children(&self) -> Children<'a, T, F> ⓘ

Iterate over the children of the element, which is empty for a token. See Node::children.

Source

pub fn ancestors(&self) -> Ancestors<'a, T, F> ⓘ

Get an iterator over this element and its ancestors. See Node::ancestors.

Source

pub fn siblings(&self) -> Siblings<'a, T, F> ⓘ

Get an iterator over this element and its following siblings. See Node::siblings.

Source

pub fn walk(&self) -> Walk<'a, T, F> ⓘ

Walk the tree forward in depth-first order starting with this element. See Node::walk.

Source

pub fn walk_from(&self) -> Walk<'a, T, F> ⓘ

Walk forwards and upwards from this element, not including it or its descendants. See Node::walk_from.

Source

pub fn walk_events(&self) -> WalkEvents<'a, T, F> ⓘ

Walk forward from this element emitting events. See Node::walk_events.

Source

pub fn find_preceding<P>(&self, predicate: P) -> Option<Element<'a, T, F>>
where P: FnMut(Element<'a, T, F>) -> bool,

Find a preceding element which matches the given predicate.

A “preceding element” is one whose tokens immediately precede the tokens of the current element. This is found in two steps:

  1. Starting with the current element and moving through its ancestors, find the first prev sibling which matches the predicate.
  2. From that sibling, descend through last children for as long as each matches the predicate, and return the last match.

Step 2 stops at the first last child which does not match, so a matching element nested below a non-matching last child is not found.

§Examples
let tree = syntree::tree! {
    "root" => {
        "child1" => {
            ("token2", 1),
            "child2" => {
                ("token1", 2)
            }
        },
        "child3" => {
            "child4" => {
                ("token1", 4),
            }
        }
    }
};

let node = tree.node_with_range(3..3).ok_or("missing 3")?;
assert_eq!(node.value(), "token1");

let found = node.find_preceding(|n| n.span().end == 3 && n.has_children());
let found = found.ok_or("expected preceding node")?;
assert_eq!(found.value(), "child2");

Trait Implementations§

Source§

impl<N: Clone, K: Clone> Clone for NodeOrToken<N, K>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<N: Copy, K: Copy> Copy for NodeOrToken<N, K>

Source§

impl<N, K> Debug for NodeOrToken<N, K>
where N: Debug, K: Debug,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<N, K> Eq for NodeOrToken<N, K>
where N: Eq, K: Eq,

Source§

impl<N: Hash, K: Hash> Hash for NodeOrToken<N, K>

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<A, B, C, D> PartialEq<NodeOrToken<C, D>> for NodeOrToken<A, B>
where A: PartialEq<C>, B: PartialEq<D>,

Source§

fn eq(&self, other: &NodeOrToken<C, D>) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more

Auto Trait Implementations§

§

impl<N, K> Forget for NodeOrToken<N, K>

§

impl<N, K> Freeze for NodeOrToken<N, K>
where N: Freeze, K: Freeze,

§

impl<N, K> Move for NodeOrToken<N, K>

§

impl<N, K> RefUnwindSafe for NodeOrToken<N, K>

§

impl<N, K> Send for NodeOrToken<N, K>
where K: Send, N: Send,

§

impl<N, K> Sync for NodeOrToken<N, K>
where K: Sync, N: Sync,

§

impl<N, K> Unpin for NodeOrToken<N, K>
where K: Unpin, N: Unpin,

§

impl<N, K> UnsafeUnpin for NodeOrToken<N, K>
where N: UnsafeUnpin, K: UnsafeUnpin,

§

impl<N, K> UnwindSafe for NodeOrToken<N, K>
where N: UnwindSafe, K: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.