1use std::{
2 error::Error,
3 fmt::{self, Debug, Display},
4 ops::Deref,
5};
6
7#[derive(Debug, Eq, PartialEq)]
9#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
10#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
11pub struct ValidationErrors<E>(Vec<E>);
12
13impl<E> Deref for ValidationErrors<E> {
14 type Target = Vec<E>;
15
16 fn deref(&self) -> &Self::Target {
17 &self.0
18 }
19}
20
21impl<E: Debug> Display for ValidationErrors<E> {
22 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
23 write!(f, "{:?}", self.0)
24 }
25}
26
27impl<E: Error> Error for ValidationErrors<E> {}
28
29impl<E> FromIterator<E> for ValidationErrors<E> {
30 fn from_iter<T: IntoIterator<Item = E>>(iter: T) -> Self {
31 Self(Vec::from_iter(iter))
32 }
33}
34
35impl<E> From<Vec<E>> for ValidationErrors<E> {
36 fn from(value: Vec<E>) -> Self {
37 Self(value)
38 }
39}
40
41impl<E> IntoIterator for ValidationErrors<E> {
42 type Item = E;
43 type IntoIter = std::vec::IntoIter<Self::Item>;
44
45 fn into_iter(self) -> Self::IntoIter {
46 self.0.into_iter()
47 }
48}
49
50#[derive(Debug, Eq, PartialEq)]
52#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
53#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
54pub struct IndexedValidationError<E: Error> {
55 pub index: usize,
57
58 #[cfg_attr(feature = "serde", serde(flatten))]
60 pub error: E,
61}
62
63impl<E: Error> IndexedValidationError<E> {
64 pub fn new(index: usize, error: E) -> Self {
66 Self { index, error }
67 }
68}
69
70impl<E: Error> Display for IndexedValidationError<E> {
71 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
72 write!(f, "{:#?}", self)
73 }
74}
75
76impl<E: Error> Error for IndexedValidationError<E> {}
77
78#[derive(Debug, Eq, PartialEq)]
80#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
81#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
82pub struct KeyedValidationError<K, E: Error> {
83 pub key: K,
85
86 #[cfg_attr(feature = "serde", serde(flatten))]
88 pub error: E,
89}
90
91impl<K, E: Error> KeyedValidationError<K, E> {
92 pub fn new(key: K, error: E) -> Self {
94 Self { key, error }
95 }
96}
97
98impl<K: Debug, E: Error> Display for KeyedValidationError<K, E> {
99 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
100 write!(f, "{:#?}", self)
101 }
102}
103
104impl<K: Debug, E: Error> Error for KeyedValidationError<K, E> {}