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
143
144
145
146
147
148
149
extern crate capnp;

use std::fmt;

use std::error;
use std::result;
use std::io;
use std::string;
use std::sync::mpsc;

use ports::Msg;
use scheduler::CompMsg;

pub type Result<T> = result::Result<T, Error>;

#[derive(Debug)]
pub enum Error {
    Capnp(capnp::Error),
    CapnpNIS(capnp::NotInSchema),
    IO(io::Error),
    FromUtf8(string::FromUtf8Error),
    Mpsc(mpsc::RecvError),
    MpscTryRecv(mpsc::TryRecvError),
    Misc(String),
    MpscSend,
    AgentNotFound(String),
    OutputPortNotConnected(String, String),
    OutputNotConnected,
    ArrayOutputPortNotConnected(String, String, String),
    PortNotFound(String, String),
    PortDontExist(String),
    ElementNotFound(String, String, String),
    CannotRemove(String),
    BadMessageInfo,
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            Error::Capnp(ref err) => write!(f, "Cap'n Proto error: {}", err),
            Error::CapnpNIS(ref err) => write!(f, "Cap'n Proto error: {}", err),
            Error::IO(ref err) => write!(f, "IO error : {}", err),
            Error::FromUtf8(ref err) => write!(f, "From Utf8 error : {}", err),
            Error::Mpsc(ref err) => write!(f, "Mpsc error : {}", err),
            Error::MpscTryRecv(ref err) => write!(f, "Mpsc error : {}", err),
            Error::Misc(ref err) => write!(f, "Misc error : {}", err),
            Error::MpscSend => write!(f, "Mpsc error : cannot send"),
            Error::OutputPortNotConnected(ref c, ref p) => write!(f, "OutputSender : Port {} of agent {} is not connected", p, c),
            Error::OutputNotConnected => write!(f, "OutputSender : Port not connected"),
            Error::ArrayOutputPortNotConnected(ref c, ref p, ref s) => write!(f, "OutputSender : Element {} Port {} of agent {} is not connected", s, p, c),
            Error::AgentNotFound(ref c) => write!(f, "Scheduler error : agent {} is not found", c),
            Error::PortNotFound(ref c, ref p) => write!(f, "agent error : Port {} of agent {} is not found", p, c),
            Error::PortDontExist(ref p) => write!(f, "agent error : Port {} doesn't exist", p),
            Error::ElementNotFound(ref c, ref p, ref s) => write!(f, "agent error : Element {} on port {} of agent {} is not found", s, p, c),
            Error::CannotRemove(ref c) => write!(f, "Scheduler error : Cannot remove agent {}", c),
            Error::BadMessageInfo => write!(f, "Ports error : Bad message information"),
        }
    }
}

impl error::Error for Error {
    fn description(&self) -> &str {
        match *self {
            Error::Capnp(ref err) => err.description(),
            Error::CapnpNIS(ref err) => err.description(),
            Error::IO(ref err) => err.description(),
            Error::FromUtf8(ref err) => err.description(),
            Error::Mpsc(ref err) => err.description(),
            Error::MpscTryRecv(ref err) => err.description(),
            Error::Misc(ref err) => &err,
            Error::MpscSend => "Mpsc : cannot send",
            Error::OutputPortNotConnected(..) => "Output port not connected",
            Error::OutputNotConnected => "Output port not connected",
            Error::ArrayOutputPortNotConnected(..) => "Array Output port not connect",
            Error::AgentNotFound(..) => "Agent not found",
            Error::PortNotFound(..) => "Port not found",
            Error::PortDontExist(..) => "Port not found",
            Error::ElementNotFound(..) => "Element not found",
            Error::CannotRemove(..) => "Cannot remove agent",
            Error::BadMessageInfo => "Ports error : cannot receive the message, wrong bit information",
        }
    }

    fn cause(&self) -> Option<&error::Error> {
        match *self {
            Error::Capnp(ref err) => Some(err),
            Error::CapnpNIS(ref err) => Some(err),
            Error::IO(ref err) => Some(err),
            Error::FromUtf8(ref err) => Some(err),
            Error::Mpsc(ref err) => Some(err),
            Error::MpscTryRecv(ref err) => Some(err),
            _ => None
        }
    }
}

impl From<capnp::Error> for Error {
    fn from(err: capnp::Error) -> Error {
        Error::Capnp(err)
    }
}

impl From<capnp::NotInSchema> for Error {
    fn from(err: capnp::NotInSchema) -> Error {
        Error::CapnpNIS(err)
    }
}

impl From<String> for Error {
    fn from(err: String) -> Error {
        Error::Misc(err)
    }
}

impl From<io::Error> for Error {
    fn from(err: io::Error) -> Error {
        Error::IO(err)
    }
}

impl From<string::FromUtf8Error> for Error {
    fn from(err: string::FromUtf8Error) -> Error {
        Error::FromUtf8(err)
    }
}

impl From<mpsc::RecvError> for Error {
    fn from(err: mpsc::RecvError) -> Error {
        Error::Mpsc(err)
    }
}

impl From<mpsc::TryRecvError> for Error {
    fn from(err: mpsc::TryRecvError) -> Error {
        Error::MpscTryRecv(err)
    }
}

impl From<mpsc::SendError<CompMsg>> for Error {
    fn from(_: mpsc::SendError<CompMsg>) -> Error {
        Error::MpscSend
    }
}

impl From<mpsc::SendError<Msg>> for Error {
    fn from(_: mpsc::SendError<Msg>) -> Error {
        Error::MpscSend
    }
}