organic/src/parser/token.rs

38 lines
1.2 KiB
Rust
Raw Normal View History

use super::Document;
use super::Element;
use super::Heading;
use super::Object;
use super::Section;
use crate::parser::DocumentElement;
pub enum Token<'r, 's> {
Document(&'r Document<'s>),
Heading(&'r Heading<'s>),
Section(&'r Section<'s>),
Object(&'r Object<'s>),
Element(&'r Element<'s>),
}
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>> + '_> {
match self {
2023-07-14 19:57:27 -04:00
Token::Document(document) => Box::new(
document
.zeroth_section
.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)),
Token::Object(_) => panic!(),
Token::Element(_) => panic!(),
}
}
}