scuffle_amf0/
errors.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
use std::io;

use super::define::Amf0Marker;

/// Errors that can occur when decoding AMF0 data.
#[derive(Debug, thiserror::Error)]
pub enum Amf0ReadError {
    /// An unknown marker was encountered.
    #[error("unknown marker: {0}")]
    UnknownMarker(u8),
    /// An unsupported type was encountered.
    #[error("unsupported type: {0:?}")]
    UnsupportedType(Amf0Marker),
    /// A string parse error occurred.
    #[error("string parse error: {0}")]
    StringParseError(#[from] std::str::Utf8Error),
    /// An IO error occurred.
    #[error("io error: {0}")]
    Io(#[from] io::Error),
    /// A wrong type was encountered. Created when using
    /// `Amf0Decoder::next_with_type` and the next value is not the expected
    /// type.
    #[error("wrong type: expected {0:?}, got {1:?}")]
    WrongType(Amf0Marker, Amf0Marker),
}

/// Errors that can occur when encoding AMF0 data.
#[derive(Debug, thiserror::Error)]
pub enum Amf0WriteError {
    /// A normal string was too long.
    #[error("normal string too long")]
    NormalStringTooLong,
    /// An IO error occurred.
    #[error("io error: {0}")]
    Io(#[from] io::Error),
    /// An unsupported type was encountered.
    #[error("unsupported type: {0:?}")]
    UnsupportedType(Amf0Marker),
}

#[cfg(test)]
#[cfg_attr(all(test, coverage_nightly), coverage(off))]
mod tests {
    use byteorder::ReadBytesExt;
    use io::Cursor;

    use super::*;

    #[test]
    fn test_read_error_display() {
        let cases = [
            (Amf0ReadError::UnknownMarker(100), "unknown marker: 100"),
            (
                Amf0ReadError::UnsupportedType(Amf0Marker::Reference),
                "unsupported type: Reference",
            ),
            (
                Amf0ReadError::WrongType(Amf0Marker::Reference, Amf0Marker::Boolean),
                "wrong type: expected Reference, got Boolean",
            ),
            (
                Amf0ReadError::StringParseError(
                    #[allow(unknown_lints, invalid_from_utf8)]
                    std::str::from_utf8(b"\xFF\xFF").unwrap_err(),
                ),
                "string parse error: invalid utf-8 sequence of 1 bytes from index 0",
            ),
            (
                Amf0ReadError::Io(Cursor::new(Vec::<u8>::new()).read_u8().unwrap_err()),
                "io error: failed to fill whole buffer",
            ),
        ];

        for (err, expected) in cases {
            assert_eq!(err.to_string(), expected);
        }
    }

    #[test]
    fn test_write_error_display() {
        let cases = [
            (
                Amf0WriteError::UnsupportedType(Amf0Marker::ObjectEnd),
                "unsupported type: ObjectEnd",
            ),
            (
                Amf0WriteError::Io(Cursor::new(Vec::<u8>::new()).read_u8().unwrap_err()),
                "io error: failed to fill whole buffer",
            ),
            (Amf0WriteError::NormalStringTooLong, "normal string too long"),
        ];

        for (err, expected) in cases {
            assert_eq!(err.to_string(), expected);
        }
    }
}