xref: /xiu/protocol/rtmp/src/amf0/errors.rs (revision 4ff0d3ac)
1 use failure::Fail;
2 use std::{io, string};
3 
4 use liverust_lib::netio::{
5     bytes_errors::{BytesReadError, BytesWriteError},
6     bytes_reader::BytesReader,
7     bytes_writer::BytesWriter,
8 };
9 
10 //#[derive(Debug, Fail)]
11 pub enum Amf0ReadErrorValue {
12     //#[fail(display = "Encountered unknown marker: {}", marker)]
13     UnknownMarker { marker: u8 },
14 
15     //#[fail(display = "Unexpected empty object property name")]
16     UnexpectedEmptyObjectPropertyName,
17 
18     //#[fail(display = "Hit end of the byte buffer but was expecting more data")]
19     UnexpectedEof,
20 
21     //#[fail(display = "Failed to read byte buffer: {}", _0)]
22     //BufferReadError(#[cause] io::Error),
23 
24     //#[fail(display = "Failed to read a utf8 string from the byte buffer: {}", _0)]
25     StringParseError(string::FromUtf8Error),
26 
27     //#[fail(display = "Failed to read a utf8 string from the byte buffer: {}", _0)]
28     BytesReadError(BytesReadError),
29     WrongType,
30 }
31 
32 pub struct Amf0ReadError {
33     pub value: Amf0ReadErrorValue,
34 }
35 
36 // Since an IO error can only be thrown while reading the buffer, auto-conversion should work
37 // impl From<io::Error> for Amf0ReadError {
38 //     fn from(error: io::Error) -> Self {
39 //         Amf0ReadError::BufferReadError(error)
40 //     }
41 // }
42 
43 impl From<string::FromUtf8Error> for Amf0ReadError {
44     fn from(error: string::FromUtf8Error) -> Self {
45         Amf0ReadError {
46             value: Amf0ReadErrorValue::StringParseError(error),
47         }
48     }
49 }
50 
51 impl From<BytesReadError> for Amf0ReadError {
52     fn from(error: BytesReadError) -> Self {
53         Amf0ReadError {
54             value: Amf0ReadErrorValue::BytesReadError(error),
55         }
56     }
57 }
58 
59 // impl From<u8> for Amf0ReadError {
60 //     fn from(error: u8) -> Self {
61 //         Amf0ReadError {
62 //             value: Amf0ReadErrorValue::UnknownMarker(error),
63 //         }
64 //     }
65 // }
66 
67 /// Errors raised during to the serialization process
68 
69 pub enum Amf0WriteErrorValue {
70     NormalStringTooLong,
71     BufferWriteError(io::Error),
72     BytesWriteError(BytesWriteError),
73 }
74 
75 pub struct Amf0WriteError {
76     pub value: Amf0WriteErrorValue,
77 }
78 
79 impl From<io::Error> for Amf0WriteError {
80     fn from(error: io::Error) -> Self {
81         Amf0WriteError {
82             value: Amf0WriteErrorValue::BufferWriteError(error),
83         }
84     }
85 }
86 
87 impl From<BytesWriteError> for Amf0WriteError {
88     fn from(error: BytesWriteError) -> Self {
89         Amf0WriteError {
90             value: Amf0WriteErrorValue::BytesWriteError(error),
91         }
92     }
93 }
94