xref: /xiu/protocol/rtmp/src/handshake/errors.rs (revision 69de9bbd)
1 use {
2     bytesio::bytes_errors::{BytesReadError, BytesWriteError},
3     failure::{Backtrace, Fail},
4     std::{fmt, io::Error, time::SystemTimeError},
5 };
6 
7 #[derive(Debug, Fail)]
8 pub enum HandshakeErrorValue {
9     #[fail(display = "bytes read error: {}", _0)]
10     BytesReadError(BytesReadError),
11     #[fail(display = "bytes write error: {}", _0)]
12     BytesWriteError(BytesWriteError),
13     #[fail(display = "system time error: {}", _0)]
14     SysTimeError(SystemTimeError),
15     #[fail(display = "digest error: {}", _0)]
16     DigestError(DigestError),
17     #[fail(display = "Digest not found error")]
18     DigestNotFound,
19     #[fail(display = "s0 version not correct error")]
20     S0VersionNotCorrect,
21     #[fail(display = "io error")]
22     IOError(Error),
23 }
24 
25 impl From<Error> for HandshakeError {
from(error: Error) -> Self26     fn from(error: Error) -> Self {
27         HandshakeError {
28             value: HandshakeErrorValue::IOError(error),
29         }
30     }
31 }
32 
33 #[derive(Debug)]
34 pub struct HandshakeError {
35     pub value: HandshakeErrorValue,
36 }
37 
38 impl From<HandshakeErrorValue> for HandshakeError {
from(val: HandshakeErrorValue) -> Self39     fn from(val: HandshakeErrorValue) -> Self {
40         HandshakeError { value: val }
41     }
42 }
43 
44 impl From<BytesReadError> for HandshakeError {
from(error: BytesReadError) -> Self45     fn from(error: BytesReadError) -> Self {
46         HandshakeError {
47             value: HandshakeErrorValue::BytesReadError(error),
48         }
49     }
50 }
51 
52 impl From<BytesWriteError> for HandshakeError {
from(error: BytesWriteError) -> Self53     fn from(error: BytesWriteError) -> Self {
54         HandshakeError {
55             value: HandshakeErrorValue::BytesWriteError(error),
56         }
57     }
58 }
59 
60 impl From<SystemTimeError> for HandshakeError {
from(error: SystemTimeError) -> Self61     fn from(error: SystemTimeError) -> Self {
62         HandshakeError {
63             value: HandshakeErrorValue::SysTimeError(error),
64         }
65     }
66 }
67 
68 impl From<DigestError> for HandshakeError {
from(error: DigestError) -> Self69     fn from(error: DigestError) -> Self {
70         HandshakeError {
71             value: HandshakeErrorValue::DigestError(error),
72         }
73     }
74 }
75 
76 impl fmt::Display for HandshakeError {
fmt(&self, f: &mut fmt::Formatter) -> fmt::Result77     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
78         fmt::Display::fmt(&self.value, f)
79     }
80 }
81 
82 impl Fail for HandshakeError {
cause(&self) -> Option<&dyn Fail>83     fn cause(&self) -> Option<&dyn Fail> {
84         self.value.cause()
85     }
86 
backtrace(&self) -> Option<&Backtrace>87     fn backtrace(&self) -> Option<&Backtrace> {
88         self.value.backtrace()
89     }
90 }
91 
92 #[derive(Debug)]
93 pub struct DigestError {
94     pub value: DigestErrorValue,
95 }
96 
97 #[derive(Debug, Fail)]
98 pub enum DigestErrorValue {
99     #[fail(display = "bytes read error: {}", _0)]
100     BytesReadError(BytesReadError),
101     #[fail(display = "digest length not correct")]
102     DigestLengthNotCorrect,
103     #[fail(display = "cannot generate digest")]
104     CannotGenerate,
105     #[fail(display = "unknow schema")]
106     UnknowSchema,
107 }
108 
109 impl From<BytesReadError> for DigestError {
from(error: BytesReadError) -> Self110     fn from(error: BytesReadError) -> Self {
111         DigestError {
112             value: DigestErrorValue::BytesReadError(error),
113         }
114     }
115 }
116 
117 impl fmt::Display for DigestError {
fmt(&self, f: &mut fmt::Formatter) -> fmt::Result118     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
119         fmt::Display::fmt(&self.value, f)
120     }
121 }
122 
123 impl Fail for DigestError {
cause(&self) -> Option<&dyn Fail>124     fn cause(&self) -> Option<&dyn Fail> {
125         self.value.cause()
126     }
127 
backtrace(&self) -> Option<&Backtrace>128     fn backtrace(&self) -> Option<&Backtrace> {
129         self.value.backtrace()
130     }
131 }
132