1use std::error::Error;
2use std::fmt;
3use std::io::{self, ErrorKind};
4
5#[derive(Debug)]
6pub enum SocketError {
7 ConnectionClosed,
8 ConnectionReset,
9 ConnectionTimedOut,
10 InvalidAddress,
11 InvalidReply,
12 NotConnected,
13 Other(String),
14}
15
16impl Error for SocketError {
17 fn description(&self) -> &str {
18 use self::SocketError::*;
19 match *self {
20 ConnectionClosed => "The socket is closed",
21 ConnectionReset => "Connection reset by remote peer",
22 ConnectionTimedOut => "Connection timed out",
23 InvalidAddress => "Invalid address",
24 InvalidReply => "The remote peer sent an invalid reply",
25 NotConnected => "The socket is not connected",
26 Other(ref s) => s,
27 }
28 }
29}
30
31impl fmt::Display for SocketError {
32 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
33 write!(f, "{:?}", &self)
34 }
35}
36
37impl From<SocketError> for io::Error {
38 fn from(error: SocketError) -> io::Error {
39 use self::SocketError::*;
40 let kind = match error {
41 ConnectionClosed | NotConnected => ErrorKind::NotConnected,
42 ConnectionReset => ErrorKind::ConnectionReset,
43 ConnectionTimedOut => ErrorKind::TimedOut,
44 InvalidAddress => ErrorKind::InvalidInput,
45 InvalidReply => ErrorKind::ConnectionRefused,
46 Other(_) => ErrorKind::Other,
47 };
48 io::Error::new(kind, error.to_string())
49 }
50}
51
52#[derive(Debug)]
53pub enum ParseError {
54 InvalidExtensionLength,
55 InvalidPacketLength,
56 InvalidPacketType(u8),
57 UnsupportedVersion,
58}
59
60impl fmt::Display for ParseError {
61 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
62 write!(f, "{:?}", &self)
63 }
64}
65
66impl Error for ParseError {
67 fn description(&self) -> &str {
68 use self::ParseError::*;
69 match *self {
70 InvalidExtensionLength => "Invalid extension length (must be a non-zero multiple of 4)",
71 InvalidPacketLength => "The packet is too small",
72 InvalidPacketType(_) => "Invalid packet type",
73 UnsupportedVersion => "Unsupported packet version",
74 }
75 }
76}
77
78impl From<ParseError> for io::Error {
79 fn from(error: ParseError) -> io::Error {
80 io::Error::new(ErrorKind::Other, error.to_string())
81 }
82}