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 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
use medians::MedError;
use ran::RanError;
use std::error::Error;
use std::fmt;
use std::fmt::{Debug, Display};
use std::thread::AccessError;
/// Shorthand type for returned errors with String (message) payload
pub type RE = RError<String>;
#[derive(Debug)]
/// Custom RStats Error
/// Parameter <T> is future proofing, so that any type of argument may be returned.
/// Currently only messages of type <String> and <&str> are used
pub enum RError<T>
where
T: Sized + Debug,
{
/// Error indicating that insufficient data has been supplied
NoDataError(T),
/// Error indicating that a wrong kind/size of data has been supplied
DataError(T),
/// Error indicating an invalid result, such as an attempt at division by zero
ArithError(T),
/// Other error converted to RError
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}"),
}
}
}
/// Convenience function for building RError::NoDataError(String)
/// from payload message, which can be either literal `&str` or `String`.
/// `String` allows splicing in values of variables for debugging, using `format!`
pub fn nodata_error<T>(msg: impl Into<String>) -> Result<T,RError<String>> {
Err(RError::NoDataError(msg.into()))
}
/// Convenience function for building RError::DataError(String)
/// from payload message, which can be either literal `&str` or `String`.
/// `String` allows splicing in values of variables for debugging, using `format!`
pub fn data_error<T>(msg: impl Into<String>) -> Result<T,RError<String>> {
Err(RError::DataError(msg.into()))
}
/// Convenience function for building RError::ArithError(String)
/// from payload message, which can be either literal `&str` or `String`.
/// `String` allows splicing in values of variables for debugging, using `format!`
pub fn arith_error<T>(msg: impl Into<String>) -> Result<T,RError<String>> {
Err(RError::ArithError(msg.into()))
}
/// Convenience function for building RError::ArithError(String)
/// from payload message, which can be either literal `&str` or `String`.
/// `String` allows splicing in values of variables for debugging, using `format!`
pub fn other_error<T>(msg: impl Into<String>) -> Result<T,RError<String>> {
Err(RError::OtherError(msg.into()))
}
/*
/// Convenience function for building RError<String>
/// from short name and payload message, which can be either literal `&str` or `String`.
/// `String` allows splicing in values of variables for debugging, using `format!`
pub fn re_error<T>(kind: &str, msg: impl Into<String>) -> Result<T,RError<String>> {
match kind {
"empty" => Err(RError::NoDataError(msg.into())),
"size" => Err(RError::DataError(msg.into())),
"arith" => Err(RError::ArithError(msg.into())),
"other" => Err(RError::OtherError(msg.into())),
_ => Err(RError::OtherError("Wrong error kind given to re_error".into()))
}
}
*/
/// Automatically converting any RanError to RError::OtherError
impl From<RanError<String>> for RError<String> {
fn from(e: RanError<String>) -> Self {
RError::OtherError(format!("RanError: {e}"))
}
}
/// Automatically converting any MedError to RError::OtherError
impl From<MedError<String>> for RError<String> {
fn from(e: MedError<String>) -> Self {
RError::OtherError(format!("MedError: {e}"))
}
}
/// Example 'From' implementation for converting to RError
impl From<AccessError> for RError<String> {
fn from(e: AccessError) -> Self {
RError::OtherError(format!("AccessError: {e}"))
}
}
// 'From' implementation for converting ioerror to RError
impl From<std::io::Error> for RError<String> {
fn from(e: std::io::Error) -> Self {
RError::OtherError(format!("IOError: {e}"))
}
}