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
use async_codec::{AsyncDecode, PollDec};
use async_codec::PollDec::{Done, Progress, Pending, Errored};
use futures_core::task::Context;
use futures_io::AsyncRead;

enum State<S, T, F> {
    First(S, F),
    Second(T),
}

/// Run a decoder and use the produced item to construct the next decoder to run.
pub struct AndThen<S, T, F>(State<S, T, F>);

impl<S, T, F> AndThen<S, T, F> {
    /// Run a decoder and use the produced item to construct the next decoder to run.
    pub fn new(first: S, f: F) -> AndThen<S, T, F> {
        AndThen(State::First(first, f))
    }
}

impl<S, T, F> AsyncDecode for AndThen<S, T, F>
    where S: AsyncDecode,
          T: AsyncDecode<Error = S::Error>,
          F: FnOnce(S::Item) -> T
{
    type Item = T::Item;
    type Error = T::Error;

    fn poll_decode<R: AsyncRead>(mut self,
                                 cx: &mut Context,
                                 reader: &mut R)
                                 -> PollDec<Self::Item, Self, Self::Error> {
        match self.0 {
            State::First(first, f) => {
                match first.poll_decode(cx, reader) {
                    Done(item, read) => {
                        self.0 = State::Second(f(item));
                        Progress(self, read)
                    }
                    Progress(first, read) => {
                        self.0 = State::First(first, f);
                        Progress(self, read)
                    }
                    Pending(first) => {
                        self.0 = State::First(first, f);
                        Pending(self)
                    }
                    Errored(err) => Errored(err),
                }
            }
            State::Second(second) => {
                match second.poll_decode(cx, reader) {
                    Done(item, read) => Done(item, read),
                    Progress(second, read) => {
                        self.0 = State::Second(second);
                        Progress(self, read)
                    }
                    Pending(second) => {
                        self.0 = State::Second(second);
                        Pending(self)
                    }
                    Errored(err) => Errored(err),
                }
            }
        }
    }
}