use std::fmt;
use std::error;
use std::result;
use std::ops::Deref;
use std::num::{ ParseIntError, ParseFloatError };
use std::str::Utf8Error;
use std::string::FromUtf8Error;
use syn::synom::ParseError;
macro_rules! err_fmt {
($($arg:tt)*) => { Err(crate::error::Error::new(format!($($arg)*))) }
}
pub fn err_msg<T>(message: &str) -> Result<T> {
Err(Error::new(message))
}
pub type Result<T> = result::Result<T, Error>;
#[derive(Debug)]
pub struct Error {
message: String,
cause: Option<Box<dyn error::Error + 'static>>,
}
impl Error {
pub fn new<T: Into<String>>(message: T) -> Self {
Error {
message: message.into(),
cause: None,
}
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self.cause {
Some(ref cause) => write!(f, "{}: {}", self.message, cause),
None => self.message.fmt(f),
}
}
}
impl error::Error for Error {
fn description(&self) -> &str {
&self.message
}
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
self.cause.as_ref().map(Deref::deref)
}
}
macro_rules! impl_error {
($($ty:ident => $message:expr;)*) => {$(
impl From<$ty> for Error {
fn from(error: $ty) -> Self {
Error {
message: String::from($message),
cause: Some(Box::new(error)),
}
}
}
)*}
}
impl_error! {
ParseError => "could not parse derive input";
Utf8Error => "byte string is not valid UTF-8";
FromUtf8Error => "byte string is not valid UTF-8";
ParseIntError => "string does not represent an integer";
ParseFloatError => "string does not represent a floating-point number";
}