imessage_database/error/
typedstream.rs

1/*!
2 Errors that can happen when parsing `typedstream` data. This module is for the new `typedstream` deserializer.
3*/
4
5use std::{
6    array::TryFromSliceError,
7    fmt::{Display, Formatter, Result},
8    str::Utf8Error,
9};
10
11/// Errors that can happen when parsing `typedstream` data
12#[derive(Debug)]
13pub enum TypedStreamError {
14    /// Indicates an attempt to access data beyond the bounds of the buffer.
15    /// The first parameter is the attempted index, second is the buffer length
16    OutOfBounds(usize, usize),
17    /// Indicates that the typedstream header is invalid or corrupted
18    InvalidHeader,
19    /// Error that occurs when trying to convert a slice
20    SliceError(TryFromSliceError),
21    /// Error that occurs when parsing a UTF-8 string
22    StringParseError(Utf8Error),
23    /// Indicates that an array could not be properly parsed
24    InvalidArray,
25    /// Indicates that a pointer could not be parsed, with the invalid byte value
26    InvalidPointer(usize),
27}
28
29impl Display for TypedStreamError {
30    fn fmt(&self, fmt: &mut Formatter<'_>) -> Result {
31        match self {
32            TypedStreamError::OutOfBounds(idx, len) => {
33                write!(fmt, "Index {idx:x} is outside of range {len:x}!")
34            }
35            TypedStreamError::InvalidHeader => write!(fmt, "Invalid typedstream header!"),
36            TypedStreamError::SliceError(why) => {
37                write!(fmt, "Unable to slice source stream: {why}")
38            }
39            TypedStreamError::StringParseError(why) => write!(fmt, "Failed to parse string: {why}"),
40            TypedStreamError::InvalidArray => write!(fmt, "Failed to parse array data"),
41            TypedStreamError::InvalidPointer(why) => write!(fmt, "Failed to parse pointer: {why}"),
42        }
43    }
44}