xref: /xiu/protocol/rtmp/src/amf0/errors.rs (revision e1aaa799)
1 use failure::Fail;
2 use std::{io, string};
3 
4 use liverust_lib::netio::{
5     errors::IOReadError,
6     reader::Reader,
7     writer::{IOWriteError, Writer},
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     IORead(IOReadError),
29 }
30 
31 pub struct Amf0ReadError {
32     pub value: Amf0ReadErrorValue,
33 }
34 
35 // Since an IO error can only be thrown while reading the buffer, auto-conversion should work
36 // impl From<io::Error> for Amf0ReadError {
37 //     fn from(error: io::Error) -> Self {
38 //         Amf0ReadError::BufferReadError(error)
39 //     }
40 // }
41 
42 impl From<string::FromUtf8Error> for Amf0ReadError {
43     fn from(error: string::FromUtf8Error) -> Self {
44         Amf0ReadError {
45             value: Amf0ReadErrorValue::StringParseError(error),
46         }
47     }
48 }
49 
50 impl From<IOReadError> for Amf0ReadError {
51     fn from(error: IOReadError) -> Self {
52         Amf0ReadError {
53             value: Amf0ReadErrorValue::IORead(error),
54         }
55     }
56 }
57 
58 // impl From<u8> for Amf0ReadError {
59 //     fn from(error: u8) -> Self {
60 //         Amf0ReadError {
61 //             value: Amf0ReadErrorValue::UnknownMarker(error),
62 //         }
63 //     }
64 // }
65 
66 /// Errors raised during to the serialization process
67 
68 pub enum Amf0WriteErrorValue {
69     NormalStringTooLong,
70 
71     BufferWriteError(io::Error),
72     IOWriteError(IOWriteError),
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<IOWriteError> for Amf0WriteError {
88     fn from(error: IOWriteError) -> Self {
89         Amf0WriteError {
90             value: Amf0WriteErrorValue::IOWriteError(error),
91         }
92     }
93 }
94