use crate::impl_from;
use std::{
error::Error,
fmt::{self, Display, Formatter},
};
#[derive(Debug)]
pub enum ErrorKind {
NotImplemented,
InvalidChartShape(String),
InvalidLane(usize, usize),
ReqwestError(reqwest::Error),
}
pub type Result<T> = std::result::Result<T, ErrorKind>;
impl Error for ErrorKind {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Self::ReqwestError(e) => Some(e),
_ => None,
}
}
}
impl Display for ErrorKind {
fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
match self {
ErrorKind::NotImplemented => write!(fmt, "Not implemented! This is probably a bug."),
ErrorKind::InvalidChartShape(s) => write!(
fmt,
"Chart shape \"{}\" not supported! this is almost certainly a bug!",
s
),
ErrorKind::InvalidLane(lane, max) => {
write!(fmt, "Element's lane {} is invalid! (max {})!", lane, max)
}
_ => write!(fmt, "Some error has occurred"),
}
}
}
impl_from!(reqwest::Error, ErrorKind::ReqwestError);
#[doc(hidden)]
#[macro_export]
macro_rules! impl_from {
($from:path, $to:expr) => {
impl From<$from> for ErrorKind {
fn from(e: $from) -> Self {
$to(e)
}
}
};
}