tokio-xmpp: Error out when both tls-native and tls-rust features are enabled

Maxime “pep” Buquet created

If the user enables the tls-rust feature and forgets to disable
default-features (which includes tls-native), tell them and bail out.

The code was made to work anyway when both are enabled, and here it
defaults to tls-native. It does seem better to have one explicitely
choose one though hence the compile_error! message.

Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>

Change summary

tokio-xmpp/src/client/async_client.rs  |  2 +-
tokio-xmpp/src/client/simple_client.rs |  2 +-
tokio-xmpp/src/error.rs                | 10 +++++-----
tokio-xmpp/src/lib.rs                  |  3 +++
tokio-xmpp/src/starttls.rs             |  4 ++--
5 files changed, 12 insertions(+), 9 deletions(-)

Detailed changes

tokio-xmpp/src/client/async_client.rs 🔗

@@ -7,7 +7,7 @@ use tokio::net::TcpStream;
 use tokio::task::JoinHandle;
 #[cfg(feature = "tls-native")]
 use tokio_native_tls::TlsStream;
-#[cfg(feature = "tls-rust")]
+#[cfg(all(feature = "tls-rust", not(feature = "tls-native")))]
 use tokio_rustls::client::TlsStream;
 use xmpp_parsers::{ns, Element, Jid};
 

tokio-xmpp/src/client/simple_client.rs 🔗

@@ -7,7 +7,7 @@ use std::task::{Context, Poll};
 use tokio::net::TcpStream;
 #[cfg(feature = "tls-native")]
 use tokio_native_tls::TlsStream;
-#[cfg(feature = "tls-rust")]
+#[cfg(all(feature = "tls-rust", not(feature = "tls-native")))]
 use tokio_rustls::client::TlsStream;
 use tokio_stream::StreamExt;
 use xmpp_parsers::{ns, Element, Jid};

tokio-xmpp/src/error.rs 🔗

@@ -6,9 +6,9 @@ use std::error::Error as StdError;
 use std::fmt;
 use std::io::Error as IoError;
 use std::str::Utf8Error;
-#[cfg(feature = "tls-rust")]
+#[cfg(all(feature = "tls-rust", not(feature = "tls-native")))]
 use tokio_rustls::rustls::client::InvalidDnsNameError;
-#[cfg(feature = "tls-rust")]
+#[cfg(all(feature = "tls-rust", not(feature = "tls-native")))]
 use tokio_rustls::rustls::Error as TlsError;
 use trust_dns_proto::error::ProtoError;
 use trust_dns_resolver::error::ResolveError;
@@ -34,7 +34,7 @@ pub enum Error {
     Auth(AuthError),
     /// TLS error
     Tls(TlsError),
-    #[cfg(feature = "tls-rust")]
+    #[cfg(all(feature = "tls-rust", not(feature = "tls-native")))]
     /// DNS name parsing error
     DnsNameError(InvalidDnsNameError),
     /// Connection closed
@@ -57,7 +57,7 @@ impl fmt::Display for Error {
             Error::Protocol(e) => write!(fmt, "protocol error: {}", e),
             Error::Auth(e) => write!(fmt, "authentication error: {}", e),
             Error::Tls(e) => write!(fmt, "TLS error: {}", e),
-            #[cfg(feature = "tls-rust")]
+            #[cfg(all(feature = "tls-rust", not(feature = "tls-native")))]
             Error::DnsNameError(e) => write!(fmt, "DNS name error: {}", e),
             Error::Disconnected => write!(fmt, "disconnected"),
             Error::InvalidState => write!(fmt, "invalid state"),
@@ -117,7 +117,7 @@ impl From<Utf8Error> for Error {
     }
 }
 
-#[cfg(feature = "tls-rust")]
+#[cfg(all(feature = "tls-rust", not(feature = "tls-native")))]
 impl From<InvalidDnsNameError> for Error {
     fn from(e: InvalidDnsNameError) -> Self {
         Error::DnsNameError(e)

tokio-xmpp/src/lib.rs 🔗

@@ -2,6 +2,9 @@
 
 #![deny(unsafe_code, missing_docs, bare_trait_objects)]
 
+#[cfg(all(feature = "tls-native", feature = "tls-rust"))]
+compile_error!("Both tls-native and tls-rust features can't be enabled at the same time.");
+
 mod starttls;
 mod stream_start;
 mod xmpp_codec;

tokio-xmpp/src/starttls.rs 🔗

@@ -1,6 +1,6 @@
 use futures::{sink::SinkExt, stream::StreamExt};
 
-#[cfg(feature = "tls-rust")]
+#[cfg(all(feature = "tls-rust", not(feature = "tls-native")))]
 use {
     std::convert::TryFrom,
     std::sync::Arc,
@@ -37,7 +37,7 @@ async fn get_tls_stream<S: AsyncRead + AsyncWrite + Unpin>(
     Ok(tls_stream)
 }
 
-#[cfg(feature = "tls-rust")]
+#[cfg(all(feature = "tls-rust", not(feature = "tls-native")))]
 async fn get_tls_stream<S: AsyncRead + AsyncWrite + Unpin>(
     xmpp_stream: XMPPStream<S>,
 ) -> Result<TlsStream<S>, Error> {