Skip to main content

flash_lso/
errors.rs

1use nom::error::{ErrorKind, FromExternalError, ParseError};
2use thiserror::Error;
3
4/// Enum for representing decoding errors
5#[derive(Error, Debug, Clone, Eq, PartialEq)]
6#[allow(variant_size_differences)] /* Allow the Nom variant to be large */
7pub enum Error<'a> {
8    /// Out of bounds decoding
9    #[error("Out of bounds")]
10    OutOfBounds,
11
12    /// Invalid Amf0 reference tag
13    #[error("Invalid reference")]
14    InvalidReference(u16),
15
16    /// Invalid type marker
17    #[error("Unsupported tag")]
18    UnsupportedType(u8),
19
20    /// A nom internal error
21    #[error("Nom internal error")]
22    Nom(&'a [u8], ErrorKind),
23
24    /// Packet is too large (too many headers or messages)
25    #[error("Packet has too many headers or messages")]
26    PacketTooLarge,
27
28    /// Unable to find an object in the reference table
29    #[error("Object not in reference table")]
30    ObjectMissingFromReferenceTable(u64),
31
32    /// An unknown IO error occured
33    #[error("IO error: {0}")]
34    IoError(String, std::io::ErrorKind),
35}
36
37impl<'a> ParseError<&'a [u8]> for Error<'a> {
38    fn from_error_kind(input: &'a [u8], kind: ErrorKind) -> Self {
39        Error::Nom(input, kind)
40    }
41
42    fn append(_: &[u8], _: ErrorKind, other: Self) -> Self {
43        other
44    }
45}
46
47impl<'a, E> FromExternalError<&'a [u8], E> for Error<'a> {
48    fn from_external_error(input: &'a [u8], kind: ErrorKind, _e: E) -> Self {
49        Error::Nom(input, kind)
50    }
51}