xref: /xiu/protocol/rtmp/src/netconnection/errors.rs (revision cbe12ea9)
1 use {
2     crate::{
3         amf0::errors::{Amf0ReadError, Amf0WriteError},
4         chunk::errors::PackError,
5     },
6     failure::{Backtrace, Fail},
7     std::fmt,
8 };
9 
10 #[derive(Debug)]
11 pub struct NetConnectionError {
12     pub value: NetConnectionErrorValue,
13 }
14 #[derive(Debug, Fail)]
15 pub enum NetConnectionErrorValue {
16     #[fail(display = "amf0 write error: {}\n", _0)]
17     Amf0WriteError(Amf0WriteError),
18     #[fail(display = "amf0 read error: {}\n", _0)]
19     Amf0ReadError(Amf0ReadError),
20     #[fail(display = "pack error\n")]
21     PackError(PackError),
22 }
23 
24 impl From<Amf0WriteError> for NetConnectionError {
25     fn from(error: Amf0WriteError) -> Self {
26         NetConnectionError {
27             value: NetConnectionErrorValue::Amf0WriteError(error),
28         }
29     }
30 }
31 
32 impl From<Amf0ReadError> for NetConnectionError {
33     fn from(error: Amf0ReadError) -> Self {
34         NetConnectionError {
35             value: NetConnectionErrorValue::Amf0ReadError(error),
36         }
37     }
38 }
39 
40 impl From<PackError> for NetConnectionError {
41     fn from(error: PackError) -> Self {
42         NetConnectionError {
43             value: NetConnectionErrorValue::PackError(error),
44         }
45     }
46 }
47 
48 impl fmt::Display for NetConnectionError {
49     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
50         fmt::Display::fmt(&self.value, f)
51     }
52 }
53 
54 impl Fail for NetConnectionError {
55     fn cause(&self) -> Option<&dyn Fail> {
56         self.value.cause()
57     }
58 
59     fn backtrace(&self) -> Option<&Backtrace> {
60         self.value.backtrace()
61     }
62 }
63