From c6a233281a2123969d27591164c5603a47eb798d Mon Sep 17 00:00:00 2001 From: Sean McArthur Date: Tue, 10 Oct 2017 14:30:34 -0700 Subject: [PATCH] custom Debug for Reason (#151) --- src/frame/reason.rs | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/src/frame/reason.rs b/src/frame/reason.rs index a3660c5..6271026 100644 --- a/src/frame/reason.rs +++ b/src/frame/reason.rs @@ -1,7 +1,7 @@ use std::fmt; -#[derive(Debug, PartialEq, Eq, Clone, Copy)] +#[derive(PartialEq, Eq, Clone, Copy)] pub struct Reason(u32); impl Reason { @@ -56,6 +56,39 @@ impl From for u32 { } } +impl fmt::Debug for Reason { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + let name = match self.0 { + 0 => "NO_ERROR", + 1 => "PROTOCOL_ERROR", + 2 => "INTERNAL_ERROR", + 3 => "FLOW_CONTROL_ERROR", + 4 => "SETTINGS_TIMEOUT", + 5 => "STREAM_CLOSED", + 6 => "FRAME_SIZE_ERROR", + 7 => "REFUSED_STREAM", + 8 => "CANCEL", + 9 => "COMPRESSION_ERROR", + 10 => "CONNECT_ERROR", + 11 => "ENHANCE_YOUR_CALM", + 12 => "INADEQUATE_SECURITY", + 13 => "HTTP_1_1_REQUIRED", + other => return f.debug_tuple("Reason") + .field(&Hex(other)) + .finish(), + }; + f.write_str(name) + } +} + +struct Hex(u32); + +impl fmt::Debug for Hex { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + fmt::LowerHex::fmt(&self.0, f) + } +} + impl fmt::Display for Reason { fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { write!(fmt, "{}", self.description())