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