1use crate::yamux::frame::FrameDecodeError;
12
13#[non_exhaustive]
15#[derive(Debug)]
16pub enum ConnectionError {
17 Io(std::io::Error),
19 Decode(FrameDecodeError),
21 NoMoreStreamIds,
23 Closed,
25 TooManyStreams,
27}
28
29impl PartialEq for ConnectionError {
30 fn eq(&self, other: &Self) -> bool {
31 match (self, other) {
32 (ConnectionError::Io(e1), ConnectionError::Io(e2)) => e1.kind() == e2.kind(),
33 (ConnectionError::Decode(e1), ConnectionError::Decode(e2)) => e1 == e2,
34 (ConnectionError::NoMoreStreamIds, ConnectionError::NoMoreStreamIds)
35 | (ConnectionError::Closed, ConnectionError::Closed)
36 | (ConnectionError::TooManyStreams, ConnectionError::TooManyStreams) => true,
37 _ => false,
38 }
39 }
40}
41
42impl std::fmt::Display for ConnectionError {
43 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
44 match self {
45 ConnectionError::Io(e) => write!(f, "i/o error: {}", e),
46 ConnectionError::Decode(e) => write!(f, "decode error: {}", e),
47 ConnectionError::NoMoreStreamIds =>
48 f.write_str("number of stream ids has been exhausted"),
49 ConnectionError::Closed => f.write_str("connection is closed"),
50 ConnectionError::TooManyStreams => f.write_str("maximum number of streams reached"),
51 }
52 }
53}
54
55impl std::error::Error for ConnectionError {
56 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
57 match self {
58 ConnectionError::Io(e) => Some(e),
59 ConnectionError::Decode(e) => Some(e),
60 ConnectionError::NoMoreStreamIds
61 | ConnectionError::Closed
62 | ConnectionError::TooManyStreams => None,
63 }
64 }
65}
66
67impl From<std::io::Error> for ConnectionError {
68 fn from(e: std::io::Error) -> Self {
69 ConnectionError::Io(e)
70 }
71}
72
73impl From<FrameDecodeError> for ConnectionError {
74 fn from(e: FrameDecodeError) -> Self {
75 ConnectionError::Decode(e)
76 }
77}
78
79impl From<futures::channel::mpsc::SendError> for ConnectionError {
80 fn from(_: futures::channel::mpsc::SendError) -> Self {
81 ConnectionError::Closed
82 }
83}
84
85impl From<futures::channel::oneshot::Canceled> for ConnectionError {
86 fn from(_: futures::channel::oneshot::Canceled) -> Self {
87 ConnectionError::Closed
88 }
89}