fj_core/validation/
validation.rs

1use std::{collections::HashMap, error::Error, thread};
2
3use crate::storage::ObjectId;
4
5use super::{ValidationConfig, ValidationError};
6
7/// Errors that occurred while validating the objects inserted into the stores
8#[derive(Default)]
9pub struct Validation {
10    /// All unhandled validation errors
11    pub errors: HashMap<ObjectId, ValidationError>,
12
13    /// Validation configuration for the validation service
14    pub config: ValidationConfig,
15}
16
17impl Validation {
18    /// Construct an instance of `Validation`, using the provided configuration
19    pub fn with_validation_config(config: ValidationConfig) -> Self {
20        let errors = HashMap::new();
21        Self { errors, config }
22    }
23}
24
25impl Drop for Validation {
26    fn drop(&mut self) {
27        let num_errors = self.errors.len();
28        if num_errors > 0 {
29            println!(
30                "Dropping `Validation` with {num_errors} unhandled validation \
31                errors:"
32            );
33
34            for err in self.errors.values() {
35                println!("{}", err);
36
37                // Once `Report` is stable, we can replace this:
38                // https://doc.rust-lang.org/std/error/struct.Report.html
39                let mut source = err.source();
40                while let Some(err) = source {
41                    println!("\nCaused by:\n\t{err}");
42                    source = err.source();
43                }
44
45                print!("\n\n");
46            }
47
48            if !thread::panicking() {
49                panic!();
50            }
51        }
52    }
53}