Skip to main content

graphrecords_core/errors/
graph.rs

1use std::{
2    error::Error,
3    fmt::{Display, Formatter, Result},
4};
5
6#[derive(Debug)]
7pub enum GraphError {
8    IndexError(String),
9    AssertionError(String),
10    SchemaError(String),
11}
12
13impl Error for GraphError {
14    fn description(&self) -> &str {
15        match self {
16            Self::IndexError(message)
17            | Self::AssertionError(message)
18            | Self::SchemaError(message) => message,
19        }
20    }
21}
22
23impl Display for GraphError {
24    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
25        match self {
26            Self::IndexError(message) => write!(f, "IndexError: {message}"),
27            Self::AssertionError(message) => write!(f, "AssertionError: {message}"),
28            Self::SchemaError(message) => write!(f, "SchemaError: {message}"),
29        }
30    }
31}
32
33#[cfg(test)]
34mod test {
35    use super::GraphError;
36
37    #[test]
38    fn test_display() {
39        assert_eq!(
40            "IndexError: value",
41            GraphError::IndexError("value".to_string()).to_string()
42        );
43        assert_eq!(
44            "AssertionError: value",
45            GraphError::AssertionError("value".to_string()).to_string()
46        );
47        assert_eq!(
48            "SchemaError: value",
49            GraphError::SchemaError("value".to_string()).to_string()
50        );
51    }
52}