rstats/error.rs
1use medians::MedError;
2use ran::RanError;
3use std::error::Error;
4use std::fmt;
5use std::fmt::{Debug, Display};
6use std::thread::AccessError;
7
8/// Shorthand type for returned errors with String (message) payload
9pub type RE = RError<String>;
10
11#[derive(Debug)]
12/// Custom RStats Error
13/// Parameter <T> is future proofing, so that any type of argument may be returned.
14/// Currently only messages of type <String> and <&str> are used
15pub enum RError<T>
16where
17 T: Sized + Debug,
18{
19 /// Error indicating that insufficient data has been supplied
20 NoDataError(T),
21 /// Error indicating that a wrong kind/size of data has been supplied
22 DataError(T),
23 /// Error indicating an invalid result, such as an attempt at division by zero
24 ArithError(T),
25 /// Other error converted to RError
26 OtherError(T),
27}
28
29impl<T> Error for RError<T> where T: Sized + Debug + Display {}
30
31impl<T> fmt::Display for RError<T>
32where
33 T: Sized + Debug + Display,
34{
35 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
36 match self {
37 RError::NoDataError(s) => write!(f, "Missing or insufficient data: {s}"),
38 RError::DataError(s) => write!(f, "Wrong data: {s}"),
39 RError::ArithError(s) => write!(f, "Arithmetic error: {s}"),
40 RError::OtherError(s) => write!(f, "Converted from {s}"),
41 }
42 }
43}
44/// Convenience function for building RError::NoDataError(String)
45/// from payload message, which can be either literal `&str` or `String`.
46/// `String` allows splicing in values of variables for debugging, using `format!`
47pub fn nodata_error<T>(msg: impl Into<String>) -> Result<T,RError<String>> {
48 Err(RError::NoDataError(msg.into()))
49}
50/// Convenience function for building RError::DataError(String)
51/// from payload message, which can be either literal `&str` or `String`.
52/// `String` allows splicing in values of variables for debugging, using `format!`
53pub fn data_error<T>(msg: impl Into<String>) -> Result<T,RError<String>> {
54 Err(RError::DataError(msg.into()))
55}
56/// Convenience function for building RError::ArithError(String)
57/// from payload message, which can be either literal `&str` or `String`.
58/// `String` allows splicing in values of variables for debugging, using `format!`
59pub fn arith_error<T>(msg: impl Into<String>) -> Result<T,RError<String>> {
60 Err(RError::ArithError(msg.into()))
61}
62/// Convenience function for building RError::ArithError(String)
63/// from payload message, which can be either literal `&str` or `String`.
64/// `String` allows splicing in values of variables for debugging, using `format!`
65pub fn other_error<T>(msg: impl Into<String>) -> Result<T,RError<String>> {
66 Err(RError::OtherError(msg.into()))
67}
68/*
69/// Convenience function for building RError<String>
70/// from short name and payload message, which can be either literal `&str` or `String`.
71/// `String` allows splicing in values of variables for debugging, using `format!`
72pub fn re_error<T>(kind: &str, msg: impl Into<String>) -> Result<T,RError<String>> {
73 match kind {
74 "empty" => Err(RError::NoDataError(msg.into())),
75 "size" => Err(RError::DataError(msg.into())),
76 "arith" => Err(RError::ArithError(msg.into())),
77 "other" => Err(RError::OtherError(msg.into())),
78 _ => Err(RError::OtherError("Wrong error kind given to re_error".into()))
79 }
80}
81*/
82/// Automatically converting any RanError to RError::OtherError
83impl From<RanError<String>> for RError<String> {
84 fn from(e: RanError<String>) -> Self {
85 RError::OtherError(format!("RanError: {e}"))
86 }
87}
88
89/// Automatically converting any MedError to RError::OtherError
90impl From<MedError<String>> for RError<String> {
91 fn from(e: MedError<String>) -> Self {
92 RError::OtherError(format!("MedError: {e}"))
93 }
94}
95
96/// Example 'From' implementation for converting to RError
97impl From<AccessError> for RError<String> {
98 fn from(e: AccessError) -> Self {
99 RError::OtherError(format!("AccessError: {e}"))
100 }
101}
102
103// 'From' implementation for converting ioerror to RError
104impl From<std::io::Error> for RError<String> {
105 fn from(e: std::io::Error) -> Self {
106 RError::OtherError(format!("IOError: {e}"))
107 }
108}