From 8df6bdbeb0df95c49c8c313216deccc41b0ea752 Mon Sep 17 00:00:00 2001 From: Alexey Galakhov Date: Mon, 3 Apr 2017 16:43:17 +0200 Subject: [PATCH] Add Display to CloseFrame and CloseCode Signed-off-by: Alexey Galakhov --- src/protocol/frame/coding.rs | 17 +++++++++++++++-- src/protocol/frame/frame.rs | 16 ++++++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/src/protocol/frame/coding.rs b/src/protocol/frame/coding.rs index 3476c68..97f9fd0 100644 --- a/src/protocol/frame/coding.rs +++ b/src/protocol/frame/coding.rs @@ -193,9 +193,16 @@ impl CloseCode { } } -impl Into for CloseCode { +impl fmt::Display for CloseCode { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + let code: u16 = self.into(); + write!(f, "{}", code) + } +} + +impl<'t> Into for &'t CloseCode { fn into(self) -> u16 { - match self { + match *self { Normal => 1000, Away => 1001, Protocol => 1002, @@ -218,6 +225,12 @@ impl Into for CloseCode { } } +impl Into for CloseCode { + fn into(self) -> u16 { + (&self).into() + } +} + impl From for CloseCode { fn from(code: u16) -> CloseCode { match code { diff --git a/src/protocol/frame/frame.rs b/src/protocol/frame/frame.rs index 610a7ea..a22bf38 100644 --- a/src/protocol/frame/frame.rs +++ b/src/protocol/frame/frame.rs @@ -35,6 +35,22 @@ pub struct CloseFrame<'t> { pub reason: Cow<'t, str>, } +impl<'t> CloseFrame<'t> { + /// Convert into a owned string. + pub fn into_owned(self) -> CloseFrame<'static> { + CloseFrame { + code: self.code, + reason: self.reason.into_owned().into(), + } + } +} + +impl<'t> fmt::Display for CloseFrame<'t> { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "{} ({})", self.reason, self.code) + } +} + /// A struct representing a WebSocket frame. #[derive(Debug, Clone)] pub struct Frame {