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
use std::error::Error as StdError; use std::fmt; use std::io; pub const ERR_INVALID_SETUP: u32 = 0x0000_0001; pub const ERR_UNSUPPORTED_SETUP: u32 = 0x0000_0002; pub const ERR_REJECT_SETUP: u32 = 0x0000_0003; pub const ERR_REJECT_RESUME: u32 = 0x0000_0004; pub const ERR_CONN_FAILED: u32 = 0x0000_0101; pub const ERR_CONN_CLOSED: u32 = 0x0000_0102; pub const ERR_APPLICATION: u32 = 0x0000_0201; pub const ERR_REJECTED: u32 = 0x0000_0202; pub const ERR_CANCELED: u32 = 0x0000_0203; pub const ERR_INVALID: u32 = 0x0000_0204; #[derive(Debug)] pub enum ErrorKind { Internal(u32, String), WithDescription(String), IO(io::Error), Cancelled(), } #[derive(Debug)] pub struct RSocketError { kind: ErrorKind, } impl StdError for RSocketError {} impl fmt::Display for RSocketError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match &self.kind { ErrorKind::Internal(c, s) => write!(f, "ERROR({}): {}", translate(c), s), ErrorKind::WithDescription(s) => write!(f, "{}", s), ErrorKind::IO(e) => write!(f, "{}", e), ErrorKind::Cancelled() => write!(f, "ERROR(CANCELLED)"), } } } impl From<io::Error> for RSocketError { fn from(e: io::Error) -> RSocketError { RSocketError { kind: ErrorKind::IO(e), } } } impl From<ErrorKind> for RSocketError { fn from(kind: ErrorKind) -> RSocketError { RSocketError { kind } } } impl From<String> for RSocketError { fn from(e: String) -> RSocketError { RSocketError { kind: ErrorKind::WithDescription(e), } } } impl From<&'static str> for RSocketError { fn from(e: &'static str) -> RSocketError { RSocketError { kind: ErrorKind::WithDescription(String::from(e)), } } } #[inline] fn translate(code: &u32) -> &str { match *code { ERR_APPLICATION => "APPLICATION", ERR_INVALID_SETUP => "INVALID_SETUP", ERR_UNSUPPORTED_SETUP => "UNSUPPORTED_SETUP", ERR_REJECT_SETUP => "REJECT_SETUP", ERR_REJECT_RESUME => "REJECT_RESUME", ERR_CONN_FAILED => "CONN_FAILED", ERR_CONN_CLOSED => "CONN_CLOSED", ERR_REJECTED => "REJECTED", ERR_CANCELED => "CANCELED", ERR_INVALID => "INVALID", _ => "UNKNOWN", } }