error.rs

  1//! StartTLS ServerConnector Error
  2
  3use hickory_resolver::{error::ResolveError, proto::error::ProtoError};
  4#[cfg(feature = "tls-native")]
  5use native_tls::Error as TlsError;
  6use std::borrow::Cow;
  7use std::error::Error as StdError;
  8use std::fmt;
  9#[cfg(all(feature = "tls-rust", not(feature = "tls-native")))]
 10use tokio_rustls::rustls::client::InvalidDnsNameError;
 11#[cfg(all(feature = "tls-rust", not(feature = "tls-native")))]
 12use tokio_rustls::rustls::Error as TlsError;
 13
 14/// StartTLS ServerConnector Error
 15#[derive(Debug)]
 16pub enum Error {
 17    /// Error resolving DNS and establishing a connection
 18    Connection(ConnectorError),
 19    /// DNS label conversion error, no details available from module
 20    /// `idna`
 21    Idna,
 22    /// TLS error
 23    Tls(TlsError),
 24    #[cfg(all(feature = "tls-rust", not(feature = "tls-native")))]
 25    /// DNS name parsing error
 26    DnsNameError(InvalidDnsNameError),
 27    /// tokio-xmpp error
 28    TokioXMPP(crate::error::Error),
 29}
 30
 31impl fmt::Display for Error {
 32    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
 33        match self {
 34            Error::Connection(e) => write!(fmt, "connection error: {}", e),
 35            Error::Idna => write!(fmt, "IDNA error"),
 36            Error::Tls(e) => write!(fmt, "TLS error: {}", e),
 37            #[cfg(all(feature = "tls-rust", not(feature = "tls-native")))]
 38            Error::DnsNameError(e) => write!(fmt, "DNS name error: {}", e),
 39            Error::TokioXMPP(e) => write!(fmt, "TokioXMPP error: {}", e),
 40        }
 41    }
 42}
 43
 44impl StdError for Error {}
 45
 46impl From<crate::error::Error> for Error {
 47    fn from(e: crate::error::Error) -> Self {
 48        Error::TokioXMPP(e)
 49    }
 50}
 51
 52impl From<ConnectorError> for Error {
 53    fn from(e: ConnectorError) -> Self {
 54        Error::Connection(e)
 55    }
 56}
 57
 58impl From<TlsError> for Error {
 59    fn from(e: TlsError) -> Self {
 60        Error::Tls(e)
 61    }
 62}
 63
 64#[cfg(all(feature = "tls-rust", not(feature = "tls-native")))]
 65impl From<InvalidDnsNameError> for Error {
 66    fn from(e: InvalidDnsNameError) -> Self {
 67        Error::DnsNameError(e)
 68    }
 69}
 70
 71/// XML parse error wrapper type
 72#[derive(Debug)]
 73pub struct ParseError(pub Cow<'static, str>);
 74
 75impl StdError for ParseError {
 76    fn description(&self) -> &str {
 77        self.0.as_ref()
 78    }
 79    fn cause(&self) -> Option<&dyn StdError> {
 80        None
 81    }
 82}
 83
 84impl fmt::Display for ParseError {
 85    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
 86        write!(f, "{}", self.0)
 87    }
 88}
 89
 90/// Error establishing connection
 91#[derive(Debug)]
 92pub enum ConnectorError {
 93    /// All attempts failed, no error available
 94    AllFailed,
 95    /// DNS protocol error
 96    Dns(ProtoError),
 97    /// DNS resolution error
 98    Resolve(ResolveError),
 99}
100
101impl StdError for ConnectorError {}
102
103impl std::fmt::Display for ConnectorError {
104    fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
105        write!(fmt, "{:?}", self)
106    }
107}