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::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    /// Timestamp value is invalid or out of range
21    InvalidTimestamp(i64),
22}
23
24impl Display for MessageError {
25    fn fmt(&self, fmt: &mut Formatter<'_>) -> Result {
26        match self {
27            MessageError::NoText => write!(fmt, "Message has no text!"),
28            MessageError::StreamTypedParseError(why) => {
29                write!(
30                    fmt,
31                    "Failed to parse attributedBody with legacy parser: {why}"
32                )
33            }
34            MessageError::InvalidTimestamp(when) => {
35                write!(fmt, "Timestamp is invalid: {when}")
36            }
37            MessageError::TypedStreamError(typed_stream_error) => {
38                write!(
39                    fmt,
40                    "Failed to deserialize typed stream: {typed_stream_error}"
41                )
42            }
43        }
44    }
45}
46
47impl From<StreamTypedError> for MessageError {
48    fn from(err: StreamTypedError) -> Self {
49        MessageError::StreamTypedParseError(err)
50    }
51}
52
53impl From<TypedStreamError> for MessageError {
54    fn from(err: TypedStreamError) -> Self {
55        MessageError::TypedStreamError(err)
56    }
57}