imessage_database/error/
plist.rs

1/*!
2 Errors that can happen when parsing plist data.
3*/
4
5use crabstep::error::TypedStreamError;
6
7use crate::error::handwriting::HandwritingError;
8use crate::error::streamtyped::StreamTypedError;
9use std::fmt::{Display, Formatter, Result};
10
11/// Errors that can happen when parsing the plist data stored in the `payload_data` field
12#[derive(Debug)]
13pub enum PlistParseError {
14    /// Expected key was not found in the plist data
15    MissingKey(String),
16    /// No value was found at the specified index
17    NoValueAtIndex(usize),
18    /// Value had an incorrect type for the specified key
19    InvalidType(String, String),
20    /// Value had an incorrect type at the specified index
21    InvalidTypeIndex(usize, String),
22    /// Dictionary has mismatched number of keys and values
23    InvalidDictionarySize(usize, usize),
24    /// No payload data was found
25    NoPayload,
26    /// Message is not of the expected type
27    WrongMessageType,
28    /// Could not parse an edited message
29    InvalidEditedMessage(String),
30    /// Error from stream typed parsing
31    StreamTypedError(StreamTypedError),
32    /// Error from typedstream parsing
33    TypedStreamError(TypedStreamError),
34    /// Error from handwriting data parsing
35    HandwritingError(HandwritingError),
36    /// Error parsing Digital Touch message
37    DigitalTouchError,
38    /// Error parsing a poll message
39    PollError,
40}
41
42impl Display for PlistParseError {
43    fn fmt(&self, fmt: &mut Formatter<'_>) -> Result {
44        match self {
45            PlistParseError::MissingKey(key) => write!(fmt, "Expected key {key}, found nothing!"),
46            PlistParseError::NoValueAtIndex(idx) => {
47                write!(fmt, "Payload referenced index {idx}, but there is no data!")
48            }
49            PlistParseError::InvalidType(key, value) => {
50                write!(fmt, "Invalid data found at {key}, expected {value}")
51            }
52            PlistParseError::InvalidTypeIndex(idx, value) => {
53                write!(
54                    fmt,
55                    "Invalid data found at object index {idx}, expected {value}"
56                )
57            }
58            PlistParseError::InvalidDictionarySize(a, b) => write!(
59                fmt,
60                "Invalid dictionary size, found {a} keys and {b} values"
61            ),
62            PlistParseError::NoPayload => write!(fmt, "Unable to acquire payload data!"),
63            PlistParseError::WrongMessageType => write!(fmt, "Message is not an app message!"),
64            PlistParseError::InvalidEditedMessage(message) => {
65                write!(fmt, "Unable to parse message from binary data: {message}")
66            }
67            PlistParseError::StreamTypedError(why) => write!(fmt, "{why}"),
68            PlistParseError::HandwritingError(why) => write!(fmt, "{why}"),
69            PlistParseError::DigitalTouchError => {
70                write!(fmt, "Unable to parse Digital Touch Message!")
71            }
72            PlistParseError::TypedStreamError(typed_stream_error) => {
73                write!(fmt, "TypedStream error: {typed_stream_error}")
74            }
75            PlistParseError::PollError => write!(fmt, "Unable to parse Poll Message!"),
76        }
77    }
78}
79
80impl From<TypedStreamError> for PlistParseError {
81    fn from(error: TypedStreamError) -> Self {
82        PlistParseError::TypedStreamError(error)
83    }
84}
85
86impl From<StreamTypedError> for PlistParseError {
87    fn from(error: StreamTypedError) -> Self {
88        PlistParseError::StreamTypedError(error)
89    }
90}