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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
use std::error::Error;
use std::fmt;
use std::num::TryFromIntError;
use std::{error, sync::PoisonError};

use super::graphblas_error::{GraphblasError, GraphblasErrorType};

#[derive(Debug)]
pub struct SystemError {
    error_type: SystemErrorType,
    explanation: String,
    source: Option<SystemErrorSource>,
}

#[derive(Debug)]
pub enum SystemErrorSource {
    GraphBLAS(GraphblasError),
    IntegerConversionError(TryFromIntError),
    PoisonedData,
}

#[derive(Debug, Clone, PartialEq)]
pub enum SystemErrorType {
    GraphBLAS(GraphblasErrorType),
    CreateGraphBlasErrorOnSuccessValue,
    ContextAlreadyInitialized,
    IndexOutOfBounds,
    UninitialisedContext,
    UnsupportedGraphBlasErrorValue,
    UnsupportedArchitecture,
    PoisonedData,
    IntegerConversionFailed,
    Other,
}

impl SystemError {
    pub fn new(
        error_type: SystemErrorType,
        explanation: String,
        source: Option<SystemErrorSource>,
    ) -> Self {
        Self {
            error_type,
            explanation,
            source,
        }
    }

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

impl error::Error for SystemError {
    fn source(&self) -> Option<&(dyn error::Error + 'static)> {
        match self.source {
            Some(ref error) => match error {
                SystemErrorSource::GraphBLAS(error) => Some(error),
                SystemErrorSource::IntegerConversionError(error) => Some(error),
                SystemErrorSource::PoisonedData => None,
            },
            None => None,
        }
    }
}

impl fmt::Display for SystemError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match &self.error_type {
            _ => writeln!(f, "Explanation:\n{}", &self.explanation)?,
        };

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

impl From<GraphblasError> for SystemError {
    fn from(error: GraphblasError) -> Self {
        Self {
            error_type: SystemErrorType::GraphBLAS(error.error_type()),
            explanation: String::new(),
            source: Some(SystemErrorSource::GraphBLAS(error)),
        }
    }
}

impl From<TryFromIntError> for SystemError {
    fn from(error: TryFromIntError) -> Self {
        Self {
            error_type: SystemErrorType::IntegerConversionFailed,
            explanation: String::new(),
            source: Some(SystemErrorSource::IntegerConversionError(error)),
        }
    }
}

impl<T> From<PoisonError<T>> for SystemError {
    fn from(error: PoisonError<T>) -> Self {
        Self {
            error_type: SystemErrorType::PoisonedData,
            explanation: format!("{:?}", error),
            source: Some(SystemErrorSource::PoisonedData),
        }
    }
}