compose_validatr/
errors.rs1#[derive(Debug)]
4pub enum ValidationError {
5 MissingField(String),
6 InvalidValue(String),
7 InvalidCompose(String),
8}
9
10impl std::fmt::Display for ValidationError {
11 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
12 match self {
13 ValidationError::MissingField(field) => write!(f, "Missing field: {}", field),
14 ValidationError::InvalidValue(value) => write!(f, "Invalid value: {}", value),
15 ValidationError::InvalidCompose(value) => write!(f, "Invalid compose file: {}", value),
16 }
17 }
18}
19
20impl std::error::Error for ValidationError {}
21
22#[derive(Debug)]
23pub struct ValidationErrors {
24 errors: Vec<ValidationError>,
25}
26
27impl ValidationErrors {
28 pub fn new() -> Self {
29 ValidationErrors { errors: Vec::new() }
30 }
31
32 pub fn add_error(&mut self, error: ValidationError) {
33 self.errors.push(error);
34 }
35
36 pub fn has_errors(&self) -> bool {
37 !self.errors.is_empty()
38 }
39
40 pub fn all_errors(&self) -> &[ValidationError] {
41 &self.errors
42 }
43}
44
45impl std::fmt::Display for ValidationErrors {
46 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
47 for error in &self.errors {
48 writeln!(f, "{}", error)?;
49 }
50 Ok(())
51 }
52}
53
54impl std::error::Error for ValidationErrors {}