amqp 0.0.18

AMQP/RabbitMQ protocol client
Documentation
use std::convert::From;
use std::{io, error, fmt};
use url;

#[cfg(feature = "tls")]
use openssl;

#[derive(Debug, Clone)]
pub enum AMQPError {
    IoError(io::ErrorKind),
    DecodeError(&'static str),
    Protocol(String),
    SchemeError(String),
    UrlParseError(url::ParseError),
    QueueEmpty,
    SyncError,
    FramingError(String),
    VHostError,
    #[cfg(feature = "tls")]
    TLSHandshakeError,
    #[cfg(feature = "tls")]
    TLSErrorStack
}

impl fmt::Display for AMQPError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "AMQP Error: {}", error::Error::description(self))
    }
}

impl error::Error for AMQPError {
    fn description<'a>(&'a self) -> &'a str {
        match *self {
            AMQPError::IoError(_) => "IoError",
            AMQPError::DecodeError(err) => err,
            AMQPError::Protocol(ref err) => err,
            AMQPError::SchemeError(ref err) => err,
            AMQPError::UrlParseError(_) => "URL parsing error",
            AMQPError::QueueEmpty => "Queue is empty",
            AMQPError::SyncError => "Synchronisation error",
            AMQPError::FramingError(ref err) => err,
            AMQPError::VHostError => "Access to vhost is denied for a current user",
            #[cfg(feature = "tls")]
            AMQPError::TLSHandshakeError => "OpenSSL handshake error",
            #[cfg(feature = "tls")]
            AMQPError::TLSErrorStack => "OpenSSL ErrorStack error"
        }
    }
}

pub type AMQPResult<T> = Result<T, AMQPError>;

impl From<io::Error> for AMQPError {
    fn from(err: io::Error) -> AMQPError {
        AMQPError::IoError(err.kind())
    }
}

impl <T> From<::std::sync::PoisonError<T>> for AMQPError {
    fn from(_err: ::std::sync::PoisonError<T>) -> AMQPError {
        AMQPError::SyncError
    }
}

#[cfg(feature = "tls")]
impl From<openssl::ssl::HandshakeError<::std::net::TcpStream>> for AMQPError {
    fn from(_err: openssl::ssl::HandshakeError<::std::net::TcpStream>) -> AMQPError {
        AMQPError::TLSHandshakeError
    }
}

#[cfg(feature = "tls")]
impl From<openssl::error::ErrorStack> for AMQPError {
    fn from(_err: openssl::error::ErrorStack) -> AMQPError {
        AMQPError::TLSErrorStack
    }
}


impl From<url::ParseError> for AMQPError {
    fn from(err: url::ParseError) -> AMQPError {
        AMQPError::UrlParseError(err)
    }
}

#[cfg(feature = "tls")]
impl From<openssl::ssl::error::Error> for AMQPError {
    fn from(err: openssl::ssl::error::Error) -> AMQPError {
        AMQPError::Protocol(format!("{}", err))
    }
}