use medians::MedError;
use ran::RanError;
use std::error::Error;
use std::fmt;
use std::fmt::{Debug, Display};
use std::thread::AccessError;
pub type RE = RError<String>;
#[derive(Debug)]
pub enum RError<T>
where
T: Sized + Debug,
{
NoDataError(T),
DataError(T),
ArithError(T),
OtherError(T),
}
impl<T> Error for RError<T> where T: Sized + Debug + Display {}
impl<T> fmt::Display for RError<T>
where
T: Sized + Debug + Display,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
RError::NoDataError(s) => write!(f, "Missing or insufficient data: {s}"),
RError::DataError(s) => write!(f, "Wrong data: {s}"),
RError::ArithError(s) => write!(f, "Arithmetic error: {s}"),
RError::OtherError(s) => write!(f, "Converted from {s}"),
}
}
}
pub fn nodata_error<T>(msg: impl Into<String>) -> Result<T,RError<String>> {
Err(RError::NoDataError(msg.into()))
}
pub fn data_error<T>(msg: impl Into<String>) -> Result<T,RError<String>> {
Err(RError::DataError(msg.into()))
}
pub fn arith_error<T>(msg: impl Into<String>) -> Result<T,RError<String>> {
Err(RError::ArithError(msg.into()))
}
pub fn other_error<T>(msg: impl Into<String>) -> Result<T,RError<String>> {
Err(RError::OtherError(msg.into()))
}
impl From<RanError<String>> for RError<String> {
fn from(e: RanError<String>) -> Self {
RError::OtherError(format!("RanError: {e}"))
}
}
impl From<MedError<String>> for RError<String> {
fn from(e: MedError<String>) -> Self {
RError::OtherError(format!("MedError: {e}"))
}
}
impl From<AccessError> for RError<String> {
fn from(e: AccessError) -> Self {
RError::OtherError(format!("AccessError: {e}"))
}
}
impl From<std::io::Error> for RError<String> {
fn from(e: std::io::Error) -> Self {
RError::OtherError(format!("IOError: {e}"))
}
}