Skip to main content

imessage_database/error/
message.rs

1/*!
2 Errors that can happen when parsing message data.
3*/
4
5use std::fmt::{Display, Formatter, Result};
6
7use crabstep::error::TypedStreamError;
8
9use crate::error::{plist::PlistParseError, streamtyped::StreamTypedError};
10
11/// Errors that can happen when working with message table data
12#[derive(Debug)]
13pub enum MessageError {
14    /// Message has no text content
15    NoText,
16    /// Error occurred when parsing with the `StreamTyped` parser
17    StreamTypedParseError(StreamTypedError),
18    /// Error occurred when deserializing a `typedstream`
19    TypedStreamError(TypedStreamError),
20    /// Error occurred when parsing a property list
21    PlistParseError(PlistParseError),
22    /// Timestamp value is invalid or out of range
23    InvalidTimestamp(i64),
24}
25
26impl Display for MessageError {
27    fn fmt(&self, fmt: &mut Formatter<'_>) -> Result {
28        match self {
29            MessageError::NoText => write!(fmt, "Message has no text!"),
30            MessageError::StreamTypedParseError(why) => {
31                write!(
32                    fmt,
33                    "Failed to parse attributedBody with legacy parser: {why}"
34                )
35            }
36            MessageError::InvalidTimestamp(when) => {
37                write!(fmt, "Timestamp is invalid: {when}")
38            }
39            MessageError::TypedStreamError(typed_stream_error) => {
40                write!(
41                    fmt,
42                    "Failed to deserialize typed stream: {typed_stream_error}"
43                )
44            }
45            MessageError::PlistParseError(why) => {
46                write!(fmt, "Failed to parse property list: {why}")
47            }
48        }
49    }
50}
51
52impl std::error::Error for MessageError {
53    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
54        match self {
55            MessageError::StreamTypedParseError(e) => Some(e),
56            MessageError::TypedStreamError(e) => Some(e),
57            MessageError::PlistParseError(e) => Some(e),
58            _ => None,
59        }
60    }
61}
62
63impl From<StreamTypedError> for MessageError {
64    fn from(err: StreamTypedError) -> Self {
65        MessageError::StreamTypedParseError(err)
66    }
67}
68
69impl From<TypedStreamError> for MessageError {
70    fn from(err: TypedStreamError) -> Self {
71        MessageError::TypedStreamError(err)
72    }
73}
74
75impl From<PlistParseError> for MessageError {
76    fn from(err: PlistParseError) -> Self {
77        MessageError::PlistParseError(err)
78    }
79}