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
//! The stub transport only logs message envelope and drops the content. It can be useful for
//! testing purposes.
//!

mod error;

pub use self::error::*;
use crate::{
    stub::error::{Error, StubResult},
    Envelope, MailDataStream, SyncFuture, Transport,
};
use async_std::io::Write;
use futures::future;
use std::pin::Pin;
use std::task::{Context, Poll};

/// This transport logs the message envelope and returns the given response
#[derive(Debug)]
pub struct StubTransport {
    response: StubResult,
}

impl StubTransport {
    /// Creates a new transport that always returns the given response
    pub fn new(response: StubResult) -> StubTransport {
        StubTransport { response }
    }

    /// Creates a new transport that always returns a success response
    pub fn new_positive() -> StubTransport {
        StubTransport { response: Ok(()) }
    }
}

impl Transport for StubTransport {
    type DataStream = StubStream;
    fn send_stream<'life1, 'async_trait>(
        &'life1 self,
        envelope: Envelope,
    ) -> SyncFuture<Result<StubStream, Error>>
    where
        'life1: 'async_trait,
    {
        info!(
            "{}: from=<{}> to=<{:?}>",
            envelope.message_id(),
            match envelope.from() {
                Some(address) => address.to_string(),
                None => "".to_string(),
            },
            envelope.to()
        );
        Box::pin(future::ready(Ok(StubStream {
            response: self.response.clone(),
        })))
    }
}

#[derive(Debug)]
pub struct StubStream {
    response: StubResult,
}

impl MailDataStream for StubStream {
    type Output = ();
    type Error = Error;
    fn result(&self) -> StubResult {
        info!("Done: {:?}", self.response);
        self.response.clone()
    }
}

impl Write for StubStream {
    fn poll_write(
        self: Pin<&mut Self>,
        _cx: &mut Context<'_>,
        buf: &[u8],
    ) -> Poll<std::io::Result<usize>> {
        info!("Writing {} bytes", buf.len());
        Poll::Ready(Ok(buf.len()))
    }
    fn poll_flush(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<std::io::Result<()>> {
        info!("Flushing");
        Poll::Ready(Ok(()))
    }
    fn poll_close(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<std::io::Result<()>> {
        info!("Closing");
        Poll::Ready(Ok(()))
    }
}