1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
//! Support for [Typist](https://typst.app/docs).

pub use pulldown_typst::{
    Bookmarks, CodeBlockDisplay, Event, LinkType, NumberingPattern, QuoteQuotes, QuoteType,
    ShowType, TableCellAlignment, TableOfContents, Tag,
};

use crate::ParserEvent;

pub mod to;

/// Assert that an iterator only contains Typst events. Panics if another type of event
/// is encountered.
///
/// For a non-panic version, see [`TypstFilter`].
pub struct AssertTypst<T>(pub T);
impl<'a, T> Iterator for AssertTypst<T>
where
    T: Iterator<Item = ParserEvent<'a>>,
{
    type Item = self::Event<'a>;

    fn next(&mut self) -> Option<Self::Item> {
        match self.0.next() {
            None => None,
            Some(ParserEvent::Typst(x)) => Some(x),
            #[cfg(feature = "markdown")]
            Some(ParserEvent::Markdown(x)) => panic!("unexpected markdown event: {x:?}"),
            #[cfg(feature = "mdbook")]
            Some(ParserEvent::Mdbook(x)) => panic!("unexpected mdbook event: {x:?}"),
        }
    }
}

/// An iterator that only contains Typst events. Other types of events will be filtered
/// out.
///
/// To panic when a non-Typst event is encountered, see [`AssertTypst`].
pub struct TypstFilter<T>(pub T);
impl<'a, T> Iterator for TypstFilter<T>
where
    T: Iterator<Item = ParserEvent<'a>>,
{
    type Item = Event<'a>;

    fn next(&mut self) -> Option<Self::Item> {
        match self.0.next() {
            None => None,
            Some(ParserEvent::Typst(x)) => Some(x),
            #[cfg(feature = "markdown")]
            Some(ParserEvent::Markdown(_)) => self.next(),
            #[cfg(feature = "mdbook")]
            Some(ParserEvent::Mdbook(_)) => self.next(),
        }
    }
}

/// An adaptor for events from a Typst parser.
pub struct TypstIter<T>(pub T);

impl<'a, T> Iterator for TypstIter<T>
where
    T: Iterator<Item = self::Event<'a>>,
{
    type Item = ParserEvent<'a>;

    fn next(&mut self) -> Option<Self::Item> {
        self.0.next().map(ParserEvent::Typst)
    }
}