1use crate::{element::{Element, ElementBorrowed}, hocr::{HOCRBorrowed, HOCR}};
2
3
4pub struct ElementsIterator<'a> {
5 elements: Vec<&'a Element>,
6}
7
8impl<'a> Iterator for ElementsIterator<'a> {
9 type Item = &'a Element;
10
11 fn next(&mut self) -> Option<Self::Item> {
12 self.elements.pop()
13 }
14}
15
16pub struct ElementsBorrowedIterator<'a> {
17 elements: Vec<&'a ElementBorrowed<'a>>,
18}
19
20impl<'a> Iterator for ElementsBorrowedIterator<'a> {
21 type Item = &'a ElementBorrowed<'a>;
22
23 fn next(&mut self) -> Option<Self::Item> {
24 self.elements.pop()
25 }
26}
27
28impl HOCR {
29 pub fn iter(&self) -> ElementsIterator {
32 let mut elements: Vec<&Element> = self.elements.iter().collect();
33 let mut index = 0;
34
35 while let Some(element) = elements.get(index) {
36 elements.extend(element.children.iter());
37 index += 1;
38 }
39
40 ElementsIterator { elements }
41 }
42}
43
44impl Element {
45 pub fn descendants(&self) -> ElementsIterator {
48 let mut elements: Vec<&Element> = self.children.iter().collect();
49 let mut index = 0;
50
51 while let Some(element) = elements.get(index) {
52 elements.extend(element.children.iter());
53 index += 1;
54 }
55
56 ElementsIterator { elements }
57 }
58}
59
60impl<'a> HOCRBorrowed<'a> {
61 pub fn iter(&self) -> ElementsBorrowedIterator {
64 let mut elements: Vec<&ElementBorrowed> = self.elements.iter().collect();
65 let mut index = 0;
66
67 while let Some(element) = elements.get(index) {
68 elements.extend(element.children.iter());
69 index += 1;
70 }
71
72 ElementsBorrowedIterator { elements }
73 }
74}
75
76impl<'a> ElementBorrowed<'a> {
77 pub fn descendants(&self) -> ElementsBorrowedIterator {
80 let mut elements: Vec<&ElementBorrowed> = self.children.iter().collect();
81 let mut index = 0;
82
83 while let Some(element) = elements.get(index) {
84 elements.extend(element.children.iter());
85 index += 1;
86 }
87
88 ElementsBorrowedIterator { elements }
89 }
90}