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    /// Indicates that the frame's width or height is too large to render.
15    InvalidDimensions(u16, u16),
16    /// Wraps an error returned by the LZMA decompression.
17    XZError(lzma_rs::error::Error),
18    /// Indicates that the compression method is unknown.
19    CompressionUnknown,
20    /// Indicates that the strokes length is invalid.
21    InvalidStrokesLength(usize, usize),
22    /// Indicates that a stroke contained zero points.
23    EmptyStroke,
24    /// Indicates a numeric conversion error.
25    ConversionError,
26    /// Indicates that the decompressed data was not set.
27    DecompressedNotSet,
28    /// Indicates that the decompressed length is invalid.
29    InvalidDecompressedLength(usize, usize),
30    /// Wraps an error that occurred during resizing of handwriting coordinates.
31    ResizeError(std::num::TryFromIntError),
32}
33
34impl std::error::Error for HandwritingError {
35    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
36        match self {
37            HandwritingError::ProtobufError(e) => Some(e),
38            HandwritingError::XZError(e) => Some(e),
39            HandwritingError::ResizeError(e) => Some(e),
40            _ => None,
41        }
42    }
43}
44
45impl Display for HandwritingError {
46    fn fmt(&self, fmt: &mut Formatter<'_>) -> Result {
47        match self {
48            HandwritingError::ProtobufError(why) => {
49                write!(fmt, "failed to parse handwriting protobuf: {why}")
50            }
51            HandwritingError::InvalidFrameSize(size) => write!(fmt, "expected size 8, got {size}"),
52            HandwritingError::InvalidDimensions(width, height) => {
53                write!(fmt, "frame dimensions {width}x{height} are out of range")
54            }
55            HandwritingError::XZError(why) => write!(fmt, "failed to decompress xz: {why}"),
56            HandwritingError::CompressionUnknown => write!(fmt, "compress method unknown"),
57            HandwritingError::InvalidStrokesLength(index, length) => {
58                write!(fmt, "can't access index {index} on array length {length}")
59            }
60            HandwritingError::EmptyStroke => write!(fmt, "stroke contains zero points"),
61            HandwritingError::ConversionError => write!(fmt, "failed to convert num"),
62            HandwritingError::DecompressedNotSet => {
63                write!(fmt, "decompressed length not set on compressed message")
64            }
65            HandwritingError::InvalidDecompressedLength(expected, got) => {
66                write!(fmt, "expected decompressed length of {expected}, got {got}")
67            }
68            HandwritingError::ResizeError(why) => {
69                write!(fmt, "failed to resize handwriting coordinates: {why}")
70            }
71        }
72    }
73}