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
/*!
 Errors that can happen when parsing plist data
*/

use std::fmt::{Display, Formatter, Result};

use crate::error::streamtyped::StreamTypedError;

/// Errors that can happen when parsing the plist data stored in the `payload_data` field
#[derive(Debug)]
pub enum PlistParseError {
    MissingKey(String),
    NoValueAtIndex(usize),
    InvalidType(String, String),
    InvalidTypeIndex(usize, String),
    InvalidDictionarySize(usize, usize),
    NoPayload,
    WrongMessageType,
    InvalidEditedMessage(String),
    StreamTypedError(StreamTypedError),
}

impl Display for PlistParseError {
    fn fmt(&self, fmt: &mut Formatter<'_>) -> Result {
        match self {
            PlistParseError::MissingKey(key) => write!(fmt, "Expected key {}, found nothing!", key),
            PlistParseError::NoValueAtIndex(idx) => write!(
                fmt,
                "Payload referenced index {}, but there is no data!",
                idx
            ),
            PlistParseError::InvalidType(key, value) => {
                write!(fmt, "Invalid data found at {}, expected {}", key, value)
            }
            PlistParseError::InvalidTypeIndex(idx, value) => {
                write!(
                    fmt,
                    "Invalid data found at object index {}, expected {}",
                    idx, value
                )
            }
            PlistParseError::InvalidDictionarySize(a, b) => write!(
                fmt,
                "Invalid dictionary size, found {} keys and {} values",
                a, b
            ),
            PlistParseError::NoPayload => write!(fmt, "Unable to acquire payload data!"),
            PlistParseError::WrongMessageType => write!(fmt, "Message is not an app message!"),
            PlistParseError::InvalidEditedMessage(message) => {
                write!(fmt, "Unable to parse message from binary data: {message}")
            }
            PlistParseError::StreamTypedError(why) => write!(fmt, "{why}"),
        }
    }
}