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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
use crate::{Pipe, Repetition, Result};
use std::marker::PhantomData;
use tuplify::{Extend, HList, Unpack};

/// Until combinator
pub trait UntilExt<I, O, E> {
    /// Applies a [Pipe] until the 2nd succeeds
    fn until<O2, P>(self, p: P) -> Until<Self, P, O, O2>
    where
        I: Clone,
        O: Unpack,
        O2: HList,
        (Vec<O::Output>,): Extend<O2>,
        Self: Sized,
        P: Pipe<I, O2, E>,
    {
        Until::new(self, p)
    }

    /// Applies `self` until `p` succeeds, `self` is applied 0 or more times
    fn no_more_until<O2, P>(self, p: P) -> NoMoreUntil<Self, P, O, O2>
    where
        I: Clone,
        O: Unpack,
        O2: HList,
        (Vec<O::Output>,): Extend<O2>,
        Self: Sized,
        P: Pipe<I, O2, E>,
    {
        NoMoreUntil::new(self, p)
    }

    /// repeats `self` with the given repetition then applies `p`
    fn repeat_until<O2, R, P>(self, r: R, p: P) -> RepeatUntil<Self, P, O, O2>
    where
        I: Clone,
        O: Unpack,
        O2: HList,
        R: TryInto<Repetition>,
        R::Error: std::error::Error,
        (Vec<O::Output>,): Extend<O2>,
        Self: Sized,
        P: Pipe<I, O2, E>,
    {
        RepeatUntil::new(self, p, r.try_into().unwrap())
    }
}

impl<I, O, E, P: Pipe<I, O, E>> UntilExt<I, O, E> for P {}

/// [UntilExt::until] implementation
pub struct Until<P1, P2, O, O2>(P1, P2, PhantomData<O>, PhantomData<O2>);

impl<P1, P2, O, O2> Until<P1, P2, O, O2> {
    fn new(p1: P1, p2: P2) -> Self { Self(p1, p2, PhantomData, PhantomData) }
}

impl<I, O, O2, E, P1, P2> Pipe<I, <(Vec<O::Output>,) as Extend<O2>>::Output, E>
    for Until<P1, P2, O, O2>
where
    I: Clone,
    O: Unpack,
    O2: HList,
    (Vec<O::Output>,): Extend<O2>,
    P1: Pipe<I, O, E>,
    P2: Pipe<I, O2, E>,
{
    fn apply(
        &mut self, mut input: I,
    ) -> Result<I, <(Vec<O::Output>,) as Extend<O2>>::Output, E> {
        let mut r = vec![];
        loop {
            match self.1.apply(input.clone()) {
                Ok((i, x)) => return Ok((i, (r,).extend(x))),
                Err(_) => match self.0.apply(input) {
                    Ok((i, x)) => {
                        input = i;
                        r.push(x.unpack());
                    }
                    Err(x) => return Err(x),
                },
            }
        }
    }
}

/// [UntilExt::no_more_until] implementation
pub struct NoMoreUntil<P1, P2, O, O2>(P1, P2, PhantomData<O>, PhantomData<O2>);

impl<P1, P2, O, O2> NoMoreUntil<P1, P2, O, O2> {
    fn new(p1: P1, p2: P2) -> Self { Self(p1, p2, PhantomData, PhantomData) }
}

impl<I, O, O2, E, P1, P2> Pipe<I, <(Vec<O::Output>,) as Extend<O2>>::Output, E>
    for NoMoreUntil<P1, P2, O, O2>
where
    I: Clone,
    O: Unpack,
    O2: HList,
    (Vec<O::Output>,): Extend<O2>,
    P1: Pipe<I, O, E>,
    P2: Pipe<I, O2, E>,
{
    fn apply(
        &mut self, mut input: I,
    ) -> Result<I, <(Vec<O::Output>,) as Extend<O2>>::Output, E> {
        let mut r = vec![];
        loop {
            match self.0.apply(input.clone()) {
                Ok((i, x)) => {
                    input = i;
                    r.push(x.unpack());
                }
                Err(_) => match self.1.apply(input) {
                    Ok((i, x)) => return Ok((i, (r,).extend(x))),
                    Err(x) => return Err(x),
                },
            }
        }
    }
}

/// [UntilExt::repeat_until] implementation
pub struct RepeatUntil<P1, P2, O, O2>(
    P1,
    P2,
    Repetition,
    PhantomData<O>,
    PhantomData<O2>,
);

impl<P1, P2, O, O2> RepeatUntil<P1, P2, O, O2> {
    fn new(p1: P1, p2: P2, r: Repetition) -> Self {
        Self(p1, p2, r, PhantomData, PhantomData)
    }
}

impl<I, O, O2, E, P1, P2> Pipe<I, <(Vec<O::Output>,) as Extend<O2>>::Output, E>
    for RepeatUntil<P1, P2, O, O2>
where
    I: Clone,
    O: Unpack,
    O2: HList,
    (Vec<O::Output>,): Extend<O2>,
    P1: Pipe<I, O, E>,
    P2: Pipe<I, O2, E>,
{
    fn apply(
        &mut self, mut input: I,
    ) -> Result<I, <(Vec<O::Output>,) as Extend<O2>>::Output, E> {
        let mut r = vec![];
        let mut nb = 0;
        loop {
            if self.2.is_max(nb) {
                let (i, x) = self.1.apply(input)?;
                return Ok((i, (r,).extend(x)));
            }
            if self.2.needs_more(nb) {
                let (i, x) = self.0.apply(input)?;
                input = i;
                r.push(x.unpack());
            } else {
                match self.1.apply(input.clone()) {
                    Ok((i, x)) => {
                        return Ok((i, (r,).extend(x)));
                    }
                    Err(_) => {
                        let (i, x) = self.0.apply(input)?;
                        input = i;
                        r.push(x.unpack());
                    }
                }
            }
            nb += 1;
        }
    }
}