cairn_knowledge_graph/error/
other_error.rs1use std::error;
2use std::error::Error;
3use std::fmt;
4
5use graphblas_sparse_linear_algebra::error::{
6 SparseLinearAlgebraError, SparseLinearAlgebraErrorType,
7};
8
9#[derive(Debug)]
10pub struct OtherError {
11 error_type: OtherErrorType,
12 explanation: String,
13 source: Option<OtherErrorSource>,
14}
15
16#[derive(Debug)]
17pub enum OtherErrorSource {
18 Display(std::fmt::Error),
19 SparseLinearAlgebra(SparseLinearAlgebraError),
20}
21
22#[derive(Debug, Clone, PartialEq)]
23pub enum OtherErrorType {
24 Display,
25 Other,
26 SparseLinearAlgebra(SparseLinearAlgebraErrorType),
27}
28
29impl OtherError {
30 pub fn new(
31 error_type: OtherErrorType,
32 explanation: String,
33 source: Option<OtherErrorSource>,
34 ) -> Self {
35 Self {
36 error_type,
37 explanation,
38 source,
39 }
40 }
41
42 pub fn error_type(&self) -> OtherErrorType {
43 self.error_type.clone()
44 }
45 pub fn explanation(&self) -> String {
46 self.explanation.clone()
47 }
48}
49
50impl error::Error for OtherError {
51 fn source(&self) -> Option<&(dyn error::Error + 'static)> {
52 match self.source {
53 Some(ref error) => match error {
54 OtherErrorSource::Display(error) => Some(error),
55 OtherErrorSource::SparseLinearAlgebra(error) => Some(error),
56 },
57 None => None,
58 }
59 }
60}
61
62impl fmt::Display for OtherError {
63 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
64 match &self.error_type {
65 _ => writeln!(f, "Explanation:\n{}", &self.explanation)?,
67 };
68
69 match &self.source() {
70 Some(err) => writeln!(f, "Source error:\n{}", err)?,
71 &None => (),
72 }
73 Ok(())
74 }
75}
76
77impl From<std::fmt::Error> for OtherError {
78 fn from(error: std::fmt::Error) -> Self {
79 Self {
80 error_type: OtherErrorType::Display,
81 explanation: String::new(),
82 source: Some(OtherErrorSource::Display(error)),
83 }
84 }
85}
86
87impl From<SparseLinearAlgebraError> for OtherError {
88 fn from(error: SparseLinearAlgebraError) -> Self {
89 Self {
90 error_type: OtherErrorType::SparseLinearAlgebra(error.error_type()),
91 explanation: String::new(),
92 source: Some(OtherErrorSource::SparseLinearAlgebra(error)),
93 }
94 }
95}