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    Term,
24}
25
26impl fmt::Display for FuzzyError {
27    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
28        match self {
29            FuzzyError::BadArity => {
30                write!(f, "Bad arity")
31            }
32            FuzzyError::EmptyInput => {
33                write!(f, "Empty input")
34            }
35            FuzzyError::TypeMismatch => {
36                write!(f, "Invalid type input")
37            }
38            FuzzyError::OutOfBounds => {
39                write!(f, "Out of bounds")
40            }
41            FuzzyError::NotFound { space, key } => {
42                write!(
43                    f,
44                    "Keys not found. {key} cannot be found in {}",
45                    match space {
46                        MissingSpace::Input => "Inputs",
47                        MissingSpace::Var => "Vars",
48                        MissingSpace::Term => "Terms",
49                    }
50                )
51            }
52        }
53    }
54}
55
56impl Error for FuzzyError {}
57
58//Basic Unit Tests
59#[cfg(test)]
60mod tests {
61    use crate::error::FuzzyError;
62    #[test]
63    fn print_error() {
64        assert_eq!(FuzzyError::BadArity.to_string(), "Bad arity");
65        assert_eq!(FuzzyError::EmptyInput.to_string(), "Empty input");
66        assert_eq!(FuzzyError::TypeMismatch.to_string(), "Invalid type input");
67        assert_eq!(FuzzyError::OutOfBounds.to_string(), "Out of bounds");
68    }
69}