xref: /xiu/protocol/rtmp/src/amf0/errors.rs (revision cbe12ea9)
1 use {
2     bytesio::bytes_errors::{BytesReadError, BytesWriteError},
3     failure::{Backtrace, Fail},
4     std::{
5         fmt, {io, string},
6     },
7 };
8 
9 #[derive(Debug, Fail)]
10 pub enum Amf0ReadErrorValue {
11     #[fail(display = "Encountered unknown marker: {}\n", marker)]
12     UnknownMarker { marker: u8 },
13     #[fail(display = "parser string error: {}\n", _0)]
14     StringParseError(#[cause] string::FromUtf8Error),
15     #[fail(display = "bytes read error :{}\n", _0)]
16     BytesReadError(BytesReadError),
17     #[fail(display = "wrong type")]
18     WrongType,
19 }
20 
21 #[derive(Debug)]
22 pub struct Amf0ReadError {
23     pub value: Amf0ReadErrorValue,
24 }
25 
26 impl From<string::FromUtf8Error> for Amf0ReadError {
27     fn from(error: string::FromUtf8Error) -> Self {
28         Amf0ReadError {
29             value: Amf0ReadErrorValue::StringParseError(error),
30         }
31     }
32 }
33 
34 impl From<BytesReadError> for Amf0ReadError {
35     fn from(error: BytesReadError) -> Self {
36         Amf0ReadError {
37             value: Amf0ReadErrorValue::BytesReadError(error),
38         }
39     }
40 }
41 
42 #[derive(Debug, Fail)]
43 pub enum Amf0WriteErrorValue {
44     #[fail(display = "normal string too long")]
45     NormalStringTooLong,
46     #[fail(display = "io error\n")]
47     BufferWriteError(io::Error),
48     #[fail(display = "bytes write error\n")]
49     BytesWriteError(BytesWriteError),
50 }
51 
52 #[derive(Debug)]
53 pub struct Amf0WriteError {
54     pub value: Amf0WriteErrorValue,
55 }
56 
57 impl From<io::Error> for Amf0WriteError {
58     fn from(error: io::Error) -> Self {
59         Amf0WriteError {
60             value: Amf0WriteErrorValue::BufferWriteError(error),
61         }
62     }
63 }
64 
65 impl From<BytesWriteError> for Amf0WriteError {
66     fn from(error: BytesWriteError) -> Self {
67         Amf0WriteError {
68             value: Amf0WriteErrorValue::BytesWriteError(error),
69         }
70     }
71 }
72 
73 impl fmt::Display for Amf0ReadError {
74     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
75         fmt::Display::fmt(&self.value, f)
76     }
77 }
78 
79 impl Fail for Amf0ReadError {
80     fn cause(&self) -> Option<&dyn Fail> {
81         self.value.cause()
82     }
83 
84     fn backtrace(&self) -> Option<&Backtrace> {
85         self.value.backtrace()
86     }
87 }
88 
89 impl fmt::Display for Amf0WriteError {
90     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
91         fmt::Display::fmt(&self.value, f)
92     }
93 }
94 
95 impl Fail for Amf0WriteError {
96     fn cause(&self) -> Option<&dyn Fail> {
97         self.value.cause()
98     }
99 
100     fn backtrace(&self) -> Option<&Backtrace> {
101         self.value.backtrace()
102     }
103 }
104