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 crate::error::plist::PlistParseError;
8
9/// Errors that can happen when working with message table data
10#[derive(Debug)]
11pub enum MessageError {
12    /// Error occurred when parsing a property list
13    PlistParseError(PlistParseError),
14    /// Timestamp value is invalid or out of range
15    InvalidTimestamp(i64),
16}
17
18impl Display for MessageError {
19    fn fmt(&self, fmt: &mut Formatter<'_>) -> Result {
20        match self {
21            MessageError::InvalidTimestamp(when) => {
22                write!(fmt, "Timestamp is invalid: {when}")
23            }
24            MessageError::PlistParseError(why) => {
25                write!(fmt, "Failed to parse property list: {why}")
26            }
27        }
28    }
29}
30
31impl std::error::Error for MessageError {
32    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
33        match self {
34            MessageError::PlistParseError(e) => Some(e),
35            _ => None,
36        }
37    }
38}
39
40impl From<PlistParseError> for MessageError {
41    fn from(err: PlistParseError) -> Self {
42        MessageError::PlistParseError(err)
43    }
44}