1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
use crate::types::{
    flags::*,
    generation::*,
    parsing::{traits::ParsableInput, *},
    *,
};
use nom::{
    combinator::{flat_map, map, map_opt},
    error::context,
};
use std::{convert::TryFrom, error, fmt, io::Write};

include!(concat!(env!("OUT_DIR"), "/protocol.rs"));

/// Type alias for AMQP BasicProperties
pub type BasicProperties = basic::AMQPProperties;

/// An AMQP Error
#[derive(Clone, Debug, PartialEq)]
pub struct AMQPError {
    kind: AMQPErrorKind,
    message: ShortString,
}

impl AMQPError {
    /// Create a new error
    pub fn new(kind: AMQPErrorKind, message: ShortString) -> Self {
        Self { kind, message }
    }

    /// Get the error corresponding to an id
    pub fn from_id(id: ShortUInt, message: ShortString) -> Option<Self> {
        AMQPErrorKind::from_id(id).map(|kind| Self { kind, message })
    }

    /// Get the kind of error
    pub fn kind(&self) -> &AMQPErrorKind {
        &self.kind
    }

    /// Get the id of the error
    pub fn get_id(&self) -> ShortUInt {
        self.kind.get_id()
    }

    /// Get the message of the error
    pub fn get_message(&self) -> &ShortString {
        &self.message
    }
}

impl TryFrom<channel::Close> for AMQPError {
    type Error = String;

    fn try_from(method: channel::Close) -> Result<Self, Self::Error> {
        Self::from_id(method.reply_code, method.reply_text.clone())
            .ok_or_else(|| format!("Couldn't convert method to error: {:?}", method))
    }
}

impl TryFrom<connection::Close> for AMQPError {
    type Error = String;

    fn try_from(method: connection::Close) -> Result<Self, Self::Error> {
        Self::from_id(method.reply_code, method.reply_text.clone())
            .ok_or_else(|| format!("Couldn't convert method to error: {:?}", method))
    }
}

impl fmt::Display for AMQPError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}: {}", self.kind, self.message)
    }
}

impl error::Error for AMQPError {}

/// The kind of AMQP Error
#[derive(Clone, Debug, PartialEq)]
pub enum AMQPErrorKind {
    /// A soft AMQP error
    Soft(AMQPSoftError),
    /// A hard AMQP error
    Hard(AMQPHardError),
}

impl AMQPErrorKind {
    /// Get the id of the error
    pub fn get_id(&self) -> ShortUInt {
        match *self {
            AMQPErrorKind::Soft(ref s) => s.get_id(),
            AMQPErrorKind::Hard(ref h) => h.get_id(),
        }
    }

    /// Get the error kind corresponding to an id
    pub fn from_id(id: ShortUInt) -> Option<Self> {
        AMQPSoftError::from_id(id)
            .map(AMQPErrorKind::Soft)
            .or_else(|| AMQPHardError::from_id(id).map(AMQPErrorKind::Hard))
    }
}

impl fmt::Display for AMQPErrorKind {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            AMQPErrorKind::Soft(err) => write!(f, "AMQP soft error: {}", err),
            AMQPErrorKind::Hard(err) => write!(f, "AMQP hard error: {}", err),
        }
    }
}

impl From<AMQPSoftError> for AMQPErrorKind {
    fn from(error: AMQPSoftError) -> Self {
        Self::Soft(error)
    }
}

impl From<AMQPHardError> for AMQPErrorKind {
    fn from(error: AMQPHardError) -> Self {
        Self::Hard(error)
    }
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn test_description() {
        assert_eq!(
            format!(
                "{} - {}.{}.{}",
                metadata::NAME,
                metadata::MAJOR_VERSION,
                metadata::MINOR_VERSION,
                metadata::REVISION
            ),
            "AMQP - 0.9.1"
        );
    }
}