1use std::error;
4use std::fmt;
5use std::io;
6use std::result;
7use std::str::Utf8Error;
8use std::string::FromUtf8Error;
9use serde::{de, ser};
10
11#[derive(Debug)]
13pub enum Error {
14 Io(io::Error),
17 Encoding(Utf8Error),
19 Unknown(String),
21 Unsupported(&'static str),
23}
24pub type Result<T> = result::Result<T, Error>;
26
27impl fmt::Display for Error {
28 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
29 match *self {
30 Error::Io(ref err) => err.fmt(fmt),
31 Error::Encoding(ref err) => err.fmt(fmt),
32 Error::Unknown(ref msg) => msg.fmt(fmt),
33 Error::Unsupported(ref msg) => msg.fmt(fmt),
34 }
35 }
36}
37
38impl error::Error for Error {
39 fn source(&self) -> Option<&(dyn error::Error + 'static)> {
40 match *self {
41 Error::Io(ref err) => Some(err),
42 Error::Encoding(ref err) => Some(err),
43 Error::Unknown(_) => None,
44 Error::Unsupported(_) => None,
45 }
46 }
47}
48impl ser::Error for Error {
50 fn custom<T: fmt::Display>(msg: T) -> Self {
51 Error::Unknown(msg.to_string())
52 }
53}
54impl de::Error for Error {
56 fn custom<T: fmt::Display>(msg: T) -> Self {
57 Error::Unknown(msg.to_string())
58 }
59}
60impl From<io::Error> for Error {
62 fn from(err: io::Error) -> Self {
63 Error::Io(err)
64 }
65}
66impl From<Utf8Error> for Error {
68 fn from(err: Utf8Error) -> Self {
69 Error::Encoding(err)
70 }
71}
72impl From<FromUtf8Error> for Error {
73 fn from(err: FromUtf8Error) -> Self {
74 Error::Encoding(err.utf8_error())
75 }
76}