1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/*!
 Errors that can happen when parsing `handwriting` data.
*/

use std::fmt::{Display, Formatter, Result};

/// Errors that can happen when parsing `handwriting` data
#[derive(Debug)]
pub enum HandwritingError {
    ProtobufError(protobuf::Error),
    InvalidFrameSize(usize),
    XZError(lzma_rs::error::Error),
    CompressionUnknown,
    InvalidStrokesLength(usize, usize),
    ConversionError,
    DecompressedNotSet,
    InvalidDecompressedLength(usize, usize),
    ResizeError(std::num::TryFromIntError),
}

impl Display for HandwritingError {
    fn fmt(&self, fmt: &mut Formatter<'_>) -> Result {
        match self {
            HandwritingError::ProtobufError(why) => {
                write!(fmt, "failed to parse handwriting protobuf: {why}")
            }
            HandwritingError::InvalidFrameSize(size) => write!(fmt, "expected size 8, got {size}"),
            HandwritingError::XZError(why) => write!(fmt, "failed to decompress xz: {why}"),
            HandwritingError::CompressionUnknown => write!(fmt, "compress method unknown"),
            HandwritingError::InvalidStrokesLength(index, length) => {
                write!(fmt, "can't access index {index} on array length {length}")
            }
            HandwritingError::ConversionError => write!(fmt, "failed to convert num"),
            HandwritingError::DecompressedNotSet => {
                write!(fmt, "decompressed length not set on compressed message")
            }
            HandwritingError::InvalidDecompressedLength(expected, got) => {
                write!(fmt, "expected decompressed length of {expected}, got {got}")
            }
            HandwritingError::ResizeError(why) => {
                write!(fmt, "failed to resize handwriting coordinates: {why}")
            }
        }
    }
}