sdf_metadata/util/
validation_failure.rs1use crate::util::config_error::INDENT;
2
3use super::{config_error::ConfigError, validation_error::ValidationError};
4
5#[derive(Debug, Default, Clone, Eq)]
6pub struct ValidationFailure {
7 pub errors: Vec<ValidationError>,
8}
9
10impl ValidationFailure {
11 pub fn new() -> Self {
12 Default::default()
13 }
14
15 pub fn any(&self) -> bool {
16 !self.errors.is_empty()
17 }
18
19 pub fn push_str(&mut self, msg: &str) {
20 self.errors.push(ValidationError::new(msg));
21 }
22
23 pub fn push(&mut self, validation_error: &ValidationError) {
24 self.errors.push(validation_error.clone());
25 }
26
27 pub fn concat(&mut self, other: &Self) {
28 self.errors.extend(other.errors.iter().cloned());
29 }
30
31 pub fn concat_with_context(&mut self, context: &str, other: &Self) {
32 for ValidationError { msg } in other.errors.iter() {
33 self.errors
34 .push(ValidationError::new(&format!("{} {}", context, msg)));
35 }
36 }
37}
38
39impl ConfigError for ValidationFailure {
40 fn readable(&self, indent: usize) -> String {
41 self.errors
42 .iter()
43 .map(|error| format!("{}{}\n", INDENT.repeat(indent), error.msg))
44 .collect::<Vec<String>>()
45 .join("")
46 }
47}
48
49impl From<&str> for ValidationFailure {
50 fn from(msg: &str) -> Self {
51 let mut errors = ValidationFailure::new();
52
53 errors.push_str(msg);
54
55 errors
56 }
57}
58
59impl PartialEq for ValidationFailure {
60 fn eq(&self, other: &Self) -> bool {
61 let mut errors = self.errors.clone();
62 let mut other_errors = other.errors.clone();
63
64 errors.sort();
65 other_errors.sort();
66
67 errors == other_errors
68 }
69}
70
71impl std::fmt::Display for ValidationFailure {
72 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
73 for error in &self.errors {
74 writeln!(f, "{}", error.msg)?;
75 }
76
77 Ok(())
78 }
79}