2023-04-24 22:10:24 -04:00
|
|
|
use super::Document;
|
|
|
|
use super::Element;
|
|
|
|
use super::Heading;
|
|
|
|
use super::Object;
|
|
|
|
use super::Section;
|
2023-07-14 19:54:41 -04:00
|
|
|
use crate::parser::DocumentElement;
|
2023-04-24 22:10:24 -04:00
|
|
|
|
|
|
|
pub enum Token<'r, 's> {
|
2023-07-14 19:54:41 -04:00
|
|
|
Document(&'r Document<'s>),
|
2023-04-24 22:10:24 -04:00
|
|
|
Heading(&'r Heading<'s>),
|
|
|
|
Section(&'r Section<'s>),
|
|
|
|
Object(&'r Object<'s>),
|
|
|
|
Element(&'r Element<'s>),
|
|
|
|
}
|
2023-07-14 19:54:41 -04:00
|
|
|
|
|
|
|
impl<'r, 's> Token<'r, 's> {
|
2023-07-14 19:57:27 -04:00
|
|
|
pub fn iter_tokens(&self) -> Box<dyn Iterator<Item = Token<'r, 's>> + '_> {
|
2023-07-14 19:54:41 -04:00
|
|
|
match self {
|
2023-07-14 19:57:27 -04:00
|
|
|
Token::Document(document) => Box::new(
|
|
|
|
document
|
|
|
|
.zeroth_section
|
2023-07-14 19:54:41 -04:00
|
|
|
.iter()
|
2023-07-14 19:57:27 -04:00
|
|
|
.map(Token::Section)
|
|
|
|
.chain(document.children.iter().map(Token::Heading)),
|
|
|
|
),
|
|
|
|
Token::Heading(heading) => Box::new(heading.title.iter().map(Token::Object).chain(
|
|
|
|
heading.children.iter().map(|de| match de {
|
|
|
|
DocumentElement::Heading(ref obj) => Token::Heading(obj),
|
|
|
|
DocumentElement::Section(ref obj) => Token::Section(obj),
|
|
|
|
}),
|
|
|
|
)),
|
|
|
|
Token::Section(section) => Box::new(section.children.iter().map(Token::Element)),
|
2023-07-14 19:54:41 -04:00
|
|
|
Token::Object(_) => panic!(),
|
|
|
|
Token::Element(_) => panic!(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|