1 use super::bytes_errors::BytesReadError; 2 use super::bytes_errors::BytesWriteError; 3 use failure::{Backtrace, Fail}; 4 use std::fmt; 5 6 #[derive(Debug, Fail)] 7 pub enum BitErrorValue { 8 #[fail(display = "bytes read error\n")] 9 BytesReadError(BytesReadError), 10 #[fail(display = "bytes write error\n")] 11 BytesWriteError(BytesWriteError), 12 #[fail(display = "the size is bigger than 64\n")] 13 TooBig, 14 #[fail(display = "cannot write the whole 8 bits\n")] 15 CannotWrite8Bit, 16 #[fail(display = "cannot read byte\n")] 17 CannotReadByte, 18 } 19 #[derive(Debug)] 20 pub struct BitError { 21 pub value: BitErrorValue, 22 } 23 24 impl From<BitErrorValue> for BitError { 25 fn from(val: BitErrorValue) -> Self { 26 BitError { value: val } 27 } 28 } 29 30 impl From<BytesReadError> for BitError { 31 fn from(error: BytesReadError) -> Self { 32 BitError { 33 value: BitErrorValue::BytesReadError(error), 34 } 35 } 36 } 37 38 impl From<BytesWriteError> for BitError { 39 fn from(error: BytesWriteError) -> Self { 40 BitError { 41 value: BitErrorValue::BytesWriteError(error), 42 } 43 } 44 } 45 46 // impl From<Elapsed> for NetIOError { 47 // fn from(error: Elapsed) -> Self { 48 // NetIOError { 49 // value: NetIOErrorValue::TimeoutError(error), 50 // } 51 // } 52 // } 53 54 impl fmt::Display for BitError { 55 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 56 fmt::Display::fmt(&self.value, f) 57 } 58 } 59 60 impl Fail for BitError { 61 fn cause(&self) -> Option<&dyn Fail> { 62 self.value.cause() 63 } 64 65 fn backtrace(&self) -> Option<&Backtrace> { 66 self.value.backtrace() 67 } 68 } 69