Skip to main content

tl/parser/
handle.rs

1use crate::Node;
2
3use super::Parser;
4
5/// The inner type of a NodeHandle, used to represent an index into the tags table
6pub type InnerNodeHandle = u32;
7
8/// A detached, external handle to a HTML node, originally obtained from a [Parser]
9///
10/// It contains an identifier that uniquely identifies an HTML node.
11/// In particular, it is an index into the global HTML tag table managed by the [`Parser`].
12/// To get a [`Node`] out of a [`NodeHandle`], call `NodeHandle::get()`
13///
14/// A common way to model self referential/recursive graphs is to have one "global" vector
15/// of nodes, and store indices into the vector instead of references.
16/// In the case of tl, the "global" HTML tag vector is stored in the [`Parser`] and [`NodeHandle`] represents the index.
17/// Because [`NodeHandle`] is only an index and completely detached from anything, you need to pass a parser to `NodeHandle::get()`
18#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
19#[repr(transparent)]
20pub struct NodeHandle(InnerNodeHandle);
21
22impl NodeHandle {
23    /// Creates a new handle to the given node
24    #[inline]
25    pub fn new(node: InnerNodeHandle) -> Self {
26        NodeHandle(node)
27    }
28
29    /// Returns a reference to the node that is associated to this specific handle
30    ///
31    /// It is an error to pass in the wrong parser.
32    /// It will either return `None` if this index points outside of the nodes table,
33    /// or it will return the one it points to.
34    pub fn get<
35        'p,
36        'buf,
37        const MAX_NODES: usize,
38        const MAX_STACK: usize,
39        const MAX_ROOTS: usize,
40        const MAX_IDS: usize,
41        const MAX_CLASSES: usize,
42        const MAX_SELECTOR_NODES: usize,
43    >(
44        &self,
45        parser: &'p Parser<
46            'buf,
47            MAX_NODES,
48            MAX_STACK,
49            MAX_ROOTS,
50            MAX_IDS,
51            MAX_CLASSES,
52            MAX_SELECTOR_NODES,
53        >,
54    ) -> Option<&'p Node<'buf>> {
55        parser.resolve_node_id(self.0)
56    }
57
58    /// Returns a mutable reference to the node that is associated to this specific handle
59    ///
60    /// It is an error to pass in the wrong parser.
61    /// It will either return `None` if this index points outside of the nodes table,
62    /// or it will return the one it points to.
63    pub fn get_mut<
64        'p,
65        'buf,
66        const MAX_NODES: usize,
67        const MAX_STACK: usize,
68        const MAX_ROOTS: usize,
69        const MAX_IDS: usize,
70        const MAX_CLASSES: usize,
71        const MAX_SELECTOR_NODES: usize,
72    >(
73        &self,
74        parser: &'p mut Parser<
75            'buf,
76            MAX_NODES,
77            MAX_STACK,
78            MAX_ROOTS,
79            MAX_IDS,
80            MAX_CLASSES,
81            MAX_SELECTOR_NODES,
82        >,
83    ) -> Option<&'p mut Node<'buf>> {
84        parser.resolve_node_id_mut(self.0)
85    }
86
87    /// Returns the internal unique Node ID that maps to a specific node in the node table
88    #[inline]
89    pub fn get_inner(&self) -> InnerNodeHandle {
90        self.0
91    }
92}