dzahui/
error.rs

1use std::{num::{ParseIntError, ParseFloatError}, ffi::NulError, sync::mpsc::RecvError};
2
3#[derive(Debug)]
4/// # General Information
5/// 
6/// Error enum to use on all functions. Contains personalized errors with a corresponding explanation
7/// 
8/// # Arms
9/// 
10/// * `ExtensionNotAllowed` - Files like shaders, meshes or font maps need a specific extension. When the extension is not appropiate, this error is thrown
11/// * `ImageError` - Errors related to image reading and parsing
12/// * `ParseFloat` - Error while parsing a float
13/// * `ParseInt` - Error while parsing an int
14/// * `CharacterError` - Error while creating the character set for the window
15/// * `NotFound` - Error while looking for files
16/// * `NullCString` - Error while converting to c-types
17/// * `Matrix` - Errors ocurring while using matrices
18/// * `Parse` - Error while interpreting files
19/// * `Integration` - Error on numeric integration
20/// * `Io` - Error on IO operations
21/// * `MeshParse` - Error while parsing a mesh
22/// * `FloatConversion` - Error on float conversion betweeen f64 and f32
23/// * `Custom` - Less common errors
24/// * `PieceWiseDims` - Error while creating a piecewise function
25/// * `Unimplemented` - Error that should not exist
26/// * `Infallible` - Error that never happens
27/// * `WrongDims` - Error while operating on vectors and matrices
28/// * `Overflow` - Error when a number overflows
29/// * `Receiver` - Error on communication between threads
30/// * `Writing` - Error while writing to file values of equation
31/// 
32pub 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}