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
use grammar::Parser;
use model::command::SmtpCommand;
use model::controll::ServerControll;

use tokio::prelude::*;

pub trait IntoParse
where
    Self: Sized,
{
    fn parse<P>(self, parser: P) -> Parse<Self, P> {
        Parse::new(self, parser)
    }
}

impl<S> IntoParse for S
where
    S: Stream,
{
}

pub struct Parse<S, P> {
    stream: S,
    parser: P,
}

impl<S, P> Parse<S, P> {
    pub fn new(stream: S, parser: P) -> Self {
        Self { stream, parser }
    }
}

impl<S, P> Stream for Parse<S, P>
where
    S: Stream<Item = ServerControll>,
    P: Parser,
{
    type Item = ServerControll;
    type Error = S::Error;
    fn poll(&mut self) -> Poll<Option<Self::Item>, Self::Error> {
        match try_ready!(self.stream.poll()) {
            Some(ServerControll::Command(SmtpCommand::Unknown(line))) => {
                match self.parser.command(&line) {
                    Ok(cmd) => Ok(Async::Ready(Some(ServerControll::Command(cmd)))),
                    _ => Ok(Async::Ready(Some(ServerControll::Command(
                        SmtpCommand::Unknown(line),
                    )))),
                }
            }
            pass => Ok(Async::Ready(pass)),
        }
    }
}