xref: /xiu/protocol/rtmp/src/amf0/errors.rs (revision fc2156a4)
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     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<IOReadError> for Amf0ReadError {
52     fn from(error: IOReadError) -> Self {
53         Amf0ReadError {
54             value: Amf0ReadErrorValue::IORead(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 
72     BufferWriteError(io::Error),
73     IOWriteError(IOWriteError),
74 }
75 
76 pub struct Amf0WriteError {
77     pub value: Amf0WriteErrorValue,
78 }
79 
80 impl From<io::Error> for Amf0WriteError {
81     fn from(error: io::Error) -> Self {
82         Amf0WriteError {
83             value: Amf0WriteErrorValue::BufferWriteError(error),
84         }
85     }
86 }
87 
88 impl From<IOWriteError> for Amf0WriteError {
89     fn from(error: IOWriteError) -> Self {
90         Amf0WriteError {
91             value: Amf0WriteErrorValue::IOWriteError(error),
92         }
93     }
94 }
95