summaryrefslogtreecommitdiff
path: root/src/nibble/parse/iter.rs
blob: 1c635a38f2ffd165b94716683d937b07fb50c032 (plain)
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
use std::iter::Peekable;

pub use self::take_while::PeekableTakeWhile;

/// An iterator with a `peek()` that returns an optional reference to the next
/// element.
///
/// This trait is an extension of the `Peekable` type in the standard library.
pub trait Peek: Iterator {
    /// Returns a reference to the [`next`] value without advancing the iterator.
    ///
    /// Like [`next`], if there is a value, it is wrapped in [`Some`]. But if
    /// iteration is over, [`None`] is returned.
    fn peek(&mut self) -> Option<&Self::Item>;
}

impl<I: Peek> Peek for &mut I {
    fn peek(&mut self) -> Option<&Self::Item> {
        I::peek(self)
    }
}

impl<I> Peek for Box<&mut dyn Peek<Item = I>> {
    fn peek(&mut self) -> Option<&Self::Item> {
        (**self).peek()
    }
}

impl<I: Iterator> Peek for Peekable<I> {
    fn peek(&mut self) -> Option<&Self::Item> {
        Peekable::peek(self)
    }
}

pub trait PeekExt: Peek {
    /// Returns the [`next`] value and advance the iterator only if `pred` is true.
    fn next_if<P: FnOnce(&Self::Item) -> bool>(
        &mut self,
        pred: P,
    ) -> Option<Result<Self::Item, &mut Self>> {
        let i = self.peek()?;

        if pred(i) {
            Some(Ok(self.next().unwrap()))
        } else {
            Some(Err(self))
        }
    }

    fn next_if_then<
        P: FnOnce(&Self::Item) -> bool,
        F: FnOnce(Self::Item) -> T,
        G: FnOnce(&mut Self) -> T,
        T,
    >(
        &mut self,
        pred: P,
        matched: F,
        unmatched: G,
    ) -> Option<T> {
        let i = self.peek()?;

        if pred(i) {
            Some(matched(self.next().unwrap()))
        } else {
            Some(unmatched(self))
        }
    }

    /// Advances the iterator only if the [`next`] item equals `val`.
    fn next_if_eq<T>(&mut self, val: &T) -> Option<Result<(), &Self::Item>>
    where
        Self::Item: PartialEq<T>,
    {
        self.next_if(|i| PartialEq::eq(i, val))
            .map(|r| r.map(|_| ()).map_err(|i| i.peek().unwrap()))
    }

    fn consume_if_next<T>(&mut self, val: &T) -> bool
    where
        Self::Item: PartialEq<T>,
    {
        self.next_if_eq(val).map(|r| r.is_ok()).unwrap_or(false)
    }

    /// Advance the iterator until `pred` is false, or `peek` returns [`None`].
    fn advance_while<P: FnMut(&Self::Item) -> bool>(&mut self, mut pred: P) {
        while self.next_if(&mut pred).map(|r| r.is_ok()).unwrap_or(false) {
            // Condition already loops for us
        }
    }
}

impl<I: Peek> PeekExt for I {}

mod take_while {
    use super::Peek;

    #[derive(Clone, Debug, Eq, PartialEq)]
    pub struct PeekableTakeWhile<I, F> {
        inner: I,
        predicate: Option<F>,
    }

    impl<I, F> PeekableTakeWhile<I, F>
    where
        I: Peek,
        F: FnMut(&<I as Iterator>::Item) -> bool,
    {
        pub fn new(iter: I, predicate: F) -> Self {
            Self {
                inner: iter,
                predicate: Some(predicate),
            }
        }
    }

    impl<I, F> Iterator for PeekableTakeWhile<I, F>
    where
        I: Peek,
        F: FnMut(&<I as Iterator>::Item) -> bool,
    {
        type Item = <I as Iterator>::Item;

        fn next(&mut self) -> Option<Self::Item> {
            // Can't inline because of lifetimes
            let inner = &mut self.inner;
            if self.predicate.as_mut().and_then(|p| inner.peek().map(p))? {
                self.inner.next()
            } else {
                self.predicate = None;
                None
            }
        }
    }

    impl<I, F> Peek for PeekableTakeWhile<I, F>
    where
        I: Peek,
        F: FnMut(&<I as Iterator>::Item) -> bool,
    {
        fn peek(&mut self) -> Option<&Self::Item> {
            let inner = &mut self.inner;
            self.predicate
                .as_mut()
                .and_then(move |p| inner.peek().filter(|i| p(i)))
        }
    }
}