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