Issue 128: Convert frame::Reason to struct (#142)
Alter frame::Reason to a struct with a single u32 member. Introduce Constants to the impl for existing Reasons. Change all usage in the library and its tests to adopt this change, using the new constants.
This commit is contained in:
		
				
					committed by
					
						 Carl Lerche
						Carl Lerche
					
				
			
			
				
	
			
			
			
						parent
						
							1c179f7bf2
						
					
				
				
					commit
					2aee78c7d7
				
			| @@ -1,99 +1,58 @@ | ||||
| use std::fmt; | ||||
|  | ||||
| #[derive(Debug, PartialEq, Eq, Clone, Copy)] | ||||
| pub enum Reason { | ||||
|     NoError, | ||||
|     ProtocolError, | ||||
|     InternalError, | ||||
|     FlowControlError, | ||||
|     SettingsTimeout, | ||||
|     StreamClosed, | ||||
|     FrameSizeError, | ||||
|     RefusedStream, | ||||
|     Cancel, | ||||
|     CompressionError, | ||||
|     ConnectError, | ||||
|     EnhanceYourCalm, | ||||
|     InadequateSecurity, | ||||
|     Http11Required, | ||||
|     Other(u32), | ||||
|     // TODO: reserve additional variants | ||||
| } | ||||
|  | ||||
| // ===== impl Reason ===== | ||||
| #[derive(Debug, PartialEq, Eq, Clone, Copy)] | ||||
| pub struct Reason(u32); | ||||
|  | ||||
| impl Reason { | ||||
|     pub fn description(&self) -> &str { | ||||
|         use self::Reason::*; | ||||
|     pub const NO_ERROR: Reason = Reason(0); | ||||
|     pub const PROTOCOL_ERROR: Reason = Reason(1); | ||||
|     pub const INTERNAL_ERROR: Reason = Reason(2); | ||||
|     pub const FLOW_CONTROL_ERROR: Reason = Reason(3); | ||||
|     pub const SETTINGS_TIMEOUT: Reason = Reason(4); | ||||
|     pub const STREAM_CLOSED: Reason = Reason(5); | ||||
|     pub const FRAME_SIZE_ERROR: Reason = Reason(6); | ||||
|     pub const REFUSED_STREAM: Reason = Reason(7); | ||||
|     pub const CANCEL: Reason = Reason(8); | ||||
|     pub const COMPRESSION_ERROR: Reason = Reason(9); | ||||
|     pub const CONNECT_ERROR: Reason = Reason(10); | ||||
|     pub const ENHANCE_YOUR_CALM: Reason = Reason(11); | ||||
|     pub const INADEQUATE_SECURITY: Reason = Reason(12); | ||||
|     pub const HTTP11_REQUIRED: Reason = Reason(13); | ||||
|  | ||||
|         match *self { | ||||
|             NoError => "not a result of an error", | ||||
|             ProtocolError => "unspecific protocol error detected", | ||||
|             InternalError => "unexpected internal error encountered", | ||||
|             FlowControlError => "flow-control protocol violated", | ||||
|             SettingsTimeout => "settings ACK not received in timely manner", | ||||
|             StreamClosed => "received frame when stream half-closed", | ||||
|             FrameSizeError => "frame with invalid size", | ||||
|             RefusedStream => "refused stream before processing any application logic", | ||||
|             Cancel => "stream no longer needed", | ||||
|             CompressionError => "unable to maintain the header compression context", | ||||
|             ConnectError => { | ||||
|                 "connection established in response to a CONNECT request \ | ||||
|                  was reset or abnormally closed" | ||||
|     pub fn description(&self) -> &str { | ||||
|         match self.0 { | ||||
|             0 => "not a result of an error", | ||||
|             1 => "unspecific protocol error detected", | ||||
|             2 => "unexpected internal error encountered", | ||||
|             3 => "flow-control protocol violated", | ||||
|             4 => "settings ACK not received in timely manner", | ||||
|             5 => "received frame when stream half-closed", | ||||
|             6 => "frame with invalid size", | ||||
|             7 => "refused stream before processing any application logic", | ||||
|             8 => "stream no longer needed", | ||||
|             9 => "unable to maintain the header compression context", | ||||
|             10 => { | ||||
|                 "connection established in response to a CONNECT request was reset or abnormally \ | ||||
|                  closed" | ||||
|             }, | ||||
|             EnhanceYourCalm => "detected excessive load generating behavior", | ||||
|             InadequateSecurity => "security properties do not meet minimum requirements", | ||||
|             Http11Required => "endpoint requires HTTP/1.1", | ||||
|             Other(_) => "other reason (ain't no tellin')", | ||||
|             11 => "detected excessive load generating behavior", | ||||
|             12 => "security properties do not meet minimum requirements", | ||||
|             13 => "endpoint requires HTTP/1.1", | ||||
|             _ => "unknown reason", | ||||
|         } | ||||
|     } | ||||
| } | ||||
|  | ||||
| impl From<u32> for Reason { | ||||
|     fn from(src: u32) -> Reason { | ||||
|         use self::Reason::*; | ||||
|  | ||||
|         match src { | ||||
|             0x0 => NoError, | ||||
|             0x1 => ProtocolError, | ||||
|             0x2 => InternalError, | ||||
|             0x3 => FlowControlError, | ||||
|             0x4 => SettingsTimeout, | ||||
|             0x5 => StreamClosed, | ||||
|             0x6 => FrameSizeError, | ||||
|             0x7 => RefusedStream, | ||||
|             0x8 => Cancel, | ||||
|             0x9 => CompressionError, | ||||
|             0xa => ConnectError, | ||||
|             0xb => EnhanceYourCalm, | ||||
|             0xc => InadequateSecurity, | ||||
|             0xd => Http11Required, | ||||
|             _ => Other(src), | ||||
|         } | ||||
|         Reason(src) | ||||
|     } | ||||
| } | ||||
|  | ||||
| impl From<Reason> for u32 { | ||||
|     fn from(src: Reason) -> u32 { | ||||
|         use self::Reason::*; | ||||
|  | ||||
|         match src { | ||||
|             NoError => 0x0, | ||||
|             ProtocolError => 0x1, | ||||
|             InternalError => 0x2, | ||||
|             FlowControlError => 0x3, | ||||
|             SettingsTimeout => 0x4, | ||||
|             StreamClosed => 0x5, | ||||
|             FrameSizeError => 0x6, | ||||
|             RefusedStream => 0x7, | ||||
|             Cancel => 0x8, | ||||
|             CompressionError => 0x9, | ||||
|             ConnectError => 0xa, | ||||
|             EnhanceYourCalm => 0xb, | ||||
|             InadequateSecurity => 0xc, | ||||
|             Http11Required => 0xd, | ||||
|             Other(v) => v, | ||||
|         } | ||||
|         src.0 | ||||
|     } | ||||
| } | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user