bytesio/
bytesio_errors.rs

1use failure::{Backtrace, Fail};
2use std::fmt;
3use std::io;
4// use tokio::time::Elapsed;
5
6#[derive(Debug, Fail)]
7pub enum BytesIOErrorValue {
8    #[fail(display = "not enough bytes")]
9    NotEnoughBytes,
10    #[fail(display = "empty stream")]
11    EmptyStream,
12    #[fail(display = "io error")]
13    IOError(io::Error),
14    #[fail(display = "time out error")]
15    TimeoutError(tokio::time::error::Elapsed),
16    #[fail(display = "none return")]
17    NoneReturn,
18}
19#[derive(Debug)]
20pub struct BytesIOError {
21    pub value: BytesIOErrorValue,
22}
23
24impl From<BytesIOErrorValue> for BytesIOError {
25    fn from(val: BytesIOErrorValue) -> Self {
26        BytesIOError { value: val }
27    }
28}
29
30impl 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
46impl fmt::Display for BytesIOError {
47    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
48        fmt::Display::fmt(&self.value, f)
49    }
50}
51
52impl 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}