rust_fr/
error.rs

1//! ### Error
2//! A module for the error type used in the library. It is a simple enum with a variant for each
3//! error that can occur in the library. It uses `thiserror` internally.
4
5use super::serializer::Delimiter;
6
7#[derive(thiserror::Error, Debug)]
8pub enum Error {
9    #[error("could not get the last bit from the data.")]
10    NoBit,
11
12    #[error("could not get the last byte from the data.")]
13    NoByte,
14
15    #[error("tried to get {0} bytes from the data of length {1}.")]
16    NLargerThanLength(usize, usize),
17
18    #[error("could not serialize the value: {0}")]
19    SerializationError(String),
20
21    #[error("could not deserialize the value: {0}")]
22    DeserializationError(String),
23
24    #[error("calls to {0} are not supported")]
25    UnsupportedCall(String),
26
27    #[error("unexpected end of file")]
28    UnexpectedEOF,
29
30    #[error("invalid type size")]
31    InvalidTypeSize,
32
33    #[error("type conversion error")]
34    ConversionError,
35
36    #[error("expected delimiter {0}")]
37    ExpectedDelimiter(Delimiter),
38}
39
40impl serde::ser::Error for Error {
41    fn custom<T>(msg: T) -> Self
42    where
43        T: std::fmt::Display,
44    {
45        Error::SerializationError(msg.to_string())
46    }
47}
48
49impl serde::de::Error for Error {
50    fn custom<T>(msg: T) -> Self
51    where
52        T: std::fmt::Display,
53    {
54        Error::DeserializationError(msg.to_string())
55    }
56}