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