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
use medians::error::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}"),
}
}
}
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}"))
}
}
pub fn re_error(kind: &str, msg: impl Into<String>) -> RE {
match kind {
"empty" => RError::NoDataError(msg.into()),
"size" => RError::DataError(msg.into()),
"arith" => RError::ArithError(msg.into()),
"other" => RError::OtherError(msg.into()),
_ => RError::OtherError("Wrong error kind given to re_error".into())
}
}