validatus/validates/
convert.rs1use crate::Validation;
2use alloc::{vec, vec::Vec};
3use Validation::{Failure, Success};
4
5pub trait Validate<T, E> {
6 fn valid(self) -> Validation<T, E>;
7 fn validate(self, errors: &mut Vec<E>) -> Validation<T, E>
8 where
9 E: Clone;
10 fn recover(self, errors: &mut Vec<E>) -> Validation<T, E>
11 where
12 T: Default;
13}
14
15impl<T, E> Validate<T, E> for Option<T>
16where
17 E: From<()>,
18{
19 fn valid(self) -> Validation<T, E> {
20 match self {
21 Some(s) => Success { value: s, diagnostics: vec![] },
22 None => Failure { fatal: E::from(()), diagnostics: vec![] },
23 }
24 }
25
26 fn validate(self, errors: &mut Vec<E>) -> Validation<T, E>
27 where
28 E: Clone,
29 {
30 match self {
31 Some(s) => Success { value: s, diagnostics: vec![] },
32 None => Failure { fatal: E::from(()), diagnostics: errors.clone() },
33 }
34 }
35
36 fn recover(self, errors: &mut Vec<E>) -> Validation<T, E>
37 where
38 T: Default,
39 {
40 match self {
41 Some(s) => Success { value: s, diagnostics: vec![] },
42 None => {
43 errors.push(E::from(()));
44 Success { value: T::default(), diagnostics: vec![] }
45 }
46 }
47 }
48}
49impl<T, E, F> Validate<T, E> for Result<T, F>
50where
51 E: From<F>,
52{
53 fn valid(self) -> Validation<T, E> {
54 match self {
55 Ok(o) => Success { value: o, diagnostics: vec![] },
56 Err(e) => Failure { fatal: E::from(e), diagnostics: vec![] },
57 }
58 }
59
60 fn validate(self, errors: &mut Vec<E>) -> Validation<T, E>
61 where
62 E: Clone,
63 {
64 match self {
65 Ok(o) => Success { value: o, diagnostics: vec![] },
66 Err(e) => Failure { fatal: E::from(e), diagnostics: errors.clone() },
67 }
68 }
69
70 fn recover(self, errors: &mut Vec<E>) -> Validation<T, E>
71 where
72 T: Default,
73 {
74 match self {
75 Ok(s) => Success { value: s, diagnostics: vec![] },
76 Err(e) => {
77 errors.push(E::from(e));
78 Success { value: T::default(), diagnostics: vec![] }
79 }
80 }
81 }
82}
83
84impl<T, E, F> Validate<T, E> for Validation<T, F>
85where
86 E: From<F>,
87{
88 fn valid(self) -> Validation<T, E> {
89 match self {
90 Success { value, diagnostics } => Success { value, diagnostics: diagnostics.into_iter().map(E::from).collect() },
91 Failure { fatal, diagnostics } => {
92 Failure { fatal: E::from(fatal), diagnostics: diagnostics.into_iter().map(E::from).collect() }
93 }
94 }
95 }
96
97 fn validate(self, errors: &mut Vec<E>) -> Validation<T, E>
98 where
99 E: Clone,
100 {
101 match self {
102 Success { value, diagnostics } => {
103 errors.extend(diagnostics.into_iter().map(E::from));
104 Success { value, diagnostics: vec![] }
105 }
106 Failure { fatal, diagnostics } => {
107 errors.extend(diagnostics.into_iter().map(E::from));
108 Failure { fatal: E::from(fatal), diagnostics: errors.clone() }
109 }
110 }
111 }
112
113 fn recover(self, errors: &mut Vec<E>) -> Validation<T, E>
114 where
115 T: Default,
116 {
117 match self {
118 Success { value, diagnostics } => {
119 errors.extend(diagnostics.into_iter().map(E::from));
120 Success { value, diagnostics: vec![] }
121 }
122 Failure { fatal, diagnostics } => {
123 errors.extend(diagnostics.into_iter().map(E::from));
124 errors.push(E::from(fatal));
125 Success { value: T::default(), diagnostics: vec![] }
126 }
127 }
128 }
129}