pub struct ElementGraph { /* private fields */ }Expand description
Index-based graph of relationships between document elements.
Built from a &[Element] slice, the graph stores parent/child and next/prev
relationships as indices into the original slice. No ownership is taken and
no lifetimes are introduced — the graph is a standalone value that can be
stored, cloned, or sent across threads independently of the elements.
§Example
use oxidize_pdf::parser::PdfDocument;
use oxidize_pdf::pipeline::ElementGraph;
let doc = PdfDocument::open("document.pdf")?;
let (elements, graph) = doc.partition_graph(Default::default())?;
for title_idx in graph.top_level_sections() {
println!("Section: {}", elements[title_idx].text());
for &child_idx in graph.children_of(title_idx) {
println!(" {}", elements[child_idx].text());
}
}§Parent / child semantics
An element at index i is a child of the nearest preceding Title element
whose text matches elements[i].metadata().parent_heading. A Title element
whose own parent_heading equals its own text is treated as a root (no parent)
unless a different preceding title has the same text.
Implementations§
Source§impl ElementGraph
impl ElementGraph
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Number of elements in the graph (equals the length of the source slice).
Sourcepub fn parent_of(&self, idx: usize) -> Option<usize>
pub fn parent_of(&self, idx: usize) -> Option<usize>
Returns the index of the parent element of idx, or None if it is a root.
§Panics
Panics if idx >= self.len().
Sourcepub fn children_of(&self, idx: usize) -> &[usize]
pub fn children_of(&self, idx: usize) -> &[usize]
Sourcepub fn next_of(&self, idx: usize) -> Option<usize>
pub fn next_of(&self, idx: usize) -> Option<usize>
Returns the index of the element immediately after idx, or None if idx
is the last element.
§Panics
Panics if idx >= self.len().
Sourcepub fn prev_of(&self, idx: usize) -> Option<usize>
pub fn prev_of(&self, idx: usize) -> Option<usize>
Returns the index of the element immediately before idx, or None if idx
is the first element.
§Panics
Panics if idx >= self.len().
Sourcepub fn elements_in_section(&self, title_idx: usize) -> Vec<usize>
pub fn elements_in_section(&self, title_idx: usize) -> Vec<usize>
Returns the child indices of a Title element (i.e., the elements belonging to its section).
This is an alias for children_of with a semantically
clearer name for the common case of iterating a document section.
Sourcepub fn top_level_sections(&self) -> Vec<usize>
pub fn top_level_sections(&self) -> Vec<usize>
Returns the indices of all Title elements that have no parent (top-level sections).