1use std::{num::{ParseIntError, ParseFloatError}, ffi::NulError, sync::mpsc::RecvError};
2
3#[derive(Debug)]
4pub enum Error {
33 ExtensionNotAllowed(String, String),
34 ImageError(image::ImageError),
35 ParseFloat(ParseFloatError),
36 ParseInt(ParseIntError),
37 CharacterError(String),
38 BoundaryError(String),
39 NotFound(&'static str),
40 NullCString(NulError),
41 Matrix(&'static str),
42 Parse(&'static str),
43 Integration(String),
44 Io(std::io::Error),
45 MeshParse(String),
46 FloatConversion,
47 Custom(String),
48 PieceWiseDims,
49 Unimplemented,
50 Infallible,
51 WrongDims,
52 Overflow,
53 Receiver(RecvError),
54 Writing,
55}
56
57impl std::fmt::Display for Error {
58 fn fmt(&self, formatter: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
59 let content = match self {
60 Error::CharacterError(s) => format!("Error while using character set: {}",s),
61 Error::NullCString(e) => format!("Error while interacting with OpenGL: {}",e),
62 Error::FloatConversion => format!("Unable to convert between f32 and f64"),
63 Error::Infallible => format!("This error can not happen"),
64 Error::Matrix(s) => format!("Matrix operation failed {}",s),
65 Error::MeshParse(s) => format!("Unable to parse mesh file: {}",s),
66 Error::ParseFloat(e) => format!("ParseFloat error: {}",e),
67 Error::ParseInt(e) => format!("ParseInt error: {}",e),
68 Error::NotFound(file) => format!("Could not find file: {}",file),
69 Error::BoundaryError(e) => format!("Boundary error: {}",e),
70 Error::Io(e) => format!("IO error: {}", e),
71 Error::ImageError(e) => format!("Image error: {}",e),
72 Error::WrongDims => {
73 format!("One or more of the provided elements do not have the correct dimensions")
74 }
75 Error::Custom(e) => format!("{}", e),
76 Error::ExtensionNotAllowed(file, action) => {
77 format!("Extension of file {} is not allowed for {}", file, action)
78 }
79 Error::Overflow => String::from("Overflow occurred"),
80 Error::Parse(e) => format!("Error while parsing file: {}", e),
81 Error::PieceWiseDims => format!("Number of arguments must be one more than number of breakpoints for a piecewise function definition to make sense"),
82 Error::Unimplemented => {
83 format!("This error should not exist. Report it to the developer")
84 },
85 Error::Integration(e) => format!("Error on integration method occurred: {}",e),
86 Error::Writing => format!("Error while writing to file values of differential equation"),
87 Error::Receiver(e) => format!("No message received on thread: {}",e)
88 };
89 write!(formatter, "{}", content)
90 }
91}
92
93impl std::error::Error for Error {}
94
95impl Error {
96 pub fn custom<A: Into<String>>(message: A) -> Self {
97 Error::Custom(message.into())
98 }
99}
100
101impl From<std::io::Error> for Error {
102 fn from(source: std::io::Error) -> Self {
103 Error::Io(source)
104 }
105}
106
107impl From<image::ImageError> for Error {
108 fn from(source: image::ImageError) -> Self {
109 Error::ImageError(source)
110 }
111}
112
113impl From<ParseIntError> for Error {
114 fn from(source: ParseIntError) -> Self {
115 Error::ParseInt(source)
116 }
117}
118
119impl From<ParseFloatError> for Error {
120 fn from(source: ParseFloatError) -> Self {
121 Error::ParseFloat(source)
122 }
123}
124
125impl From<NulError> for Error {
126 fn from(source: NulError) -> Self {
127 Error::NullCString(source)
128 }
129}
130
131impl From<RecvError> for Error {
132 fn from(source: RecvError) -> Self {
133 Error::Receiver(source)
134 }
135}