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
//! Module holding various error wrappers

use std::io;

use std::sync::mpsc as channel_mpsc;

#[cfg(feature = "stream")]
use futures::sync::mpsc as stream_mpsc;

use std::fmt;
use std::error::Error;
use async::SendData;

#[derive(Debug)]
pub enum ChaseError {
    IoError(io::Error),
    ChannelSendError(channel_mpsc::SendError<SendData>),
    #[cfg(feature = "stream")] StreamSendError(stream_mpsc::SendError<SendData>),
    Custom(Box<Error + Send + Sync>),
}

impl fmt::Display for ChaseError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        use self::ChaseError::*;
        match self {
            &IoError(ref e) => write!(f, "{}", e),
            &ChannelSendError(ref e) => write!(f, "{}", e),
            #[cfg(feature = "stream")]
            &StreamSendError(ref e) => write!(f, "{}", e),
            &Custom(ref e) => e.fmt(f),
        }
    }
}

impl Error for ChaseError {
    fn description(&self) -> &str {
        use self::ChaseError::*;
        match self {
            &IoError(ref e) => e.description(),
            &ChannelSendError(ref e) => e.description(),
            #[cfg(feature = "stream")]
            &StreamSendError(ref e) => e.description(),
            &Custom(ref e) => e.description(),
        }
    }

    fn cause(&self) -> Option<&Error> {
        use self::ChaseError::*;
        match self {
            &IoError(ref e) => Some(e),
            &ChannelSendError(ref e) => Some(e),
            #[cfg(feature = "stream")]
            &StreamSendError(ref e) => Some(e),
            &Custom(ref e) => e.cause(),
        }
    }
}

impl From<io::Error> for ChaseError {
    fn from(e: io::Error) -> Self {
        ChaseError::IoError(e)
    }
}

impl From<channel_mpsc::SendError<SendData>> for ChaseError {
    fn from(e: channel_mpsc::SendError<SendData>) -> Self {
        ChaseError::ChannelSendError(e)
    }
}

#[cfg(feature = "stream")]
impl From<stream_mpsc::SendError<SendData>> for ChaseError {
    fn from(e: stream_mpsc::SendError<SendData>) -> Self {
        ChaseError::StreamSendError(e)
    }
}