1 use failure::{Backtrace, Fail}; 2 use std::fmt; 3 use std::io; 4 // use tokio::time::Elapsed; 5 6 #[derive(Debug, Fail)] 7 pub enum BytesIOErrorValue { 8 #[fail(display = "not enough bytes")] 9 NotEnoughBytes, 10 #[fail(display = "empty stream")] 11 EmptyStream, 12 #[fail(display = "io error")] 13 IOError(io::Error), 14 #[fail(display = "time out error")] 15 TimeoutError(tokio::time::error::Elapsed), 16 #[fail(display = "none return")] 17 NoneReturn, 18 } 19 #[derive(Debug)] 20 pub struct BytesIOError { 21 pub value: BytesIOErrorValue, 22 } 23 24 impl From<BytesIOErrorValue> for BytesIOError { from(val: BytesIOErrorValue) -> Self25 fn from(val: BytesIOErrorValue) -> Self { 26 BytesIOError { value: val } 27 } 28 } 29 30 impl From<io::Error> for BytesIOError { from(error: io::Error) -> Self31 fn from(error: io::Error) -> Self { 32 BytesIOError { 33 value: BytesIOErrorValue::IOError(error), 34 } 35 } 36 } 37 38 // impl From<Elapsed> for NetIOError { 39 // fn from(error: Elapsed) -> Self { 40 // NetIOError { 41 // value: NetIOErrorValue::TimeoutError(error), 42 // } 43 // } 44 // } 45 46 impl fmt::Display for BytesIOError { fmt(&self, f: &mut fmt::Formatter) -> fmt::Result47 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 48 fmt::Display::fmt(&self.value, f) 49 } 50 } 51 52 impl Fail for BytesIOError { cause(&self) -> Option<&dyn Fail>53 fn cause(&self) -> Option<&dyn Fail> { 54 self.value.cause() 55 } 56 backtrace(&self) -> Option<&Backtrace>57 fn backtrace(&self) -> Option<&Backtrace> { 58 self.value.backtrace() 59 } 60 } 61