(feat) default feature for native_tls (aka openssl)

Establishes conditional compilation for all integration with the
native_tls crate in this crate. Since native_tls has been deeply
integrated into this crate for a long time, we want to maintain
backwards compatibility by making this feature part of the default.

For a consumer of this crate to "opt-out", including this in
cargo.toml:

```
[dependencies.imap]
version = 0.16.0        # Replace this with the correct version
default-features = false
```

See the conversation on Github for details on this approach:
https://github.com/jonhoo/rust-imap/issues/123
This commit is contained in:
Bryce Fisher-Fleig 2019-09-19 23:59:31 -07:00
parent c8288ea19b
commit f15bdfb458
6 changed files with 41 additions and 1 deletions

View file

@ -17,6 +17,7 @@ task:
build_script:
- . $HOME/.cargo/env
- cargo build --all-targets --verbose
- cargo build --all-targets --verbose --no-default-features
test_script:
- . $HOME/.cargo/env
- cargo test --examples

View file

@ -21,8 +21,12 @@ maintenance = { status = "actively-developed" }
is-it-maintained-issue-resolution = { repository = "jonhoo/rust-imap" }
is-it-maintained-open-issues = { repository = "jonhoo/rust-imap" }
[features]
tls = ["native-tls"]
default = ["tls"]
[dependencies]
native-tls = "0.2.2"
native-tls = { version = "0.2.2", optional = true }
regex = "1.0"
bufstream = "0.1"
imap-proto = "0.9.0"
@ -35,3 +39,15 @@ lazy_static = "1.4"
lettre = "0.9"
lettre_email = "0.9"
rustls-connector = "0.8.0"
[[example]]
name = "basic"
required-features = ["default"]
[[example]]
name = "gmail_oauth2"
required-features = ["default"]
[[test]]
name = "imap_integration"
required-features = ["default"]

View file

@ -41,6 +41,8 @@ stages:
displayName: Run doctests
- script: cargo test --lib
displayName: Run unit tests
- script: cargo build --all-targets --verbose --no-default-features
displayName: Compile without openssl
- job: integration
displayName: cargo test
pool:

View file

@ -1,5 +1,6 @@
use base64;
use bufstream::BufStream;
#[cfg(feature = "tls")]
use native_tls::{TlsConnector, TlsStream};
use nom;
use std::collections::HashSet;
@ -162,6 +163,7 @@ pub fn connect_insecure<A: ToSocketAddrs>(addr: A) -> Result<Client<TcpStream>>
/// let client = imap::connect(("imap.example.org", 993), "imap.example.org", &tls).unwrap();
/// # }
/// ```
#[cfg(feature = "tls")]
pub fn connect<A: ToSocketAddrs, S: AsRef<str>>(
addr: A,
domain: S,
@ -186,6 +188,7 @@ impl Client<TcpStream> {
/// This will upgrade an IMAP client from using a regular TCP connection to use TLS.
///
/// The domain parameter is required to perform hostname verification.
#[cfg(feature = "tls")]
pub fn secure<S: AsRef<str>>(
mut self,
domain: S,

View file

@ -3,6 +3,7 @@
use std::error::Error as StdError;
use std::fmt;
use std::io::Error as IoError;
#[cfg(feature = "tls")]
use std::net::TcpStream;
use std::result;
use std::str::Utf8Error;
@ -10,7 +11,9 @@ use std::str::Utf8Error;
use base64::DecodeError;
use bufstream::IntoInnerError as BufError;
use imap_proto::Response;
#[cfg(feature = "tls")]
use native_tls::Error as TlsError;
#[cfg(feature = "tls")]
use native_tls::HandshakeError as TlsHandshakeError;
/// A convenience wrapper around `Result` for `imap::Error`.
@ -22,8 +25,10 @@ pub enum Error {
/// An `io::Error` that occurred while trying to read or write to a network stream.
Io(IoError),
/// An error from the `native_tls` library during the TLS handshake.
#[cfg(feature = "tls")]
TlsHandshake(TlsHandshakeError<TcpStream>),
/// An error from the `native_tls` library while managing the socket.
#[cfg(feature = "tls")]
Tls(TlsError),
/// A BAD response from the IMAP server.
Bad(String),
@ -38,6 +43,8 @@ pub enum Error {
Validate(ValidateError),
/// Error appending an e-mail.
Append,
#[doc(hidden)]
__Nonexhaustive,
}
impl From<IoError> for Error {
@ -58,12 +65,14 @@ impl<T> From<BufError<T>> for Error {
}
}
#[cfg(feature = "tls")]
impl From<TlsHandshakeError<TcpStream>> for Error {
fn from(err: TlsHandshakeError<TcpStream>) -> Error {
Error::TlsHandshake(err)
}
}
#[cfg(feature = "tls")]
impl From<TlsError> for Error {
fn from(err: TlsError) -> Error {
Error::Tls(err)
@ -80,7 +89,9 @@ impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
Error::Io(ref e) => fmt::Display::fmt(e, f),
#[cfg(feature = "tls")]
Error::Tls(ref e) => fmt::Display::fmt(e, f),
#[cfg(feature = "tls")]
Error::TlsHandshake(ref e) => fmt::Display::fmt(e, f),
Error::Validate(ref e) => fmt::Display::fmt(e, f),
Error::No(ref data) | Error::Bad(ref data) => {
@ -95,7 +106,9 @@ impl StdError for Error {
fn description(&self) -> &str {
match *self {
Error::Io(ref e) => e.description(),
#[cfg(feature = "tls")]
Error::Tls(ref e) => e.description(),
#[cfg(feature = "tls")]
Error::TlsHandshake(ref e) => e.description(),
Error::Parse(ref e) => e.description(),
Error::Validate(ref e) => e.description(),
@ -103,13 +116,16 @@ impl StdError for Error {
Error::No(_) => "No Response",
Error::ConnectionLost => "Connection lost",
Error::Append => "Could not append mail to mailbox",
Error::__Nonexhaustive => "Unknown",
}
}
fn cause(&self) -> Option<&dyn StdError> {
match *self {
Error::Io(ref e) => Some(e),
#[cfg(feature = "tls")]
Error::Tls(ref e) => Some(e),
#[cfg(feature = "tls")]
Error::TlsHandshake(ref e) => Some(e),
Error::Parse(ParseError::DataNotUtf8(_, ref e)) => Some(e),
_ => None,

View file

@ -3,6 +3,7 @@
use crate::client::Session;
use crate::error::{Error, Result};
#[cfg(feature = "tls")]
use native_tls::TlsStream;
use std::io::{self, Read, Write};
use std::net::TcpStream;
@ -164,6 +165,7 @@ impl<'a> SetReadTimeout for TcpStream {
}
}
#[cfg(feature = "tls")]
impl<'a> SetReadTimeout for TlsStream<TcpStream> {
fn set_read_timeout(&mut self, timeout: Option<Duration>) -> Result<()> {
self.get_ref().set_read_timeout(timeout).map_err(Error::Io)