Debian Codegraph @

src/serde/error.rs

raw symbols SCIP
1 ///! Serde error and result types
2 use crate::serde::common::*;
3
4 pub type Result<T, E = Error> = std::result::Result<T, E>;
5
6 /// An enumeration of potential errors that appear during serde serialiation and
7 /// deserialization
8 #[derive(Debug)]
9 pub enum Error {
10 /// Error that occurs if a map with a key type which does not serialize to
11 /// a byte string is encountered
12 ArbitraryMapKeysUnsupported,
13 /// Error that occurs if methods on MapSerializer are called out of order
14 MapSerializationCallOrder,
15 /// Error that occurs if a bool is deserialized from an integer value other
16 /// than `0` or `1`
17 InvalidBool(String),
18 /// Error that occurs if an f32 is deserialized from an string of length other
19 /// than 4
20 InvalidF32(usize),
21 /// Error that occurs if an f64 is deserialized from an string of length other
22 /// than 8
23 InvalidF64(usize),
24 /// Error that occurs if a char is deserialized from a string containing more
25 /// than one character
26 InvalidChar(usize),
27 /// Error that occurs if trailing bytes remain after deserialization, if the
28 /// deserializer is configured to forbid trailing bytes
29 TrailingBytes,
30 /// Error that occurs if a serde-related error occurs during serialization
31 CustomEncode(String),
32 /// Error that occurs if a serde-related error occurs during deserialization
33 CustomDecode(String),
34 /// Error that occurs if a problem is encountered during serialization
35 Encode(encoding::Error),
36 /// Error that occurs if a problem is encountered during deserialization
37 Decode(decoding::Error),
38 }
39
40 impl From<encoding::Error> for Error {
41 fn from(encoding_error: encoding::Error) -> Self {
42 Error::Encode(encoding_error)
43 }
44 }
45
46 impl From<decoding::Error> for Error {
47 fn from(decoding_error: decoding::Error) -> Self {
48 Error::Decode(decoding_error)
49 }
50 }
51
52 impl From<ParseIntError> for Error {
53 fn from(parse_int_error: ParseIntError) -> Self {
54 Error::Decode(parse_int_error.into())
55 }
56 }
57
58 impl From<Utf8Error> for Error {
59 fn from(utf8_error: Utf8Error) -> Self {
60 Error::Decode(utf8_error.into())
61 }
62 }
63
64 impl serde::ser::Error for Error {
65 fn custom<T>(msg: T) -> Self
66 where
67 T: Display,
68 {
69 Error::CustomEncode(msg.to_string())
70 }
71 }
72
73 impl serde::de::Error for Error {
74 fn custom<T: Display>(msg: T) -> Self {
75 Error::CustomDecode(msg.to_string())
76 }
77 }
78
79 impl Display for Error {
80 fn fmt(&self, f: &mut Formatter) -> fmt::Result {
81 match self {
82 Error::CustomEncode(message) => write!(f, "Serialization failed: {}", message),
83 Error::CustomDecode(message) => write!(f, "Deserialization failed: {}", message),
84 Error::Encode(error) => write!(f, "{}", error),
85 Error::Decode(error) => write!(f, "{}", error),
86 Error::InvalidBool(value) => write!(f, "Invalid integer value for bool: `{}`", value),
87 Error::InvalidF32(length) => {
88 write!(f, "Invalid length byte string value for f32: {}", length)
89 },
90 Error::InvalidF64(length) => {
91 write!(f, "Invalid length byte string value for f64: {}", length)
92 },
93 Error::InvalidChar(length) => {
94 write!(f, "Invalid length string value for char: {}", length)
95 },
96 Error::TrailingBytes => write!(f, "Trailing bytes remain after deserializing value"),
97 Error::ArbitraryMapKeysUnsupported => write!(
98 f,
99 "Maps with key types that do not serialize to byte strings are unsupported",
100 ),
101 Error::MapSerializationCallOrder => {
102 write!(f, "Map serialization methods called out of order")
103 },
104 }
105 }
106 }
107
108 impl std::error::Error for Error {}