Validations::validate_result

This commit is contained in:
Jonathan Johnson 2023-12-21 08:32:01 -08:00
parent 641ae3a17d
commit 32cd07c241
No known key found for this signature in database
GPG key ID: A66D6A34D6620579
2 changed files with 31 additions and 0 deletions

View file

@ -24,6 +24,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
[92]: https://github.com/khonsulabs/gooey/issues/92
### Added
- `Validations::validate_result` attaches a `Dynamic<Result<T,E>>` to the
validations. This was already available on `when` conditioned validations.
## v0.1.3 (2023-12-19)
### Added

View file

@ -2243,6 +2243,32 @@ impl Validations {
validation
}
/// Returns a dynamic validation status that is created by transforming the
/// `Err` variant of `result` using [`Display`].
///
/// The validation is linked with `self` such that checking `self`'s
/// validation status will include this validation.
#[must_use]
pub fn validate_result<T, E>(
&self,
result: impl IntoDynamic<Result<T, E>>,
) -> Dynamic<Validation>
where
T: Send + 'static,
E: Display + Send + 'static,
{
let result = result.into_dynamic();
let error_message = result.map_each(move |value| match value {
Ok(_) => None,
Err(err) => Some(err.to_string()),
});
self.validate(&error_message, |error_message| match error_message {
None => Ok(()),
Some(message) => Err(message.clone()),
})
}
fn map_to_message<T, E, Valid>(
mut check: Valid,
) -> impl for<'a> FnMut(&'a GenerationalValue<T>) -> GenerationalValue<Option<String>> + Send + 'static