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