1use strum::{Display, EnumCount, EnumIs, EnumIter, EnumString, VariantNames};
6
7pub type GraphResult<T = ()> = std::result::Result<T, GraphError>;
8
9#[derive(Clone, Debug, Display, EnumCount, EnumIs, VariantNames)]
10#[cfg_attr(
11 feature = "serde",
12 derive(serde::Deserialize, serde::Serialize),
13 serde(rename_all = "snake_case", untagged)
14)]
15#[repr(usize)]
16#[strum(serialize_all = "snake_case")]
17pub enum GraphError {
18 Cycle(CycleError),
19 Unknown(String),
20}
21
22unsafe impl Send for GraphError {}
23
24unsafe impl Sync for GraphError {}
25
26impl std::error::Error for GraphError {}
27
28impl From<&str> for GraphError {
29 fn from(error: &str) -> Self {
30 GraphError::Unknown(error.to_string())
31 }
32}
33
34impl From<String> for GraphError {
35 fn from(error: String) -> Self {
36 GraphError::Unknown(error)
37 }
38}
39
40impl<Idx> From<petgraph::algo::Cycle<Idx>> for GraphError
41where
42 Idx: Copy + core::fmt::Debug,
43{
44 fn from(error: petgraph::algo::Cycle<Idx>) -> Self {
45 GraphError::Cycle(CycleError::Cycle {
46 id: format!("{:?}", error.node_id()),
47 })
48 }
49}
50
51impl From<petgraph::algo::NegativeCycle> for GraphError {
52 fn from(_error: petgraph::algo::NegativeCycle) -> Self {
53 GraphError::Cycle(CycleError::NegativeCylce)
54 }
55}
56
57#[derive(Clone, Debug, Display, EnumCount, EnumIs, EnumIter, EnumString, VariantNames)]
58#[cfg_attr(
59 feature = "serde",
60 derive(serde::Deserialize, serde::Serialize),
61 serde(rename_all = "snake_case", untagged)
62)]
63#[repr(usize)]
64#[strum(serialize_all = "snake_case")]
65pub enum CycleError {
66 Cycle { id: String },
67 NegativeCylce,
68}
69
70macro_rules! into_error {
71 ($error:ident, $kind:ident) => {
72 impl From<$error> for GraphError {
73 fn from(error: $error) -> Self {
74 GraphError::$kind(error)
75 }
76 }
77 };
78}
79
80into_error!(CycleError, Cycle);