1 use crate::codec::SendError;
2 use crate::frame::{Reason, StreamId};
3 
4 use bytes::Bytes;
5 use std::fmt;
6 use std::io;
7 
8 /// Either an H2 reason  or an I/O error
9 #[derive(Clone, Debug)]
10 pub enum Error {
11     Reset(StreamId, Reason, Initiator),
12     GoAway(Bytes, Reason, Initiator),
13     Io(io::ErrorKind, Option<String>),
14 }
15 
16 #[derive(Clone, Copy, Debug, PartialEq)]
17 pub enum Initiator {
18     User,
19     Library,
20     Remote,
21 }
22 
23 impl Error {
is_local(&self) -> bool24     pub(crate) fn is_local(&self) -> bool {
25         match *self {
26             Self::Reset(_, _, initiator) | Self::GoAway(_, _, initiator) => initiator.is_local(),
27             Self::Io(..) => true,
28         }
29     }
30 
user_go_away(reason: Reason) -> Self31     pub(crate) fn user_go_away(reason: Reason) -> Self {
32         Self::GoAway(Bytes::new(), reason, Initiator::User)
33     }
34 
library_reset(stream_id: StreamId, reason: Reason) -> Self35     pub(crate) fn library_reset(stream_id: StreamId, reason: Reason) -> Self {
36         Self::Reset(stream_id, reason, Initiator::Library)
37     }
38 
library_go_away(reason: Reason) -> Self39     pub(crate) fn library_go_away(reason: Reason) -> Self {
40         Self::GoAway(Bytes::new(), reason, Initiator::Library)
41     }
42 
remote_reset(stream_id: StreamId, reason: Reason) -> Self43     pub(crate) fn remote_reset(stream_id: StreamId, reason: Reason) -> Self {
44         Self::Reset(stream_id, reason, Initiator::Remote)
45     }
46 
remote_go_away(debug_data: Bytes, reason: Reason) -> Self47     pub(crate) fn remote_go_away(debug_data: Bytes, reason: Reason) -> Self {
48         Self::GoAway(debug_data, reason, Initiator::Remote)
49     }
50 }
51 
52 impl Initiator {
is_local(&self) -> bool53     fn is_local(&self) -> bool {
54         match *self {
55             Self::User | Self::Library => true,
56             Self::Remote => false,
57         }
58     }
59 }
60 
61 impl fmt::Display for Error {
fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result62     fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
63         match *self {
64             Self::Reset(_, reason, _) | Self::GoAway(_, reason, _) => reason.fmt(fmt),
65             Self::Io(_, Some(ref inner)) => inner.fmt(fmt),
66             Self::Io(kind, None) => io::Error::from(kind).fmt(fmt),
67         }
68     }
69 }
70 
71 impl From<io::ErrorKind> for Error {
from(src: io::ErrorKind) -> Self72     fn from(src: io::ErrorKind) -> Self {
73         Error::Io(src.into(), None)
74     }
75 }
76 
77 impl From<io::Error> for Error {
from(src: io::Error) -> Self78     fn from(src: io::Error) -> Self {
79         Error::Io(src.kind(), src.get_ref().map(|inner| inner.to_string()))
80     }
81 }
82 
83 impl From<Error> for SendError {
from(src: Error) -> Self84     fn from(src: Error) -> Self {
85         Self::Connection(src)
86     }
87 }
88