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
use std::{io::Result as IoResult, net::SocketAddrV4};

use futures_core::future::BoxFuture;
use sqlx_core::{
    error::Error as SqlxError,
    net::{Socket, WithSocket},
};

use super::{get_etl_addr, traits::WithSocketMaker, SocketFuture, WithSocketFuture};
use crate::connection::websocket::socket::{ExaSocket, WithExaSocket};

/// Implementor of [`WithSocketMaker`] used for the creation of [`WithNonTlsSocket`].
pub struct NonTlsSocketSpawner;

impl WithSocketMaker for NonTlsSocketSpawner {
    type WithSocket = WithNonTlsSocket;

    fn make_with_socket(&self, wrapper: WithExaSocket) -> Self::WithSocket {
        WithNonTlsSocket(wrapper)
    }
}

/// Newtype implemented for uniform ETL socket spawning, even
/// though without TLS there's no need to return a future.
pub struct WithNonTlsSocket(WithExaSocket);

impl WithNonTlsSocket {
    #[allow(clippy::unused_async)]
    async fn wrap_socket<S: Socket>(self, socket: S) -> IoResult<ExaSocket> {
        let socket = self.0.with_socket(socket);
        Ok(socket)
    }

    async fn work<S: Socket>(self, socket: S) -> Result<(SocketAddrV4, SocketFuture), SqlxError> {
        let (socket, address) = get_etl_addr(socket).await?;
        let future: BoxFuture<'_, IoResult<ExaSocket>> = Box::pin(self.wrap_socket(socket));
        Ok((address, future))
    }
}

impl WithSocket for WithNonTlsSocket {
    type Output = WithSocketFuture;

    fn with_socket<S: Socket>(self, socket: S) -> Self::Output {
        Box::pin(self.work(socket))
    }
}