Skip to main content

imessage_database/error/
digital_touch.rs

1/*!
2 Errors that can happen when parsing `digital touch` data.
3*/
4
5use std::fmt::{Display, Formatter, Result};
6
7/// Errors that can happen when parsing [`digital touch`](crate::message_types::digital_touch) data.
8#[derive(Debug)]
9pub enum DigitalTouchError {
10    /// Wraps an error returned by the protobuf parser.
11    ProtobufError(protobuf::Error),
12    /// The `TouchKind` discriminant was not a value we know how to parse.
13    UnknownDigitalTouchKind(i32),
14    /// Two parallel arrays that are expected to describe the same events had
15    /// different lengths (name, length, other name, other length).
16    ArraysDoNotMatch(&'static str, usize, &'static str, usize),
17    /// A length-prefixed stroke ran past the end of its buffer (needed, available).
18    InvalidStrokesLength(usize, usize),
19    /// Wraps an error returned while reading an embedded `NSKeyedArchiver` archive.
20    ArchiveError(plist::Error),
21}
22
23impl std::error::Error for DigitalTouchError {
24    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
25        match self {
26            DigitalTouchError::ProtobufError(why) => Some(why),
27            DigitalTouchError::ArchiveError(why) => Some(why),
28            _ => None,
29        }
30    }
31}
32
33impl Display for DigitalTouchError {
34    fn fmt(&self, fmt: &mut Formatter<'_>) -> Result {
35        match self {
36            DigitalTouchError::ProtobufError(why) => {
37                write!(fmt, "failed to parse digital touch protobuf: {why}")
38            }
39            DigitalTouchError::UnknownDigitalTouchKind(kind) => {
40                write!(fmt, "unknown digital touch kind: {kind}")
41            }
42            DigitalTouchError::ArraysDoNotMatch(n1, v1, n2, v2) => {
43                write!(fmt, "mismatched array lengths: {n1} ({v1}) != {n2} ({v2})")
44            }
45            DigitalTouchError::InvalidStrokesLength(needed, available) => {
46                write!(
47                    fmt,
48                    "stroke needs {needed} bytes but only {available} remain"
49                )
50            }
51            DigitalTouchError::ArchiveError(why) => {
52                write!(fmt, "failed to read digital touch media archive: {why}")
53            }
54        }
55    }
56}