injectable_rs_graph/
error.rs1use crate::ValidationError;
4
5#[derive(Debug)]
7pub enum GraphError {
8 ValidationFailed(Vec<ValidationError>),
10}
11
12impl std::fmt::Display for GraphError {
13 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
14 match self {
15 Self::ValidationFailed(errors) => {
16 writeln!(f, "dependency graph validation failed:")?;
17 for err in errors {
18 writeln!(f, " - {err}")?;
19 }
20 Ok(())
21 }
22 }
23 }
24}
25
26impl std::error::Error for GraphError {}
27
28impl From<Vec<ValidationError>> for GraphError {
29 fn from(errors: Vec<ValidationError>) -> Self {
30 Self::ValidationFailed(errors)
31 }
32}
33
34#[cfg(test)]
35mod tests {
36 use super::*;
37 use crate::ValidationError;
38
39 #[test]
40 fn display_validation_failed_single_error() {
41 let err = GraphError::ValidationFailed(vec![ValidationError::DuplicateNode {
42 name: "Foo".to_string(),
43 }]);
44 let s = err.to_string();
45 assert!(s.contains("dependency graph validation failed"));
46 assert!(s.contains("Foo"));
47 }
48
49 #[test]
50 fn display_validation_failed_multiple_errors() {
51 let err = GraphError::ValidationFailed(vec![
52 ValidationError::DuplicateNode {
53 name: "A".to_string(),
54 },
55 ValidationError::DuplicateNode {
56 name: "B".to_string(),
57 },
58 ]);
59 let s = err.to_string();
60 assert!(s.contains("A"));
61 assert!(s.contains("B"));
62 }
63
64 #[test]
65 fn from_vec_creates_validation_failed() {
66 let errors = vec![ValidationError::DuplicateNode {
67 name: "X".to_string(),
68 }];
69 let err: GraphError = errors.into();
70 assert!(matches!(err, GraphError::ValidationFailed(_)));
71 }
72
73 #[test]
74 fn error_trait_impl() {
75 let err = GraphError::ValidationFailed(vec![]);
76 let _: &dyn std::error::Error = &err;
78 }
79
80 #[test]
81 fn debug_impl() {
82 let err = GraphError::ValidationFailed(vec![]);
83 let s = format!("{err:?}");
84 assert!(s.contains("ValidationFailed"));
85 }
86}