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
46
47
48
49
50
51
52
53
54
55
56
57
use std::ops::Deref;
use bincode::ErrorKind;
use thiserror::Error;

///Result using the `Error`
pub type Result<T> = std::result::Result<T, Error>;

///Enum representing decompression errors
#[derive(Error, Debug)]
pub enum Error {

    ///Error created by the `write` macro from `std::fmt::Error` when converting a code into a string
    ///
    /// (Not really sure if this is needed as the errors from `std::fmt::Error` are not well defined
    #[error("Error converting IR to string")]
    Format(#[from] std::fmt::Error),

    ///Raised when the deserializer expects more bytes than it gets.
    ///
    /// For example, a code starting with 240 (unicode) expects at least one byte to follow. If it doesn't, this error will be raised.
    #[error("Unexpected end of bytes. Deserialiser expected more bytes in the decompress slice")]
    UnexpectedEndOfBytes,

    ///If an invalid unicode sequence is detected by the deserializer
    #[error("Could not deserialize invalid unicode scalar value")]
    InvalidUnicodeChar,

    ///This error encompasses other `bincode` errors that are impossible or unlikely
    #[error("Unexpected bincode error")]
    OtherBincode(bincode::Error),
}

impl From<bincode::Error> for Error {
    fn from(value: bincode::Error) -> Self {
        match value.deref() {
            ErrorKind::InvalidCharEncoding => {
                Error::InvalidUnicodeChar
            }
            ErrorKind::SizeLimit => {
                Error::UnexpectedEndOfBytes
            }
            ErrorKind::Io(e) => {
                match e.kind() {
                    std::io::ErrorKind::UnexpectedEof => {
                        Error::UnexpectedEndOfBytes
                    }
                    _ => {
                        Error::OtherBincode(value)
                    }
                }
            }
            _ =>  {
                Error::OtherBincode(value)
            }
        }
    }
}