btp 0.1.2

A rust library of, a lightweight protocol, Blog Transfer Protocol.
Documentation
mod _impl {
    type Error = Box<dyn std::error::Error + Send + Sync>;
    use crate::socket::{BtpConfig, BtpSocket};
    use std::net::SocketAddr;
    use tokio::net::{TcpStream, lookup_host};

    #[cfg(feature = "tls")]
    use rustls::pki_types::ServerName;
    #[cfg(feature = "tls")]
    use tokio_rustls::{TlsConnector, client::TlsStream};
    /// src/client.rs
    impl BtpSocket<TcpStream, SocketAddr> {
        /// Takes the [`BtpConfig`]. Connects to the given [`SocketAddr`]; and, returns the [`BtpSocket`], without TLS, as client.
        ///
        /// [`BtpConfig`]: crate::socket::BtpConfig
        /// [`SocketAddr`]: std::net::SocketAddr
        /// [`BtpSocket`]: crate::socket::BtpSocket
        pub async fn connect(conf: BtpConfig) -> Result<BtpSocket<TcpStream, SocketAddr>, Error> {
            let sock_addr = lookup_host(&conf.addr)
                .await?
                .next()
                .ok_or("Couldn't find the IP address of the host")?;

            let sock = TcpStream::connect(&conf.addr).await.unwrap();

            Ok(BtpSocket::from(sock, conf, sock_addr))
        }

        #[cfg(feature = "tls")]
        /// Takes the [`BtpConfig`] and [`TcpConnector`]. Connects to the given [`SocketAddr`] with connector; and, returns the [`BtpSocket`], with TLS, as client.
        ///
        ///[`TcpConnector`]: tokio_rustls::TlsConnector
        /// [`BtpConfig`]: crate::socket::BtpConfig
        /// [`SocketAddr`]: std::net::SocketAddr
        /// [`BtpSocket`]: crate::socket::BtpSocket
        pub async fn connect_tls(
            connector: TlsConnector,
            conf: BtpConfig,
        ) -> Result<BtpSocket<TlsStream<TcpStream>, SocketAddr>, Error> {
            let sock_addr = lookup_host(&conf.addr)
                .await?
                .next()
                .ok_or("Couldn't find the IP address of the host")?;
            let server_name = ServerName::IpAddress(sock_addr.ip().into());

            let sock = TcpStream::connect(&conf.addr).await.unwrap();
            let tls_connection = connector.connect(server_name, sock).await?;

            Ok(BtpSocket::from(tls_connection, conf, sock_addr))
        }
    }
}