1use std::error::Error as StdError;
2use std::io;
3use std::str::Utf8Error;
4use std::{error, fmt};
5
6use serde;
7
8/// The result of a serialization or deserialization operation.
9pub type Result<T> = ::std::result::Result<T, Error>;
10
11/// An error that can be produced during (de)serializing.
12pub type Error = Box<ErrorKind>;
13
14/// The kind of error that can be produced during a serialization or deserialization.
15#[derive(Debug)]
16pub enum ErrorKind {
17 /// If the error stems from the reader/writer that is being used
18 /// during (de)serialization, that error will be stored and returned here.
19 Io(io::Error),
20 /// Returned if the deserializer attempts to deserialize a string that is not valid utf8
21 InvalidUtf8Encoding(Utf8Error),
22 /// Returned if the deserializer attempts to deserialize a bool that was
23 /// not encoded as either a 1 or a 0
24 InvalidBoolEncoding(u8),
25 /// Returned if the deserializer attempts to deserialize a char that is not in the correct format.
26 InvalidCharEncoding,
27 /// Returned if the deserializer attempts to deserialize the tag of an enum that is
28 /// not in the expected ranges
29 InvalidTagEncoding(usize),
30 /// Serde has a deserialize_any method that lets the format hint to the
31 /// object which route to take in deserializing.
32 DeserializeAnyNotSupported,
33 /// If (de)serializing a message takes more than the provided size limit, this
34 /// error is returned.
35 SizeLimit,
36 /// Bincode can not encode sequences of unknown length (like iterators).
37 SequenceMustHaveLength,
38 /// A custom error message from Serde.
39 Custom(String),
40}
41
42impl StdError for ErrorKind {
43 fn description(&self) -> &str {
44 match *self {
45 ErrorKind::Io(ref err) => error::Error::description(err),
46 ErrorKind::InvalidUtf8Encoding(_) => "string is not valid utf8",
47 ErrorKind::InvalidBoolEncoding(_) => "invalid u8 while decoding bool",
48 ErrorKind::InvalidCharEncoding => "char is not valid",
49 ErrorKind::InvalidTagEncoding(_) => "tag for enum is not valid",
50 ErrorKind::SequenceMustHaveLength => {
51 "Bincode can only encode sequences and maps that have a knowable size ahead of time"
52 }
53 ErrorKind::DeserializeAnyNotSupported => {
54 "Bincode doesn't support serde::Deserializer::deserialize_any"
55 }
56 ErrorKind::SizeLimit => "the size limit has been reached",
57 ErrorKind::Custom(ref msg) => msg,
58 }
59 }
60
61 fn cause(&self) -> Option<&error::Error> {
62 match *self {
63 ErrorKind::Io(ref err) => Some(err),
64 ErrorKind::InvalidUtf8Encoding(_) => None,
65 ErrorKind::InvalidBoolEncoding(_) => None,
66 ErrorKind::InvalidCharEncoding => None,
67 ErrorKind::InvalidTagEncoding(_) => None,
68 ErrorKind::SequenceMustHaveLength => None,
69 ErrorKind::DeserializeAnyNotSupported => None,
70 ErrorKind::SizeLimit => None,
71 ErrorKind::Custom(_) => None,
72 }
73 }
74}
75
76impl From<io::Error> for Error {
77 fn from(err: io::Error) -> Error {
78 ErrorKind::Io(err).into()
79 }
80}
81
82impl fmt::Display for ErrorKind {
83 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
84 match *self {
85 ErrorKind::Io(ref ioerr: &Error) => write!(fmt, "io error: {}", ioerr),
86 ErrorKind::InvalidUtf8Encoding(ref e: &Utf8Error) => write!(fmt, "{}: {}", self.description(), e),
87 ErrorKind::InvalidBoolEncoding(b: u8) => {
88 write!(fmt, "{}, expected 0 or 1, found {}", self.description(), b)
89 }
90 ErrorKind::InvalidCharEncoding => write!(fmt, "{}", self.description()),
91 ErrorKind::InvalidTagEncoding(tag: usize) => {
92 write!(fmt, "{}, found {}", self.description(), tag)
93 }
94 ErrorKind::SequenceMustHaveLength => write!(fmt, "{}", self.description()),
95 ErrorKind::SizeLimit => write!(fmt, "{}", self.description()),
96 ErrorKind::DeserializeAnyNotSupported => write!(
97 fmt,
98 "Bincode does not support the serde::Deserializer::deserialize_any method"
99 ),
100 ErrorKind::Custom(ref s: &String) => s.fmt(fmt),
101 }
102 }
103}
104
105impl serde::de::Error for Error {
106 fn custom<T: fmt::Display>(desc: T) -> Error {
107 ErrorKind::Custom(desc.to_string()).into()
108 }
109}
110
111impl serde::ser::Error for Error {
112 fn custom<T: fmt::Display>(msg: T) -> Self {
113 ErrorKind::Custom(msg.to_string()).into()
114 }
115}
116