1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// Look here for an example on how to implement error types: https://doc.rust-lang.org/src/std/io/error.rs.html#42
use std::error;
use std::fmt;
// use std::io;
use std::error::Error;

#[derive(Debug, Clone, PartialEq)]
pub struct GraphblasError {
    error_type: GraphblasErrorType,
    explanation: String,
    // source: Option<Box<(dyn error::Error)>>,
}

#[derive(Debug, Clone, PartialEq)]
pub enum GraphblasErrorType {
    NoValue,
    UnitializedObject,
    InvalidObject,
    NotImplemented,
    NullPointer,
    InvalidValue,
    InvalidIndex,
    DomainMismatch,
    DimensionMismatch,
    EmptyObject,
    OutputNotEmpty,
    OutOfMemory,
    InsufficientSpace,
    IndexOutOfBounds,
    IteratorExhausted,
    Panic,
}

impl GraphblasError {
    pub fn new(error_type: GraphblasErrorType, explanation: String) -> Self {
        Self {
            error_type: error_type,
            explanation: explanation,
        }
    }

    pub fn error_type(&self) -> GraphblasErrorType {
        self.error_type.to_owned()
    }
    pub fn explanation(&self) -> String {
        self.explanation.to_owned()
    }
}

impl error::Error for GraphblasError {
    fn source(&self) -> Option<&(dyn error::Error + 'static)> {
        match &self.error_type {
            // ErrorType::IO(error) => Some(error),
            _ => None,
        }
    }
}

impl fmt::Display for GraphblasError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        // REVIEW: is this match useful?
        match &self.error_type {
            // ErrorType::IO(_err) => writeln!(f, "Context:\n{}", &self.context)?,
            _ => writeln!(f, "Explanation:\n{}", &self.explanation)?,
        };

        match &self.source() {
            Some(err) => writeln!(f, "Source error:\n{}", err)?,
            &None => (),
        }
        Ok(())
    }
}