scuffle_amf0/
error.rs

1//! AMF0 error type.
2
3use std::io;
4use std::num::TryFromIntError;
5use std::str::Utf8Error;
6
7use crate::Amf0Marker;
8
9/// Result type.
10pub type Result<T> = std::result::Result<T, Amf0Error>;
11
12/// AMF0 error.
13#[derive(thiserror::Error, Debug)]
14pub enum Amf0Error {
15    /// IO error.
16    #[error("io error: {0}")]
17    Io(#[from] io::Error),
18    /// Element (string or sequence) is too long.
19    #[error("element is too long: {0}")]
20    TooLong(#[from] TryFromIntError),
21    /// Cannot serialize sequence with unknown length.
22    #[error("cannot serialize sequence with unknown length")]
23    UnknownLength,
24    /// Cannot serialize map with non-string key.
25    #[error("cannot serialize map with non-string key")]
26    MapKeyNotString,
27    /// Unknown marker.
28    #[error("unknown marker: {0}")]
29    UnknownMarker(u8),
30    /// This marker cannot be deserialized.
31    #[error("this marker cannot be deserialized: {0:?}")]
32    UnsupportedMarker(Amf0Marker),
33    /// String parse error.
34    #[error("string parse error: {0}")]
35    StringParseError(#[from] Utf8Error),
36    /// Unexpected type.
37    #[error("unexpected type: expected one of {expected:?}, got {got:?}")]
38    UnexpectedType {
39        /// The expected types.
40        expected: &'static [Amf0Marker],
41        /// The actual type.
42        got: Amf0Marker,
43    },
44    /// Wrong array length.
45    #[error("wrong array length: expected {expected}, got {got}")]
46    WrongArrayLength {
47        /// The expected length.
48        expected: usize,
49        /// The actual length.
50        got: usize,
51    },
52    /// char deserialization is not supported.
53    #[error("char deserialization is not supported")]
54    CharNotSupported,
55    /// Custom error message.
56    #[cfg(feature = "serde")]
57    #[error("{0}")]
58    Custom(String),
59}
60
61#[cfg(feature = "serde")]
62impl serde::ser::Error for Amf0Error {
63    fn custom<T: std::fmt::Display>(msg: T) -> Self {
64        Amf0Error::Custom(msg.to_string())
65    }
66}
67
68#[cfg(feature = "serde")]
69impl serde::de::Error for Amf0Error {
70    fn custom<T: std::fmt::Display>(msg: T) -> Self {
71        Amf0Error::Custom(msg.to_string())
72    }
73}