serde_smile/
error.rs

1use serde::{de, ser};
2use std::{error, fmt, io};
3
4#[derive(Debug)]
5enum ErrorKind {
6    Io(io::Error),
7    Custom(String),
8    KeyMustBeAString,
9    EofWhileParsingValue,
10    ReservedToken,
11    InvalidStringReference,
12    UnterminatedVint,
13    BufferLengthOverflow,
14    InvalidUtf8,
15    RecursionLimitExceeded,
16    TrailingData,
17    EofWhileParsingArray,
18    UnexpectedToken,
19    EofWhileParsingMap,
20    InvalidHeader,
21    UnsupportedVersion,
22    EofWhileParsingHeader,
23}
24
25/// An error encountered when serializing or deserializing to or from Smile.
26#[derive(Debug)]
27pub struct Error(Box<ErrorKind>);
28
29impl fmt::Display for Error {
30    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
31        match &*self.0 {
32            ErrorKind::Io(_) => f.write_str("IO error"),
33            ErrorKind::Custom(e) => f.write_str(e),
34            ErrorKind::KeyMustBeAString => f.write_str("key must be a string"),
35            ErrorKind::EofWhileParsingValue => f.write_str("EOF while parsing a value"),
36            ErrorKind::ReservedToken => f.write_str("reserved token"),
37            ErrorKind::InvalidStringReference => f.write_str("invalid string reference"),
38            ErrorKind::UnterminatedVint => f.write_str("unterminated vint"),
39            ErrorKind::BufferLengthOverflow => f.write_str("buffer length overflow"),
40            ErrorKind::InvalidUtf8 => f.write_str("invalid UTF-8"),
41            ErrorKind::RecursionLimitExceeded => f.write_str("recursion limit exceeded"),
42            ErrorKind::TrailingData => f.write_str("trailing data"),
43            ErrorKind::EofWhileParsingArray => f.write_str("EOF while parsing array"),
44            ErrorKind::UnexpectedToken => f.write_str("unexpected token"),
45            ErrorKind::EofWhileParsingMap => f.write_str("EOF while parsing map"),
46            ErrorKind::InvalidHeader => f.write_str("invalid header"),
47            ErrorKind::UnsupportedVersion => f.write_str("unsupported version"),
48            ErrorKind::EofWhileParsingHeader => f.write_str("EOF while parsing header"),
49        }
50    }
51}
52
53impl error::Error for Error {
54    fn source(&self) -> Option<&(dyn error::Error + 'static)> {
55        match &*self.0 {
56            ErrorKind::Io(e) => Some(e),
57            _ => None,
58        }
59    }
60}
61
62impl ser::Error for Error {
63    fn custom<T>(msg: T) -> Self
64    where
65        T: fmt::Display,
66    {
67        Error(Box::new(ErrorKind::Custom(msg.to_string())))
68    }
69}
70
71impl de::Error for Error {
72    fn custom<T>(msg: T) -> Self
73    where
74        T: fmt::Display,
75    {
76        Error(Box::new(ErrorKind::Custom(msg.to_string())))
77    }
78}
79
80impl Error {
81    pub(crate) fn io(e: io::Error) -> Self {
82        Error(Box::new(ErrorKind::Io(e)))
83    }
84
85    pub(crate) fn key_must_be_a_string() -> Self {
86        Error(Box::new(ErrorKind::KeyMustBeAString))
87    }
88
89    pub(crate) fn eof_while_parsing_value() -> Self {
90        Error(Box::new(ErrorKind::EofWhileParsingValue))
91    }
92
93    pub(crate) fn reserved_token() -> Self {
94        Error(Box::new(ErrorKind::ReservedToken))
95    }
96
97    pub(crate) fn invalid_string_reference() -> Self {
98        Error(Box::new(ErrorKind::InvalidStringReference))
99    }
100
101    pub(crate) fn unterminated_vint() -> Self {
102        Error(Box::new(ErrorKind::UnterminatedVint))
103    }
104
105    pub(crate) fn buffer_length_overflow() -> Self {
106        Error(Box::new(ErrorKind::BufferLengthOverflow))
107    }
108
109    pub(crate) fn invalid_utf8() -> Self {
110        Error(Box::new(ErrorKind::InvalidUtf8))
111    }
112
113    pub(crate) fn recursion_limit_exceeded() -> Self {
114        Error(Box::new(ErrorKind::RecursionLimitExceeded))
115    }
116
117    pub(crate) fn trailing_data() -> Self {
118        Error(Box::new(ErrorKind::TrailingData))
119    }
120
121    pub(crate) fn eof_while_parsing_array() -> Self {
122        Error(Box::new(ErrorKind::EofWhileParsingArray))
123    }
124
125    pub(crate) fn unexpected_token() -> Self {
126        Error(Box::new(ErrorKind::UnexpectedToken))
127    }
128
129    pub(crate) fn eof_while_parsing_map() -> Self {
130        Error(Box::new(ErrorKind::EofWhileParsingMap))
131    }
132
133    pub(crate) fn invalid_header() -> Self {
134        Error(Box::new(ErrorKind::InvalidHeader))
135    }
136
137    pub(crate) fn unsupported_version() -> Self {
138        Error(Box::new(ErrorKind::UnsupportedVersion))
139    }
140
141    pub(crate) fn eof_while_parsing_header() -> Self {
142        Error(Box::new(ErrorKind::EofWhileParsingHeader))
143    }
144}