1 use super::bytesio_errors::BytesIOError; 2 use std::io; 3 // use tokio::time::Elapsed; 4 5 use failure::{Backtrace, Fail}; 6 use std::fmt; 7 8 #[derive(Debug, Fail)] 9 pub enum BytesReadErrorValue { 10 #[fail(display = "not enough bytes to read")] 11 NotEnoughBytes, 12 #[fail(display = "empty stream")] 13 EmptyStream, 14 #[fail(display = "io error: {}\n", _0)] 15 IO(#[cause] io::Error), 16 // #[fail(display = "elapsed: {}\n", _0)] 17 // TimeoutError(#[cause] Elapsed), 18 } 19 20 #[derive(Debug)] 21 pub struct BytesReadError { 22 pub value: BytesReadErrorValue, 23 } 24 25 impl From<BytesReadErrorValue> for BytesReadError { 26 fn from(val: BytesReadErrorValue) -> Self { 27 BytesReadError { value: val } 28 } 29 } 30 31 impl From<io::Error> for BytesReadError { 32 fn from(error: io::Error) -> Self { 33 BytesReadError { 34 value: BytesReadErrorValue::IO(error), 35 } 36 } 37 } 38 39 // impl From<Elapsed> for BytesReadError { 40 // fn from(error: Elapsed) -> Self { 41 // BytesReadError { 42 // value: BytesReadErrorValue::TimeoutError(error), 43 // } 44 // } 45 // } 46 47 #[derive(Debug)] 48 pub struct BytesWriteError { 49 pub value: BytesWriteErrorValue, 50 } 51 52 #[derive(Debug, Fail)] 53 pub enum BytesWriteErrorValue { 54 #[fail(display = "io error\n")] 55 IO(io::Error), 56 #[fail(display = "not enough bytes to write: {}\n", _0)] 57 BytesIOError(BytesIOError), 58 #[fail(display = "write time out")] 59 Timeout, 60 #[fail(display = "outof index")] 61 OutofIndex, 62 } 63 64 impl From<io::Error> for BytesWriteError { 65 fn from(error: io::Error) -> Self { 66 BytesWriteError { 67 value: BytesWriteErrorValue::IO(error), 68 } 69 } 70 } 71 72 impl From<BytesIOError> for BytesWriteError { 73 fn from(error: BytesIOError) -> Self { 74 BytesWriteError { 75 value: BytesWriteErrorValue::BytesIOError(error), 76 } 77 } 78 } 79 80 impl fmt::Display for BytesReadError { 81 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 82 fmt::Display::fmt(&self.value, f) 83 } 84 } 85 86 impl Fail for BytesReadError { 87 fn cause(&self) -> Option<&dyn Fail> { 88 self.value.cause() 89 } 90 91 fn backtrace(&self) -> Option<&Backtrace> { 92 self.value.backtrace() 93 } 94 } 95 96 impl fmt::Display for BytesWriteError { 97 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 98 fmt::Display::fmt(&self.value, f) 99 } 100 } 101 102 impl Fail for BytesWriteError { 103 fn cause(&self) -> Option<&dyn Fail> { 104 self.value.cause() 105 } 106 107 fn backtrace(&self) -> Option<&Backtrace> { 108 self.value.backtrace() 109 } 110 } 111