1use std::error::Error as StdError;
2use std::io;
3use std::str::Utf8Error;
4use std::{error, fmt};
5
6use serde;
7
8pub type Result<T> = ::std::result::Result<T, Error>;
10
11pub type Error = Box<ErrorKind>;
13
14#[derive(Debug)]
16pub enum ErrorKind {
17 Io(io::Error),
20 InvalidUtf8Encoding(Utf8Error),
22 InvalidBoolEncoding(u8),
25 InvalidCharEncoding,
27 InvalidTagEncoding(usize),
30 DeserializeAnyNotSupported,
33 SizeLimit,
36 SequenceMustHaveLength,
38 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) => write!(fmt, "io error: {}", ioerr),
86 ErrorKind::InvalidUtf8Encoding(ref e) => write!(fmt, "{}: {}", self.description(), e),
87 ErrorKind::InvalidBoolEncoding(b) => {
88 write!(fmt, "{}, expected 0 or 1, found {}", self.description(), b)
89 }
90 ErrorKind::InvalidCharEncoding => write!(fmt, "{}", self.description()),
91 ErrorKind::InvalidTagEncoding(tag) => {
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) => 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}