byte_size/
error.rs

1use std::ops::Deref;
2use bincode::ErrorKind;
3use thiserror::Error;
4
5///Result using the [enum@Error]
6pub type Result<T> = std::result::Result<T, Error>;
7
8///Enum representing decompression errors
9#[derive(Error, Debug)]
10pub enum Error {
11
12    ///Error created by the `write` macro from [std::fmt::Error] when converting a code into a string
13    ///
14    /// (Not really sure if this is needed as the errors from std::fmt are not well defined
15    #[error("Error converting IR to string")]
16    Format(#[from] std::fmt::Error),
17
18    ///Raised when the deserializer expects more bytes than it gets.
19    ///
20    /// For example, a code starting with 240 (unicode) expects at least one byte to follow. If it doesn't, this error will be raised.
21    #[error("Unexpected end of bytes. Deserialiser expected more bytes in the decompress slice")]
22    UnexpectedEndOfBytes,
23
24    ///If an invalid unicode sequence is detected by the deserializer
25    #[error("Could not deserialize invalid unicode scalar value")]
26    InvalidUnicodeChar,
27
28    ///This error encompasses other [bincode] errors that are impossible or unlikely
29    #[error("Unexpected bincode error")]
30    OtherBincode(bincode::Error),
31}
32
33impl From<bincode::Error> for Error {
34    fn from(value: bincode::Error) -> Self {
35        match value.deref() {
36            ErrorKind::InvalidCharEncoding => {
37                Error::InvalidUnicodeChar
38            }
39            ErrorKind::SizeLimit => {
40                Error::UnexpectedEndOfBytes
41            }
42            ErrorKind::Io(e) => {
43                match e.kind() {
44                    std::io::ErrorKind::UnexpectedEof => {
45                        Error::UnexpectedEndOfBytes
46                    }
47                    _ => {
48                        Error::OtherBincode(value)
49                    }
50                }
51            }
52            _ =>  {
53                Error::OtherBincode(value)
54            }
55        }
56    }
57}