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 std::result;

use async_stream_packed::TlsClientUpgrader;
use futures_util::io::{AsyncRead, AsyncWrite};
use lettre::transport::smtp::{
    authentication::{Credentials, Mechanism},
    error::Error,
    extension::ClientId,
};

use crate::connection::AsyncConnection;
use crate::session::AsyncSession;

pub struct AsyncClient<S, STU>
where
    STU: TlsClientUpgrader<S>,
{
    connection: AsyncConnection<S, STU>,
}

impl<S, STU> AsyncClient<S, STU>
where
    STU: TlsClientUpgrader<S>,
{
    pub fn new(connection: AsyncConnection<S, STU>) -> Self {
        Self { connection }
    }
}

impl<S, STU> AsyncClient<S, STU>
where
    STU: TlsClientUpgrader<S> + Unpin,
    S: AsyncRead + AsyncWrite + Unpin,
    STU::Output: AsyncRead + AsyncWrite + Unpin,
{
    pub async fn handshake(
        &mut self,
        is_smtps: bool,
        hello_name: ClientId,
    ) -> result::Result<(), Error> {
        self.connection.handshake(is_smtps, hello_name).await
    }

    pub async fn auth<'a>(
        &'a mut self,
        mechanisms: &[Mechanism],
        credentials: &Credentials,
    ) -> result::Result<AsyncSession<'_, S, STU>, Error> {
        self.connection.auth(mechanisms, credentials).await?;

        Ok(AsyncSession::new(&mut self.connection))
    }
}