rust_fuzzylogic/
error.rs

1//This File Defines The Basic Error Handling(Empty Input, Bad Arity,,, etc)
2use std::error::Error;
3use std::fmt;
4
5//Basic Result-Type Definition For the functions in the library
6pub type Result<T> = std::result::Result<T, FuzzyError>;
7
8#[derive(Debug, Clone, PartialEq, Eq, Hash)]
9#[non_exhaustive]
10///Basic errors that can occur in the rust-fuzzylogic library
11pub enum FuzzyError {
12    BadArity,
13    EmptyInput,
14    TypeMismatch,
15    OutOfBounds,
16    NotFound { space: MissingSpace, key: String },
17}
18
19#[derive(Debug, Clone, PartialEq, Eq, Hash)]
20pub enum MissingSpace {
21    Var,
22    Input,
23}
24
25impl fmt::Display for FuzzyError {
26    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
27        match self {
28            FuzzyError::BadArity => {
29                write!(f, "Bad arity")
30            }
31            FuzzyError::EmptyInput => {
32                write!(f, "Empty input")
33            }
34            FuzzyError::TypeMismatch => {
35                write!(f, "Invalid type input")
36            }
37            FuzzyError::OutOfBounds => {
38                write!(f, "Out of bounds")
39            }
40            FuzzyError::NotFound { space, key } => {
41                write!(
42                    f,
43                    "Keys not found. {key} cannot be found in {}",
44                    match space {
45                        MissingSpace::Input => "Inputs",
46                        MissingSpace::Var => "Vars",
47                    }
48                )
49            }
50        }
51    }
52}
53
54impl Error for FuzzyError {}
55
56//Basic Unit Tests
57#[cfg(test)]
58mod tests {
59    use crate::error::FuzzyError;
60    #[test]
61    fn print_error() {
62        assert_eq!(FuzzyError::BadArity.to_string(), "Bad arity");
63        assert_eq!(FuzzyError::EmptyInput.to_string(), "Empty input");
64        assert_eq!(FuzzyError::TypeMismatch.to_string(), "Invalid type input");
65        assert_eq!(FuzzyError::OutOfBounds.to_string(), "Out of bounds");
66    }
67}