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 From<StreamTypedError> for MessageError {
53    fn from(err: StreamTypedError) -> Self {
54        MessageError::StreamTypedParseError(err)
55    }
56}
57
58impl From<TypedStreamError> for MessageError {
59    fn from(err: TypedStreamError) -> Self {
60        MessageError::TypedStreamError(err)
61    }
62}
63
64impl From<PlistParseError> for MessageError {
65    fn from(err: PlistParseError) -> Self {
66        MessageError::PlistParseError(err)
67    }
68}