xref: /xiu/protocol/rtmp/src/handshake/errors.rs (revision ea7993e4)
1 use {
2     failure::{Backtrace, Fail},
3     bytesio::bytes_errors::{BytesReadError, BytesWriteError},
4     std::{fmt, io::Error, time::SystemTimeError},
5 };
6 
7 #[derive(Debug, Fail)]
8 pub enum HandshakeErrorValue {
9     #[fail(display = "bytes read error: {}\n", _0)]
10     BytesReadError(BytesReadError),
11     #[fail(display = "bytes write error: {}\n", _0)]
12     BytesWriteError(BytesWriteError),
13     #[fail(display = "system time error: {}\n", _0)]
14     SysTimeError(SystemTimeError),
15     #[fail(display = "Digest not found error\n")]
16     DigestNotFound,
17     #[fail(display = "s0 version not correct error\n")]
18     S0VersionNotCorrect,
19     #[fail(display = "io error\n")]
20     IOError(Error),
21 }
22 
23 impl From<Error> for HandshakeError {
24     fn from(error: Error) -> Self {
25         HandshakeError {
26             value: HandshakeErrorValue::IOError(error),
27         }
28     }
29 }
30 
31 #[derive(Debug)]
32 pub struct HandshakeError {
33     pub value: HandshakeErrorValue,
34 }
35 
36 impl From<HandshakeErrorValue> for HandshakeError {
37     fn from(val: HandshakeErrorValue) -> Self {
38         HandshakeError { value: val }
39     }
40 }
41 
42 impl From<BytesReadError> for HandshakeError {
43     fn from(error: BytesReadError) -> Self {
44         HandshakeError {
45             value: HandshakeErrorValue::BytesReadError(error),
46         }
47     }
48 }
49 
50 impl From<BytesWriteError> for HandshakeError {
51     fn from(error: BytesWriteError) -> Self {
52         HandshakeError {
53             value: HandshakeErrorValue::BytesWriteError(error),
54         }
55     }
56 }
57 
58 impl From<SystemTimeError> for HandshakeError {
59     fn from(error: SystemTimeError) -> Self {
60         HandshakeError {
61             value: HandshakeErrorValue::SysTimeError(error),
62         }
63     }
64 }
65 
66 impl fmt::Display for HandshakeError {
67     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
68         fmt::Display::fmt(&self.value, f)
69     }
70 }
71 
72 impl Fail for HandshakeError {
73     fn cause(&self) -> Option<&dyn Fail> {
74         self.value.cause()
75     }
76 
77     fn backtrace(&self) -> Option<&Backtrace> {
78         self.value.backtrace()
79     }
80 }
81