Valid

Struct Valid 

Source
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>

Source

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());
Source

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());
Source

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());
Source

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))
    }
});
Source

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());
Source

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> Clone for Valid<A, E, T>
where A: Clone, E: Clone, T: Clone,

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<A: Debug, E: Debug, T: Debug> Debug for Valid<A, E, T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<A, E, T> From<Cause<E, T>> for Valid<A, E, T>

Source§

fn from(value: Cause<E, T>) -> Self

Creates a failed validation from a single Cause.

§Examples
use tailcall_valid::{Valid, Validator, Cause};
let cause = Cause::new("error");
let result: Valid<(), &str, ()> = Valid::from(cause);
assert!(result.is_fail());
Source§

impl<A, E, T> From<Fusion<A, E, T>> for Valid<A, E, T>

Source§

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>

Source§

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>

Source§

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>

Source§

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: PartialEq, E: PartialEq, T: PartialEq> PartialEq for Valid<A, E, T>

Source§

fn eq(&self, other: &Valid<A, E, T>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<A, E, T> Validator<A, E, T> for Valid<A, E, T>

Source§

fn to_result(self) -> Result<A, Vec<Cause<E, T>>>

Converts the validation into a Result.
Source§

fn is_succeed(&self) -> bool

Returns true if the validation is successful.
Source§

fn is_fail(&self) -> bool

Returns true if the validation contains errors.
Source§

fn map<A1>(self, f: impl FnOnce(A) -> A1) -> Valid<A1, E, T>

Maps a function over the successful value, transforming it to a new type. Read more
Source§

fn foreach(self, f: impl FnMut(A)) -> Valid<A, E, T>
where A: Clone,

Executes a side effect function if the validation is successful. The original value is preserved. Read more
Source§

fn and<A1>(self, other: Valid<A1, E, T>) -> Valid<A1, E, T>

Combines two validations, keeping the result of the second one if both succeed. If either validation fails, all errors are collected. Read more
Source§

fn zip<A1>(self, other: Valid<A1, E, T>) -> Valid<(A, A1), E, T>

Combines two validations into a tuple of their results. If either validation fails, all errors are collected. Read more
Source§

fn fuse<A1>(self, other: Valid<A1, E, T>) -> Fusion<(A, A1), E, T>

Starts a fusion chain of validations. This allows combining multiple validation results using the Append trait. Read more
Source§

fn trace(self, trace: impl Into<T> + Clone) -> Valid<A, E, T>

Adds trace context to any errors in the validation. Successful validations are unaffected. Read more
Source§

fn fold<A1>( self, ok: impl FnOnce(A) -> Valid<A1, E, T>, err: impl FnOnce() -> Valid<A1, E, T>, ) -> Valid<A1, E, T>

Handles both success and failure cases of a validation. Read more
Source§

fn and_then<B>(self, f: impl FnOnce(A) -> Valid<B, E, T>) -> Valid<B, E, T>

Chains a validation operation by applying a function to a successful value. If the original validation failed, the errors are propagated. Read more
Source§

fn unit(self) -> Valid<(), E, T>

Converts a successful validation to (). Failed validations retain their errors. Read more
Source§

fn some(self) -> Valid<Option<A>, E, T>

Wraps a successful value in Some(_). Read more
Source§

fn map_to<B>(self, b: B) -> Valid<B, E, T>

Maps a successful validation to a constant value. Read more
Source§

fn when(self, f: impl FnOnce() -> bool) -> Valid<(), E, T>

Conditionally validates based on a predicate. If the predicate returns false, succeeds with (). Read more
Source§

impl<A, E, T> StructuralPartialEq for Valid<A, E, T>

Auto Trait Implementations§

§

impl<A, E, T> Freeze for Valid<A, E, T>
where A: Freeze,

§

impl<A, E, T> RefUnwindSafe for Valid<A, E, T>

§

impl<A, E, T> Send for Valid<A, E, T>
where A: Send, E: Send, T: Send,

§

impl<A, E, T> Sync for Valid<A, E, T>
where A: Sync, E: Sync, T: Sync,

§

impl<A, E, T> Unpin for Valid<A, E, T>
where A: Unpin, E: Unpin, T: Unpin,

§

impl<A, E, T> UnwindSafe for Valid<A, E, T>
where A: UnwindSafe, E: UnwindSafe, T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<S, T> ValidInto<T> for S
where T: ValidFrom<S>,

Source§

type Error = <T as ValidFrom<S>>::Error

Source§

type Trace = <T as ValidFrom<S>>::Trace

Source§

fn valid_into( self, ) -> Valid<T, <S as ValidInto<T>>::Error, <S as ValidInto<T>>::Trace>