1use serde;
2use std;
3
4pub type Result<T> = std::result::Result<T, Error>;
5
6#[derive(Debug)]
7pub enum Error {
8 IO(std::io::Error),
9 Message(String),
10 TrailingChars,
11 Eof,
12 IncorrectType(&'static str, char),
13 NumberSyntax,
14}
15
16impl serde::ser::Error for Error {
17 fn custom<T: std::fmt::Display>(msg: T) -> Self {
18 Error::Message(msg.to_string())
19 }
20}
21
22impl serde::de::Error for Error {
23 fn custom<T: std::fmt::Display>(msg: T) -> Self {
24 Error::Message(msg.to_string())
25 }
26}
27
28impl std::fmt::Display for Error {
29 fn fmt(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
30 formatter.write_str(std::error::Error::description(self))
31 }
32}
33
34impl std::error::Error for Error {
35 fn description(&self) -> &str {
36 match *self {
37 Error::IO(ref err) => err.description(),
38 Error::Message(ref msg) => msg,
39 Error::TrailingChars => "trailing characters",
40 Error::Eof => "unexpected end of input",
41 Error::IncorrectType(ref _expected, ref _found) => "incorrect type found", Error::NumberSyntax => "failed to parse number",
43 }
44 }
45}
46
47impl From<std::io::Error> for Error {
48 fn from(e: std::io::Error) -> Error {
49 Error::IO(e)
50 }
51}