Skip to main content

ferroui_base/data/
data_validation_exception.rs

1use crate::data::core::ValueTypes;
2use crate::BoxedValue;
3use std::fmt;
4
5/// An error that describes a data validation failure reported by a model
6/// object.
7#[derive(Clone)]
8pub struct DataValidationException {
9    error_data: Option<BoxedValue>,
10    message: String,
11}
12
13impl DataValidationException {
14    /// Creates the error from the data received from the validation source.
15    pub fn new(error_data: Option<BoxedValue>) -> Self {
16        let message = match &error_data {
17            Some(v) => ValueTypes::to_display_string(Some(v)),
18            None => String::new(),
19        };
20        Self { error_data, message }
21    }
22
23    /// The data of the validation error.
24    pub fn error_data(&self) -> Option<&BoxedValue> {
25        self.error_data.as_ref()
26    }
27}
28
29/// Two errors are equal if they carry the same error data, so that the error
30/// can be held in an untyped value.
31impl PartialEq for DataValidationException {
32    fn eq(&self, other: &Self) -> bool {
33        ValueTypes::identity_equals(self.error_data.as_ref(), other.error_data.as_ref())
34    }
35}
36
37impl fmt::Debug for DataValidationException {
38    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
39        write!(f, "DataValidationException({})", self.message)
40    }
41}
42
43impl fmt::Display for DataValidationException {
44    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
45        f.write_str(&self.message)
46    }
47}
48
49impl std::error::Error for DataValidationException {}
50
51/// Several validation errors reported for one property.
52#[derive(Clone, Debug)]
53pub struct AggregateException {
54    inner: Vec<DataValidationException>,
55}
56
57impl AggregateException {
58    pub fn new(inner: Vec<DataValidationException>) -> Self {
59        Self { inner }
60    }
61
62    pub fn inner_exceptions(&self) -> &[DataValidationException] {
63        &self.inner
64    }
65}
66
67impl fmt::Display for AggregateException {
68    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
69        f.write_str("One or more errors occurred.")?;
70        for e in &self.inner {
71            write!(f, " ({e})")?;
72        }
73        Ok(())
74    }
75}
76
77impl std::error::Error for AggregateException {}