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
use crate::{SyntaxNode, SyntaxToken};

/// An element of a syntax tree.
/// Either a node or a token.
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum SyntaxElement<C> {
    #[allow(missing_docs)]
    Node(SyntaxNode<C>),
    #[allow(missing_docs)]
    Token(SyntaxToken<C>),
}

impl<C> SyntaxElement<C> {
    /// Asserts this element is a node. Panics if it was actually a token.
    pub fn unwrap_node(self) -> SyntaxNode<C> {
        match self {
            Self::Node(node) => node,
            Self::Token(_) => panic!("expected node"),
        }
    }

    /// Asserts this element is a token. Panics if it was actually a node.
    pub fn unwrap_token(self) -> SyntaxToken<C> {
        match self {
            Self::Node(_) => panic!("expected token"),
            Self::Token(token) => token,
        }
    }
}