11use bytes:: Bytes ;
22
33use crate :: frame:: Close ;
4- use crate :: { ConnectionError , TransportErrorCode } ;
4+ use crate :: {
5+ ApplicationClose , ConnectionClose , ConnectionError , TransportError , TransportErrorCode ,
6+ } ;
57
68#[ allow( unreachable_pub) ] // fuzzing only
79#[ derive( Debug , Clone ) ]
@@ -27,7 +29,7 @@ impl State {
2729 }
2830 }
2931
30- pub ( super ) fn as_closed ( & self ) -> Option < & Close > {
32+ pub ( super ) fn as_closed ( & self ) -> Option < & CloseReason > {
3133 if let InnerState :: Closed {
3234 ref remote_reason, ..
3335 } = self . inner
@@ -118,7 +120,7 @@ impl State {
118120 }
119121 }
120122
121- pub ( super ) fn move_to_closed < R : Into < Close > > ( & mut self , reason : R ) {
123+ pub ( super ) fn move_to_closed < R : Into < CloseReason > > ( & mut self , reason : R ) {
122124 assert ! (
123125 matches!(
124126 self . inner,
@@ -134,7 +136,7 @@ impl State {
134136 } ;
135137 }
136138
137- pub ( super ) fn move_to_closed_local < R : Into < Close > > ( & mut self , reason : R ) {
139+ pub ( super ) fn move_to_closed_local < R : Into < CloseReason > > ( & mut self , reason : R ) {
138140 assert ! (
139141 matches!(
140142 self . inner,
@@ -225,13 +227,65 @@ pub(super) enum StateType {
225227 Drained ,
226228}
227229
230+ #[ derive( Debug , Clone ) ]
231+ pub ( super ) enum CloseReason {
232+ TransportError ( TransportError ) ,
233+ Connection ( ConnectionClose ) ,
234+ Application ( ApplicationClose ) ,
235+ }
236+
237+ impl From < TransportError > for CloseReason {
238+ fn from ( x : TransportError ) -> Self {
239+ Self :: TransportError ( x)
240+ }
241+ }
242+ impl From < ConnectionClose > for CloseReason {
243+ fn from ( x : ConnectionClose ) -> Self {
244+ Self :: Connection ( x)
245+ }
246+ }
247+ impl From < ApplicationClose > for CloseReason {
248+ fn from ( x : ApplicationClose ) -> Self {
249+ Self :: Application ( x)
250+ }
251+ }
252+
253+ impl From < Close > for CloseReason {
254+ fn from ( value : Close ) -> Self {
255+ match value {
256+ Close :: Application ( reason) => Self :: Application ( reason) ,
257+ Close :: Connection ( reason) => Self :: Connection ( reason) ,
258+ }
259+ }
260+ }
261+
262+ impl From < CloseReason > for ConnectionError {
263+ fn from ( value : CloseReason ) -> Self {
264+ match value {
265+ CloseReason :: TransportError ( err) => Self :: TransportError ( err) ,
266+ CloseReason :: Connection ( reason) => Self :: ConnectionClosed ( reason) ,
267+ CloseReason :: Application ( reason) => Self :: ApplicationClosed ( reason) ,
268+ }
269+ }
270+ }
271+
272+ impl From < CloseReason > for Close {
273+ fn from ( value : CloseReason ) -> Self {
274+ match value {
275+ CloseReason :: TransportError ( err) => Self :: Connection ( err. into ( ) ) ,
276+ CloseReason :: Connection ( reason) => Self :: Connection ( reason) ,
277+ CloseReason :: Application ( reason) => Self :: Application ( reason) ,
278+ }
279+ }
280+ }
281+
228282#[ derive( Debug , Clone ) ]
229283enum InnerState {
230284 Handshake ( Handshake ) ,
231285 Established ,
232286 Closed {
233287 /// The reason the remote closed the connection, or the reason we are sending to the remote.
234- remote_reason : Close ,
288+ remote_reason : CloseReason ,
235289 /// Set to true if we closed the connection locally
236290 is_local : bool ,
237291 /// Did we read this as error already?
0 commit comments