xref: /xiu/library/bytesio/src/bytesio_errors.rs (revision 69de9bbd)
188325f54SHarlanC use failure::{Backtrace, Fail};
288325f54SHarlanC use std::fmt;
388325f54SHarlanC use std::io;
488325f54SHarlanC // use tokio::time::Elapsed;
588325f54SHarlanC 
688325f54SHarlanC #[derive(Debug, Fail)]
788325f54SHarlanC pub enum BytesIOErrorValue {
888325f54SHarlanC     #[fail(display = "not enough bytes")]
988325f54SHarlanC     NotEnoughBytes,
1088325f54SHarlanC     #[fail(display = "empty stream")]
1188325f54SHarlanC     EmptyStream,
12*69de9bbdSHarlanC     #[fail(display = "io error")]
1388325f54SHarlanC     IOError(io::Error),
14*69de9bbdSHarlanC     #[fail(display = "time out error")]
1546f4d48bSningnao     TimeoutError(tokio::time::error::Elapsed),
1688325f54SHarlanC     #[fail(display = "none return")]
1788325f54SHarlanC     NoneReturn,
1888325f54SHarlanC }
1988325f54SHarlanC #[derive(Debug)]
2088325f54SHarlanC pub struct BytesIOError {
2188325f54SHarlanC     pub value: BytesIOErrorValue,
2288325f54SHarlanC }
2388325f54SHarlanC 
2488325f54SHarlanC impl From<BytesIOErrorValue> for BytesIOError {
from(val: BytesIOErrorValue) -> Self2588325f54SHarlanC     fn from(val: BytesIOErrorValue) -> Self {
2688325f54SHarlanC         BytesIOError { value: val }
2788325f54SHarlanC     }
2888325f54SHarlanC }
2988325f54SHarlanC 
3088325f54SHarlanC impl From<io::Error> for BytesIOError {
from(error: io::Error) -> Self3188325f54SHarlanC     fn from(error: io::Error) -> Self {
3288325f54SHarlanC         BytesIOError {
3388325f54SHarlanC             value: BytesIOErrorValue::IOError(error),
3488325f54SHarlanC         }
3588325f54SHarlanC     }
3688325f54SHarlanC }
3788325f54SHarlanC 
3888325f54SHarlanC // impl From<Elapsed> for NetIOError {
3988325f54SHarlanC //     fn from(error: Elapsed) -> Self {
4088325f54SHarlanC //         NetIOError {
4188325f54SHarlanC //             value: NetIOErrorValue::TimeoutError(error),
4288325f54SHarlanC //         }
4388325f54SHarlanC //     }
4488325f54SHarlanC // }
4588325f54SHarlanC 
4688325f54SHarlanC impl fmt::Display for BytesIOError {
fmt(&self, f: &mut fmt::Formatter) -> fmt::Result4788325f54SHarlanC     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
4888325f54SHarlanC         fmt::Display::fmt(&self.value, f)
4988325f54SHarlanC     }
5088325f54SHarlanC }
5188325f54SHarlanC 
5288325f54SHarlanC impl Fail for BytesIOError {
cause(&self) -> Option<&dyn Fail>5388325f54SHarlanC     fn cause(&self) -> Option<&dyn Fail> {
5488325f54SHarlanC         self.value.cause()
5588325f54SHarlanC     }
5688325f54SHarlanC 
backtrace(&self) -> Option<&Backtrace>5788325f54SHarlanC     fn backtrace(&self) -> Option<&Backtrace> {
5888325f54SHarlanC         self.value.backtrace()
5988325f54SHarlanC     }
6088325f54SHarlanC }
61