From d29e3948d2b7df10602d7169b233cb8d59a5de23 Mon Sep 17 00:00:00 2001 From: Alexey Galakhov Date: Tue, 4 Apr 2017 13:18:57 +0200 Subject: [PATCH] Add Debug, Display and Error to HandshakeError Signed-off-by: Alexey Galakhov --- src/handshake/mod.rs | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/handshake/mod.rs b/src/handshake/mod.rs index 8546ca2..64d31f1 100644 --- a/src/handshake/mod.rs +++ b/src/handshake/mod.rs @@ -6,6 +6,8 @@ pub mod server; mod machine; +use std::error::Error as ErrorTrait; +use std::fmt; use std::io::{Read, Write}; use base64; @@ -62,6 +64,33 @@ pub enum HandshakeError { Failure(Error), } +impl fmt::Debug for HandshakeError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match *self { + HandshakeError::Interrupted(_) => write!(f, "HandshakeError::Interrupted(...)"), + HandshakeError::Failure(ref e) => write!(f, "HandshakeError::Failure({:?})", e), + } + } +} + +impl fmt::Display for HandshakeError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match *self { + HandshakeError::Interrupted(_) => write!(f, "Interrupted handshake (WouldBlock)"), + HandshakeError::Failure(ref e) => write!(f, "{}", e), + } + } +} + +impl ErrorTrait for HandshakeError { + fn description(&self) -> &str { + match *self { + HandshakeError::Interrupted(_) => "Interrupted handshake", + HandshakeError::Failure(ref e) => e.description(), + } + } +} + impl From for HandshakeError { fn from(err: Error) -> Self { HandshakeError::Failure(err)