1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
use serde::{ser, de}; use std::{self, error, io, str, string}; use std::fmt::{self, Display}; #[derive(Debug)] pub struct Error { msg: String, } pub type Result<T> = std::result::Result<T, Error>; impl Error { pub fn new<T: Display>(msg: T) -> Self { Error { msg: msg.to_string() } } } impl error::Error for Error { fn description(&self) -> &str { &self.msg } } impl Display for Error { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { f.write_str(&self.msg) } } impl ser::Error for Error { fn custom<T: Display>(msg: T) -> Self { Error::new(msg) } } impl de::Error for Error { fn custom<T: Display>(msg: T) -> Self { Error::new(msg) } } impl From<io::Error> for Error { fn from(err: io::Error) -> Self { Error::new(err) } } impl From<str::Utf8Error> for Error { fn from(err: str::Utf8Error) -> Self { Error::new(err) } } impl From<string::FromUtf8Error> for Error { fn from(err: string::FromUtf8Error) -> Self { Error::new(err) } }