1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
use crate::datatypes::validated::Validated;
use smallvec::smallvec;
impl<E, A> Validated<E, A> {
/// Converts from `Result<A, E>` to `Validated<E, A>` using a reference to the Result.
///
/// This method takes a reference to a `Result` and returns a new `Validated` instance.
/// The original `Result` is not consumed, making this method suitable when you need
/// to keep the original `Result` intact. This requires `A: Clone` and `E: Clone`
/// to create a new `Validated` from the referenced data.
///
/// For a version that takes ownership of the `Result`, see `from_result_owned`.
///
/// # Type Parameters
///
/// * `A`: The value type (must implement `Clone`)
/// * `E`: The error type (must implement `Clone`)
///
/// # Arguments
///
/// * `result` - The Result to convert (by reference)
///
/// # Examples
///
/// ```rust
/// use rustica::datatypes::validated::Validated;
///
/// let result: Result<i32, &str> = Ok(42);
/// let validated = Validated::from_result(&result);
/// assert_eq!(validated, Validated::valid(42));
/// // The original result is still available
/// assert_eq!(result, Ok(42));
///
/// let error_result: Result<i32, &str> = Err("error");
/// let validated = Validated::from_result(&error_result);
/// assert_eq!(validated, Validated::invalid("error"));
/// ```
#[inline]
pub fn from_result(result: &Result<A, E>) -> Validated<E, A>
where
A: Clone,
E: Clone,
{
use crate::error::result_to_validated;
result_to_validated(result.clone())
}
/// Converts from `Result<A, E>` to `Validated<E, A>`, taking ownership of the Result.
///
/// This method consumes the `Result` and returns a new `Validated` instance. By taking
/// ownership of the `Result`, this method avoids any cloning of the inner values, making
/// it more efficient than `from_result` when you don't need the original `Result` anymore.
/// This is particularly useful when working with expensive-to-clone types.
///
/// For a version that takes a reference to the `Result`, see `from_result`.
///
/// # Type Parameters
///
/// * `A`: The value type
/// * `E`: The error type
///
/// # Arguments
///
/// * `result` - The Result to convert and consume
///
/// # Examples
///
/// ```rust
/// use rustica::datatypes::validated::Validated;
///
/// let result: Result<i32, String> = Ok(42);
/// let validated = Validated::from_result_owned(result);
/// assert_eq!(validated, Validated::valid(42));
/// // Note that result is consumed and can't be used again
///
/// let error_result: Result<i32, String> = Err("error".to_string());
/// let validated = Validated::from_result_owned(error_result);
/// assert!(validated.is_invalid());
/// assert_eq!(validated.errors(), vec!["error".to_string()]);
/// ```
pub fn from_result_owned(result: Result<A, E>) -> Validated<E, A>
where
A: Clone,
E: Clone,
{
use crate::error::result_to_validated;
result_to_validated(result)
}
/// Converts this `Validated` into a `Result`.
///
/// # Examples
///
/// ```rust
/// use rustica::datatypes::validated::Validated;
///
/// let valid: Validated<&str, i32> = Validated::valid(42);
/// let result = valid.to_result();
/// assert_eq!(result, Ok(42));
///
/// let invalid: Validated<&str, i32> = Validated::invalid("error");
/// assert!(invalid.to_result().is_err());
/// ```
#[inline]
pub fn to_result(&self) -> Result<A, E>
where
A: Clone,
E: Clone,
{
match self {
Validated::Valid(a) => Ok(a.clone()),
Validated::Invalid(errors) => Err(errors[0].clone()),
}
}
/// Converts this `Validated` into a `Result`, taking ownership of the Validated.
///
/// # Examples
///
/// ```rust
/// use rustica::datatypes::validated::Validated;
///
/// let valid: Validated<String, i32> = Validated::valid(42);
/// let result = valid.to_result_owned();
/// assert_eq!(result, Ok(42));
///
/// let invalid: Validated<String, i32> = Validated::invalid("error".to_string());
/// assert!(invalid.to_result_owned().is_err());
/// ```
#[inline]
pub fn to_result_owned(self) -> Result<A, E> {
match self {
Validated::Valid(a) => Ok(a),
Validated::Invalid(mut errors) => Err(errors.remove(0)),
}
}
/// Converts from `Option<A>` to `Validated<E, A>` with a provided error.
///
/// If the Option is Some, returns a Valid value.
/// If the Option is None, returns an Invalid with the provided error.
///
/// # Arguments
///
/// * `option` - The Option to convert
/// * `error` - The error to use if the Option is None
///
/// # Examples
///
/// ```rust
/// use rustica::datatypes::validated::Validated;
///
/// let some_value: Option<i32> = Some(42);
/// let validated: Validated<&str, i32> = Validated::from_option(&some_value, &"missing value");
/// assert_eq!(validated, Validated::valid(42));
///
/// let none_value: Option<i32> = None;
/// let validated: Validated<&str, i32> = Validated::from_option(&none_value, &"missing value");
/// assert_eq!(validated, Validated::invalid("missing value"));
/// ```
#[inline]
pub fn from_option(option: &Option<A>, error: &E) -> Self
where
A: Clone,
E: Clone,
{
match option {
Some(value) => Validated::Valid(value.clone()),
None => Validated::Invalid(smallvec![error.clone()]),
}
}
/// Converts from `Option<A>` to `Validated<E, A>` with a provided error, taking ownership.
///
/// If the Option is Some, returns a Valid value.
/// If the Option is None, returns an Invalid with the provided error.
///
/// # Arguments
///
/// * `option` - The Option to convert and consume
/// * `error` - The error to use if the Option is None
///
/// # Examples
///
/// ```rust
/// use rustica::datatypes::validated::Validated;
///
/// let some_value: Option<i32> = Some(42);
/// let validated: Validated<String, i32> = Validated::from_option_owned(some_value, "missing value".to_string());
/// assert_eq!(validated, Validated::valid(42));
///
/// let none_value: Option<i32> = None;
/// let validated: Validated<String, i32> = Validated::from_option_owned(none_value, "missing value".to_string());
/// assert!(validated.is_invalid());
/// ```
#[inline]
pub fn from_option_owned(option: Option<A>, error: E) -> Self {
match option {
Some(value) => Validated::Valid(value),
None => Validated::Invalid(smallvec![error]),
}
}
/// Converts from `Option<A>` to `Validated<E, A>` with a function to generate the error.
///
/// If the Option is Some, returns a Valid value.
/// If the Option is None, returns an Invalid with the error from the provided function.
///
/// # Arguments
///
/// * `option` - The Option to convert
/// * `error_fn` - Function to generate the error if the Option is None
///
/// # Examples
///
/// ```rust
/// use rustica::datatypes::validated::Validated;
///
/// let some_value: Option<i32> = Some(42);
/// let validated: Validated<&str, i32> = Validated::from_option_with(&some_value, &|| "missing value");
/// assert_eq!(validated, Validated::valid(42));
///
/// let none_value: Option<i32> = None;
/// let validated: Validated<&str, i32> = Validated::from_option_with(&none_value, &|| "missing value");
/// assert_eq!(validated, Validated::invalid("missing value"));
/// ```
#[inline]
pub fn from_option_with<F>(option: &Option<A>, error_fn: &F) -> Self
where
F: Fn() -> E,
A: Clone,
{
match option {
Some(value) => Validated::Valid(value.clone()),
None => Validated::Invalid(smallvec![error_fn()]),
}
}
/// Converts from `Option<A>` to `Validated<E, A>` with a function to generate the error, taking ownership.
///
/// If the Option is Some, returns a Valid value.
/// If the Option is None, returns an Invalid with the error from the provided function.
///
/// # Arguments
///
/// * `option` - The Option to convert and consume
/// * `error_fn` - Function to generate the error if the Option is None
///
/// # Examples
///
/// ```rust
/// use rustica::datatypes::validated::Validated;
///
/// let some_value: Option<i32> = Some(42);
/// let validated: Validated<String, i32> = Validated::from_option_with_owned(some_value, || "missing value".to_string());
/// assert_eq!(validated, Validated::valid(42));
///
/// let none_value: Option<i32> = None;
/// let validated: Validated<String, i32> = Validated::from_option_with_owned(none_value, || "missing value".to_string());
/// assert!(validated.is_invalid());
/// ```
#[inline]
pub fn from_option_with_owned<F>(option: Option<A>, error_fn: F) -> Self
where
F: FnOnce() -> E,
{
match option {
Some(value) => Validated::Valid(value),
None => Validated::Invalid(smallvec![error_fn()]),
}
}
}