1use thiserror::Error;
2
3#[derive(Debug, Error)]
4pub enum GraphifyError {
5 #[error("invalid node: {0}")]
6 InvalidNode(String),
7
8 #[error("invalid edge: {0}")]
9 InvalidEdge(String),
10
11 #[error("duplicate node with id `{0}`")]
12 DuplicateNode(String),
13
14 #[error("node not found: `{0}`")]
15 NodeNotFound(String),
16
17 #[error("IO error: {0}")]
18 IoError(#[from] std::io::Error),
19
20 #[error("serialization error: {0}")]
21 SerializationError(#[from] serde_json::Error),
22
23 #[error("graph error: {0}")]
24 GraphError(String),
25}
26
27pub type Result<T> = std::result::Result<T, GraphifyError>;
28
29#[cfg(test)]
30mod tests {
31 use super::*;
32
33 #[test]
34 fn error_display() {
35 let e = GraphifyError::InvalidNode("bad".into());
36 assert_eq!(e.to_string(), "invalid node: bad");
37 }
38
39 #[test]
40 fn error_from_serde() {
41 let raw = "not json";
42 let err: std::result::Result<serde_json::Value, _> = serde_json::from_str(raw);
43 let g: GraphifyError = err.unwrap_err().into();
44 assert!(matches!(g, GraphifyError::SerializationError(_)));
45 }
46
47 #[test]
48 fn error_from_io() {
49 let io = std::io::Error::new(std::io::ErrorKind::NotFound, "missing");
50 let g: GraphifyError = io.into();
51 assert!(matches!(g, GraphifyError::IoError(_)));
52 }
53
54 #[test]
55 fn duplicate_node_display() {
56 let e = GraphifyError::DuplicateNode("abc".into());
57 assert_eq!(e.to_string(), "duplicate node with id `abc`");
58 }
59}