scuffle_amf0/
errors.rs

1use std::io;
2
3use super::define::Amf0Marker;
4
5/// Errors that can occur when decoding AMF0 data.
6#[derive(Debug, thiserror::Error)]
7pub enum Amf0ReadError {
8    /// An unknown marker was encountered.
9    #[error("unknown marker: {0}")]
10    UnknownMarker(u8),
11    /// An unsupported type was encountered.
12    #[error("unsupported type: {0:?}")]
13    UnsupportedType(Amf0Marker),
14    /// A string parse error occurred.
15    #[error("string parse error: {0}")]
16    StringParseError(#[from] std::str::Utf8Error),
17    /// An IO error occurred.
18    #[error("io error: {0}")]
19    Io(#[from] io::Error),
20    /// A wrong type was encountered. Created when using
21    /// `Amf0Decoder::next_with_type` and the next value is not the expected
22    /// type.
23    #[error("wrong type: expected {0:?}, got {1:?}")]
24    WrongType(Amf0Marker, Amf0Marker),
25}
26
27/// Errors that can occur when encoding AMF0 data.
28#[derive(Debug, thiserror::Error)]
29pub enum Amf0WriteError {
30    /// A normal string was too long.
31    #[error("normal string too long")]
32    NormalStringTooLong,
33    /// An IO error occurred.
34    #[error("io error: {0}")]
35    Io(#[from] io::Error),
36    /// An unsupported type was encountered.
37    #[error("unsupported type: {0:?}")]
38    UnsupportedType(Amf0Marker),
39}
40
41#[cfg(test)]
42#[cfg_attr(all(test, coverage_nightly), coverage(off))]
43mod tests {
44    use byteorder::ReadBytesExt;
45    use io::Cursor;
46
47    use super::*;
48
49    #[test]
50    fn test_read_error_display() {
51        let cases = [
52            (Amf0ReadError::UnknownMarker(100), "unknown marker: 100"),
53            (
54                Amf0ReadError::UnsupportedType(Amf0Marker::Reference),
55                "unsupported type: Reference",
56            ),
57            (
58                Amf0ReadError::WrongType(Amf0Marker::Reference, Amf0Marker::Boolean),
59                "wrong type: expected Reference, got Boolean",
60            ),
61            (
62                Amf0ReadError::StringParseError(
63                    #[allow(unknown_lints, invalid_from_utf8)]
64                    std::str::from_utf8(b"\xFF\xFF").unwrap_err(),
65                ),
66                "string parse error: invalid utf-8 sequence of 1 bytes from index 0",
67            ),
68            (
69                Amf0ReadError::Io(Cursor::new(Vec::<u8>::new()).read_u8().unwrap_err()),
70                "io error: failed to fill whole buffer",
71            ),
72        ];
73
74        for (err, expected) in cases {
75            assert_eq!(err.to_string(), expected);
76        }
77    }
78
79    #[test]
80    fn test_write_error_display() {
81        let cases = [
82            (
83                Amf0WriteError::UnsupportedType(Amf0Marker::ObjectEnd),
84                "unsupported type: ObjectEnd",
85            ),
86            (
87                Amf0WriteError::Io(Cursor::new(Vec::<u8>::new()).read_u8().unwrap_err()),
88                "io error: failed to fill whole buffer",
89            ),
90            (Amf0WriteError::NormalStringTooLong, "normal string too long"),
91        ];
92
93        for (err, expected) in cases {
94            assert_eq!(err.to_string(), expected);
95        }
96    }
97}