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
use std::{error, fmt, io, result};

use crate::ws;

/// WebSockets Server Error
#[derive(Debug)]
pub enum Error {
	/// Io Error
	Io(io::Error),
	/// WebSockets Error
	WsError(ws::Error),
	/// Connection Closed
	ConnectionClosed,
}

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

impl fmt::Display for Error {
	fn fmt(&self, f: &mut fmt::Formatter) -> result::Result<(), fmt::Error> {
		match self {
			Error::ConnectionClosed => write!(f, "Action on closed connection."),
			Error::WsError(err) => write!(f, "WebSockets Error: {}", err),
			Error::Io(err) => write!(f, "Io Error: {}", err),
		}
	}
}

impl error::Error for Error {
	fn source(&self) -> Option<&(dyn error::Error + 'static)> {
		match self {
			Error::Io(io) => Some(io),
			Error::WsError(ws) => Some(ws),
			Error::ConnectionClosed => None,
		}
	}
}

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

impl From<ws::Error> for Error {
	fn from(err: ws::Error) -> Self {
		match err.kind {
			ws::ErrorKind::Io(err) => Error::Io(err),
			_ => Error::WsError(err),
		}
	}
}