1use std;
2use std::fmt::{self, Display};
3
4use serde::{de, ser};
5use std::io;
6use std::io::ErrorKind;
7use std::string::FromUtf8Error;
8
9pub type Result<T> = std::result::Result<T, Error>;
11
12#[derive(Clone, Debug, PartialEq)]
14pub enum Error {
15 Message(String),
17 Eof,
19 Syntax,
21 Io(String),
23 FromUtf8(String),
25}
26
27impl ser::Error for Error {
28 fn custom<T: Display>(msg: T) -> Self {
29 Error::Message(msg.to_string())
30 }
31}
32
33impl de::Error for Error {
34 fn custom<T: Display>(msg: T) -> Self {
35 Error::Message(msg.to_string())
36 }
37}
38
39impl Display for Error {
40 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
41 match self {
42 Error::Message(msg) => formatter.write_str(msg),
43 Error::Eof => formatter.write_str("unexpected end of input"),
44 Error::Syntax => formatter.write_str(
45 "syntax error. possible error: invalid integer or type byte, missing CRLF.",
46 ),
47 Error::Io(e) => formatter.write_str(&format!("an IO error occurred: {}", e)),
48 Error::FromUtf8(e) => {
49 formatter.write_str(&format!("an string conversion error occurred: {}", e))
50 }
51 }
52 }
53}
54
55impl std::error::Error for Error {}
56
57impl From<io::Error> for Error {
58 fn from(e: io::Error) -> Self {
59 match e.kind() {
60 ErrorKind::UnexpectedEof => Error::Eof,
61 _ => Error::Io(format!("{:?}", e)),
62 }
63 }
64}
65
66impl From<FromUtf8Error> for Error {
68 fn from(e: FromUtf8Error) -> Self {
69 Error::FromUtf8(format!("{:?}", e))
70 }
71}