cairn_knowledge_graph/error/
logic_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 LogicError {
11 error_type: LogicErrorType,
12 explanation: String,
13 source: Option<LogicErrorSource>,
14}
15
16#[derive(Debug)]
17pub enum LogicErrorSource {
18 SparseLinearAlgebra(SparseLinearAlgebraError),
19}
20
21#[derive(Debug, Clone, PartialEq)]
22pub enum LogicErrorType {
23 SparseLinearAlgebra(SparseLinearAlgebraErrorType),
24 DimensionMismatch,
25 EdgeTypeAlreadyExists,
26 EdgeTypeMustExist,
27 IndexOutOfBounds,
28 VertexMustExist,
29 Other,
30}
31
32impl LogicError {
33 pub fn new(
34 error_type: LogicErrorType,
35 explanation: String,
36 source: Option<LogicErrorSource>,
37 ) -> Self {
38 Self {
39 error_type,
40 explanation,
41 source,
42 }
43 }
44
45 pub fn error_type(&self) -> LogicErrorType {
46 self.error_type.clone()
47 }
48 pub fn explanation(&self) -> String {
49 self.explanation.clone()
50 }
51}
52
53impl error::Error for LogicError {
54 fn source(&self) -> Option<&(dyn error::Error + 'static)> {
55 match self.source {
56 Some(ref error) => match error {
57 LogicErrorSource::SparseLinearAlgebra(error) => Some(error),
58 },
59 None => None,
60 }
61 }
62}
63
64impl fmt::Display for LogicError {
65 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
66 match &self.error_type {
67 _ => writeln!(f, "Context:\n{}", &self.explanation)?,
69 };
70
71 match &self.source() {
72 Some(err) => writeln!(f, "Source error:\n{}", err)?,
73 &None => (),
74 }
75 Ok(())
76 }
77}
78
79impl From<SparseLinearAlgebraError> for LogicError {
80 fn from(error: SparseLinearAlgebraError) -> Self {
81 Self {
82 error_type: LogicErrorType::SparseLinearAlgebra(error.error_type()),
83 explanation: String::new(),
84 source: Some(LogicErrorSource::SparseLinearAlgebra(error)),
85 }
86 }
87}