Skip to main content

imessage_database/error/
handwriting.rs

1/*!
2 Errors that can happen when parsing `handwriting` data.
3*/
4
5use std::fmt::{Display, Formatter, Result};
6
7/// Errors that can happen when parsing `handwriting` data
8#[derive(Debug)]
9pub enum HandwritingError {
10    /// Wraps an error returned by the protobuf parser.
11    ProtobufError(protobuf::Error),
12    /// Indicates that the frame size was invalid.
13    InvalidFrameSize(usize),
14    /// Wraps an error returned by the LZMA decompression.
15    XZError(lzma_rs::error::Error),
16    /// Indicates that the compression method is unknown.
17    CompressionUnknown,
18    /// Indicates that the strokes length is invalid.
19    InvalidStrokesLength(usize, usize),
20    /// Indicates that a stroke contained zero points.
21    EmptyStroke,
22    /// Indicates a numeric conversion error.
23    ConversionError,
24    /// Indicates that the decompressed data was not set.
25    DecompressedNotSet,
26    /// Indicates that the decompressed length is invalid.
27    InvalidDecompressedLength(usize, usize),
28    /// Wraps an error that occurred during resizing of handwriting coordinates.
29    ResizeError(std::num::TryFromIntError),
30}
31
32impl std::error::Error for HandwritingError {
33    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
34        match self {
35            HandwritingError::ProtobufError(e) => Some(e),
36            HandwritingError::XZError(e) => Some(e),
37            HandwritingError::ResizeError(e) => Some(e),
38            _ => None,
39        }
40    }
41}
42
43impl Display for HandwritingError {
44    fn fmt(&self, fmt: &mut Formatter<'_>) -> Result {
45        match self {
46            HandwritingError::ProtobufError(why) => {
47                write!(fmt, "failed to parse handwriting protobuf: {why}")
48            }
49            HandwritingError::InvalidFrameSize(size) => write!(fmt, "expected size 8, got {size}"),
50            HandwritingError::XZError(why) => write!(fmt, "failed to decompress xz: {why}"),
51            HandwritingError::CompressionUnknown => write!(fmt, "compress method unknown"),
52            HandwritingError::InvalidStrokesLength(index, length) => {
53                write!(fmt, "can't access index {index} on array length {length}")
54            }
55            HandwritingError::EmptyStroke => write!(fmt, "stroke contains zero points"),
56            HandwritingError::ConversionError => write!(fmt, "failed to convert num"),
57            HandwritingError::DecompressedNotSet => {
58                write!(fmt, "decompressed length not set on compressed message")
59            }
60            HandwritingError::InvalidDecompressedLength(expected, got) => {
61                write!(fmt, "expected decompressed length of {expected}, got {got}")
62            }
63            HandwritingError::ResizeError(why) => {
64                write!(fmt, "failed to resize handwriting coordinates: {why}")
65            }
66        }
67    }
68}