pub struct Valid<A, E, T>(/* private fields */);Expand description
A validation type that can represent either a successful value of type A
or a collection of validation errors of type E with trace context T.
Valid is useful for accumulating multiple validation errors rather than
stopping at the first error encountered.
Implementations§
Source§impl<A, E, T> Valid<A, E, T>
impl<A, E, T> Valid<A, E, T>
Sourcepub fn fail(e: E) -> Valid<A, E, T>
pub fn fail(e: E) -> Valid<A, E, T>
Creates a new failed validation with a single error.
§Examples
use tailcall_valid::{Valid, Validator};
let result: Valid<(), i32, ()> = Valid::fail(1);
assert!(result.is_fail());Sourcepub fn fail_at(error: E, trace: T) -> Valid<A, E, T>where
E: Debug,
pub fn fail_at(error: E, trace: T) -> Valid<A, E, T>where
E: Debug,
Creates a new failed validation with an error and trace context.
§Examples
use tailcall_valid::{Valid, Validator};
let result = Valid::<(), &str, &str>::fail_at("error", "context");
assert!(result.is_fail());Sourcepub fn succeed(a: A) -> Valid<A, E, T>
pub fn succeed(a: A) -> Valid<A, E, T>
Creates a new successful validation containing the given value.
§Examples
use tailcall_valid::{Valid, Validator};
let result = Valid::<i32, (), ()>::succeed(42);
assert!(result.is_succeed());Sourcepub fn from_iter<B>(
iter: impl IntoIterator<Item = A>,
f: impl FnMut(A) -> Valid<B, E, T>,
) -> Valid<Vec<B>, E, T>
pub fn from_iter<B>( iter: impl IntoIterator<Item = A>, f: impl FnMut(A) -> Valid<B, E, T>, ) -> Valid<Vec<B>, E, T>
Validates each item in an iterator using the provided validation function, collecting all errors that occur.
§Examples
use tailcall_valid::{Valid, Validator};
let numbers = vec![1, 2, 3];
let result = Valid::from_iter(numbers, |n| {
if n % 2 == 0 {
Valid::<i32, String, ()>::succeed(n * 2)
} else {
Valid::<i32, String, ()>::fail(format!("{} is odd", n))
}
});Sourcepub fn from_option(option: Option<A>, e: E) -> Valid<A, E, T>
pub fn from_option(option: Option<A>, e: E) -> Valid<A, E, T>
Creates a new Valid from an Option value.
If the option is None, creates a failed validation with the provided error.
If the option is Some, creates a successful validation with the contained value.
§Examples
use tailcall_valid::{Valid, Validator};
let some_value = Some(42);
let result: Valid<i32, &str, ()> = Valid::from_option(some_value, "error");
assert_eq!(result, Valid::succeed(42));
let none_value: Option<i32> = None;
let result: Valid<i32, &str, ()> = Valid::from_option(none_value, "error");
assert!(result.is_fail());Sourcepub fn none() -> Valid<Option<A>, E, T>
pub fn none() -> Valid<Option<A>, E, T>
Creates a successful validation containing None.
This is useful when you want to explicitly represent the absence of a value as a successful validation rather than an error condition.
§Examples
use tailcall_valid::Valid;
let result: Valid<Option<i32>, &str, ()> = Valid::none();
assert_eq!(result, Valid::succeed(None));Trait Implementations§
Source§impl<A, E, T> From<Fusion<A, E, T>> for Valid<A, E, T>
impl<A, E, T> From<Fusion<A, E, T>> for Valid<A, E, T>
Source§fn from(value: Fusion<A, E, T>) -> Self
fn from(value: Fusion<A, E, T>) -> Self
Converts a Fusion back into a Valid.
This is typically used at the end of a chain of fuse operations
to convert the final result back into a Valid.
§Examples
use tailcall_valid::{Valid, Validator};
let v1: Valid<Vec<i32>, (), ()> = Valid::succeed(vec![1]);
let v2: Valid<Vec<i32>, (), ()> = Valid::succeed(vec![2]);
let fusion = v1.fuse(v2);
let result: Valid<(Vec<i32>, Vec<i32>), (), ()> = Valid::from(fusion);
assert!(result.is_succeed());Source§impl<A, E, T> From<Result<A, Cause<E, T>>> for Valid<A, E, T>
impl<A, E, T> From<Result<A, Cause<E, T>>> for Valid<A, E, T>
Source§fn from(value: Result<A, Cause<E, T>>) -> Self
fn from(value: Result<A, Cause<E, T>>) -> Self
Creates a Valid from a Result containing a single Cause as its error type.
§Examples
use tailcall_valid::{Valid, Validator, Cause};
let ok_result: Result<i32, Cause<&str, ()>> = Ok(42);
let valid = Valid::from(ok_result);
assert_eq!(valid, Valid::succeed(42));
let err_result: Result<i32, Cause<&str, ()>> = Err(Cause::new("error"));
let valid = Valid::from(err_result);
assert!(valid.is_fail());Source§impl<A, E, T> From<Result<A, Vec<Cause<E, T>>>> for Valid<A, E, T>
impl<A, E, T> From<Result<A, Vec<Cause<E, T>>>> for Valid<A, E, T>
Source§fn from(value: Result<A, Vec<Cause<E, T>>>) -> Self
fn from(value: Result<A, Vec<Cause<E, T>>>) -> Self
Creates a Valid from a Result containing multiple Causes as its error type.
§Examples
use tailcall_valid::{Valid, Validator, Cause};
let ok_result: Result<i32, Vec<Cause<&str, ()>>> = Ok(42);
let valid = Valid::from(ok_result);
assert_eq!(valid, Valid::succeed(42));
let err_result: Result<i32, Vec<Cause<&str, ()>>> = Err(vec![
Cause::new("error1"),
Cause::new("error2")
]);
let valid = Valid::from(err_result);
assert!(valid.is_fail());Source§impl<A, E, T> From<Vec<Cause<E, T>>> for Valid<A, E, T>
impl<A, E, T> From<Vec<Cause<E, T>>> for Valid<A, E, T>
Source§fn from(value: Vec<Cause<E, T>>) -> Self
fn from(value: Vec<Cause<E, T>>) -> Self
Creates a failed validation from a vector of Causes.
§Examples
use tailcall_valid::{Valid, Validator, Cause};
let causes = vec![Cause::new("error1"), Cause::new("error2")];
let result: Valid<(), &str, ()> = Valid::from(causes);
assert!(result.is_fail());Source§impl<A, E, T> Validator<A, E, T> for Valid<A, E, T>
impl<A, E, T> Validator<A, E, T> for Valid<A, E, T>
Source§fn is_succeed(&self) -> bool
fn is_succeed(&self) -> bool
Source§fn map<A1>(self, f: impl FnOnce(A) -> A1) -> Valid<A1, E, T>
fn map<A1>(self, f: impl FnOnce(A) -> A1) -> Valid<A1, E, T>
Source§fn foreach(self, f: impl FnMut(A)) -> Valid<A, E, T>where
A: Clone,
fn foreach(self, f: impl FnMut(A)) -> Valid<A, E, T>where
A: Clone,
Source§fn and<A1>(self, other: Valid<A1, E, T>) -> Valid<A1, E, T>
fn and<A1>(self, other: Valid<A1, E, T>) -> Valid<A1, E, T>
Source§fn zip<A1>(self, other: Valid<A1, E, T>) -> Valid<(A, A1), E, T>
fn zip<A1>(self, other: Valid<A1, E, T>) -> Valid<(A, A1), E, T>
Source§fn fuse<A1>(self, other: Valid<A1, E, T>) -> Fusion<(A, A1), E, T>
fn fuse<A1>(self, other: Valid<A1, E, T>) -> Fusion<(A, A1), E, T>
Append trait. Read moreSource§fn trace(self, trace: impl Into<T> + Clone) -> Valid<A, E, T>
fn trace(self, trace: impl Into<T> + Clone) -> Valid<A, E, T>
Source§fn fold<A1>(
self,
ok: impl FnOnce(A) -> Valid<A1, E, T>,
err: impl FnOnce() -> Valid<A1, E, T>,
) -> Valid<A1, E, T>
fn fold<A1>( self, ok: impl FnOnce(A) -> Valid<A1, E, T>, err: impl FnOnce() -> Valid<A1, E, T>, ) -> Valid<A1, E, T>
Source§fn and_then<B>(self, f: impl FnOnce(A) -> Valid<B, E, T>) -> Valid<B, E, T>
fn and_then<B>(self, f: impl FnOnce(A) -> Valid<B, E, T>) -> Valid<B, E, T>
Source§fn unit(self) -> Valid<(), E, T>
fn unit(self) -> Valid<(), E, T>
().
Failed validations retain their errors. Read more