#[cfg(feature = "tls")]
pub mod tls;
use std::io;
use sqlx_core::net::{Socket, WithSocket};
use crate::{
connection::websocket::socket::{ExaSocket, WithExaSocket},
etl::job::WithSocketMaker,
SqlxResult,
};
pub enum WithMaybeTlsSocketMaker {
NonTls,
#[cfg(feature = "tls")]
Tls(tls::WithTlsSocketMaker),
}
impl WithMaybeTlsSocketMaker {
pub fn new(
#[allow(unused_variables, reason = "conditionally compiled")] with_tls: bool,
#[allow(unused_variables, reason = "conditionally compiled")] with_pub_key: bool,
) -> SqlxResult<(Self, Option<String>)> {
#[cfg(feature = "tls")]
if with_tls {
let (wsm, public_key) = tls::WithTlsSocketMaker::new(with_pub_key)?;
return Ok((Self::Tls(wsm), public_key));
}
Ok((Self::NonTls, None))
}
}
impl WithSocketMaker for WithMaybeTlsSocketMaker {
type WithSocket = WithMaybeTlsSocket;
fn make_with_socket(&self, with_socket: WithExaSocket) -> Self::WithSocket {
match self {
Self::NonTls => WithMaybeTlsSocket::NonTls(with_socket),
#[cfg(feature = "tls")]
Self::Tls(w) => WithMaybeTlsSocket::Tls(w.make_with_socket(with_socket)),
}
}
}
pub enum WithMaybeTlsSocket {
NonTls(WithExaSocket),
#[cfg(feature = "tls")]
Tls(tls::WithTlsSocket),
}
impl WithSocket for WithMaybeTlsSocket {
type Output = io::Result<ExaSocket>;
async fn with_socket<S: Socket>(self, socket: S) -> Self::Output {
match self {
WithMaybeTlsSocket::NonTls(w) => Ok(w.with_socket(socket).await),
#[cfg(feature = "tls")]
WithMaybeTlsSocket::Tls(w) => w.with_socket(socket).await,
}
}
}