Skip to main content

weavatrix_graph/
error.rs

1use std::fmt::{Display, Formatter};
2
3#[derive(Debug, Clone, PartialEq, Eq)]
4#[non_exhaustive]
5pub enum GraphError {
6    EmptyNodeId,
7    InvalidKind {
8        category: &'static str,
9        value: String,
10    },
11    ConflictingNode {
12        id: String,
13    },
14    MissingEdgeSource {
15        id: String,
16    },
17    MissingEdgeTarget {
18        id: String,
19    },
20    EmptyExtractor,
21    InvalidSpan {
22        file: String,
23        reason: &'static str,
24    },
25}
26
27impl Display for GraphError {
28    fn fmt(&self, formatter: &mut Formatter<'_>) -> std::fmt::Result {
29        match self {
30            Self::EmptyNodeId => formatter.write_str("node id must not be empty"),
31            Self::InvalidKind { category, value } => {
32                write!(formatter, "invalid {category} kind: {value:?}")
33            }
34            Self::ConflictingNode { id } => {
35                write!(formatter, "node id {id} has conflicting definitions")
36            }
37            Self::MissingEdgeSource { id } => {
38                write!(formatter, "edge source does not exist: {id}")
39            }
40            Self::MissingEdgeTarget { id } => {
41                write!(formatter, "edge target does not exist: {id}")
42            }
43            Self::EmptyExtractor => formatter.write_str("provenance extractor must not be empty"),
44            Self::InvalidSpan { file, reason } => {
45                write!(formatter, "invalid source span for {file:?}: {reason}")
46            }
47        }
48    }
49}
50
51impl std::error::Error for GraphError {}
52
53pub type Result<T> = std::result::Result<T, GraphError>;