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
//! ### Error
//! A module for the error type used in the library. It is a simple enum with a variant for each
//! error that can occur in the library. It uses `thiserror` internally.

use super::serializer::Delimiter;

#[derive(thiserror::Error, Debug)]
pub enum Error {
    #[error("could not get the last bit from the data.")]
    NoBit,

    #[error("could not get the last byte from the data.")]
    NoByte,

    #[error("tried to get {0} bytes from the data of length {1}.")]
    NLargerThanLength(usize, usize),

    #[error("could not serialize the value: {0}")]
    SerializationError(String),

    #[error("could not deserialize the value: {0}")]
    DeserializationError(String),

    #[error("calls to {0} are not supported")]
    UnsupportedCall(String),

    #[error("unexpected end of file")]
    UnexpectedEOF,

    #[error("invalid type size")]
    InvalidTypeSize,

    #[error("type conversion error")]
    ConversionError,

    #[error("expected delimiter {0}")]
    ExpectedDelimiter(Delimiter),
}

impl serde::ser::Error for Error {
    fn custom<T>(msg: T) -> Self
    where
        T: std::fmt::Display,
    {
        Error::SerializationError(msg.to_string())
    }
}

impl serde::de::Error for Error {
    fn custom<T>(msg: T) -> Self
    where
        T: std::fmt::Display,
    {
        Error::DeserializationError(msg.to_string())
    }
}