xref: /xiu/library/bytesio/src/bytesio_errors.rs (revision 06c50324)
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\n")]
13     IOError(io::Error),
14     #[fail(display = "time out error\n")]
15     TimeoutError,
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 {
25     fn from(val: BytesIOErrorValue) -> Self {
26         BytesIOError { value: val }
27     }
28 }
29 
30 impl From<io::Error> for BytesIOError {
31     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 {
47     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
48         fmt::Display::fmt(&self.value, f)
49     }
50 }
51 
52 impl Fail for BytesIOError {
53     fn cause(&self) -> Option<&dyn Fail> {
54         self.value.cause()
55     }
56 
57     fn backtrace(&self) -> Option<&Backtrace> {
58         self.value.backtrace()
59     }
60 }
61