Skip to main content

deep_causality/errors/
causality_error.rs

1/*
2 * SPDX-License-Identifier: MIT
3 * Copyright (c) 2023 - 2026. The DeepCausality Authors and Contributors. All Rights Reserved.
4 */
5
6use crate::CausalityGraphError;
7use deep_causality_uncertain::UncertainError;
8use std::error::Error;
9use std::fmt;
10use ultragraph::GraphError;
11
12#[derive(Debug, Clone, PartialOrd, PartialEq)]
13pub struct CausalityError(pub String);
14
15impl CausalityError {
16    pub fn new(field0: String) -> Self {
17        Self(field0)
18    }
19}
20
21impl Error for CausalityError {}
22
23impl fmt::Display for CausalityError {
24    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
25        write!(f, "CausalityError: {}", self.0)
26    }
27}
28
29impl From<GraphError> for CausalityError {
30    fn from(err: GraphError) -> Self {
31        // Convert the specific graph error into a descriptive string
32        // and wrap it in our custom error type.
33        CausalityError(format!("Graph operation failed: {err}"))
34    }
35}
36
37impl From<CausalityGraphError> for CausalityError {
38    fn from(err: CausalityGraphError) -> Self {
39        CausalityError(format!("{err}"))
40    }
41}
42
43impl From<UncertainError> for CausalityError {
44    fn from(err: UncertainError) -> Self {
45        CausalityError(format!("{err}"))
46    }
47}
48
49impl From<CausalityError> for deep_causality_core::CausalityError {
50    fn from(err: CausalityError) -> Self {
51        deep_causality_core::CausalityError::new(deep_causality_core::CausalityErrorEnum::Custom(
52            err.0,
53        ))
54    }
55}