Validation

Enum Validation 

Source
pub enum Validation<T, E> {
    Success(T),
    Failure(E),
}
Expand description

A validation that either succeeds with a value or fails with accumulated errors

Unlike Result, Validation is designed to accumulate multiple errors when combining validations. This makes it ideal for form validation, API input validation, and other scenarios where you want to report all errors at once rather than failing on the first one.

§Type Parameters

  • T - The type of the success value
  • E - The type of the error value (must implement Semigroup for accumulation)

§Examples

use stillwater::{Validation, Semigroup};

// Simple validation
let v = Validation::<_, Vec<&str>>::success(42);
assert_eq!(v.into_result(), Ok(42));

// Error accumulation
let v1 = Validation::<i32, _>::failure(vec!["error1"]);
let v2 = Validation::<i32, _>::failure(vec!["error2"]);
let combined = v1.and(v2);
assert_eq!(combined, Validation::Failure(vec!["error1", "error2"]));

Variants§

§

Success(T)

Successful validation with a value

§

Failure(E)

Failed validation with accumulated errors

Implementations§

Source§

impl<T, E> Validation<T, E>

Source

pub fn success(value: T) -> Validation<T, E>

Create a successful validation

§Examples
use stillwater::Validation;

let v = Validation::<i32, String>::success(42);
assert!(v.is_success());
Source

pub fn failure(error: E) -> Validation<T, E>

Create a failed validation

§Examples
use stillwater::Validation;

let v = Validation::<i32, Vec<&str>>::failure(vec!["error"]);
assert!(v.is_failure());
Source

pub fn from_result(result: Result<T, E>) -> Validation<T, E>

Create a validation from a Result

§Examples
use stillwater::Validation;

let v = Validation::from_result(Ok::<_, String>(42));
assert_eq!(v, Validation::Success(42));

let v = Validation::from_result(Err::<i32, _>("error".to_string()));
assert_eq!(v, Validation::Failure("error".to_string()));
Source

pub fn into_result(self) -> Result<T, E>

Convert this validation to a Result

§Examples
use stillwater::Validation;

let v = Validation::<_, String>::success(42);
assert_eq!(v.into_result(), Ok(42));

let v = Validation::<i32, _>::failure("error".to_string());
assert_eq!(v.into_result(), Err("error".to_string()));
Source

pub fn is_success(&self) -> bool

Check if this validation is successful

§Examples
use stillwater::Validation;

let v = Validation::<_, String>::success(42);
assert!(v.is_success());
Source

pub fn is_failure(&self) -> bool

Check if this validation failed

§Examples
use stillwater::Validation;

let v = Validation::<i32, _>::failure("error".to_string());
assert!(v.is_failure());
Source

pub fn map<U, F>(self, f: F) -> Validation<U, E>
where F: FnOnce(T) -> U,

Transform the success value if present

§Examples
use stillwater::Validation;

let v = Validation::<_, String>::success(5);
let result = v.map(|x| x * 2);
assert_eq!(result, Validation::Success(10));
Source

pub fn map_err<E2, F>(self, f: F) -> Validation<T, E2>
where F: FnOnce(E) -> E2,

Transform the error value if present

§Examples
use stillwater::Validation;

let v = Validation::<i32, _>::failure(vec!["error"]);
let result = v.map_err(|errors| errors.len());
assert_eq!(result, Validation::Failure(1));
Source§

impl<T, E> Validation<T, NonEmptyVec<E>>

Source

pub fn fail(error: E) -> Validation<T, NonEmptyVec<E>>

Create a failure with a single error.

This is a convenience method for creating validations that fail with a single error, without needing to construct a NonEmptyVec explicitly.

§Examples
use stillwater::{Validation, NonEmptyVec};

let v = Validation::<i32, NonEmptyVec<&str>>::fail("error");
assert!(v.is_failure());
Source§

impl<T, E> Validation<T, E>
where E: Semigroup,

Source

pub fn and<U>(self, other: Validation<U, E>) -> Validation<(T, U), E>

Combine two validations, accumulating errors using the Semigroup instance

If both validations are successful, returns a success with a tuple of both values. If either or both fail, accumulates the errors using Semigroup::combine.

§Examples
use stillwater::Validation;

// Both successful
let v1 = Validation::<_, Vec<&str>>::success(1);
let v2 = Validation::<_, Vec<&str>>::success(2);
assert_eq!(v1.and(v2), Validation::Success((1, 2)));

// Both failed - errors accumulate
let v1 = Validation::<i32, _>::failure(vec!["error1"]);
let v2 = Validation::<i32, _>::failure(vec!["error2"]);
assert_eq!(v1.and(v2), Validation::Failure(vec!["error1", "error2"]));
Source

pub fn and_then<U, F>(self, f: F) -> Validation<U, E>
where F: FnOnce(T) -> Validation<U, E>,

Chain a dependent validation

Similar to Result::and_then, but for validations. The function is only called if the current validation is successful.

§Examples
use stillwater::Validation;

let v = Validation::<_, Vec<&str>>::success(5);
let result = v.and_then(|x| {
    if x > 0 {
        Validation::success(x * 2)
    } else {
        Validation::failure(vec!["must be positive"])
    }
});
assert_eq!(result, Validation::Success(10));
Source

pub fn all_vec(validations: Vec<Validation<T, E>>) -> Validation<Vec<T>, E>

Combine all validations in a Vec

Returns a success with a Vec of all success values if all validations succeed. Otherwise, accumulates all errors using Semigroup::combine.

§Examples
use stillwater::Validation;

let validations = vec![
    Validation::<_, Vec<&str>>::success(1),
    Validation::<_, Vec<&str>>::success(2),
    Validation::<_, Vec<&str>>::success(3),
];
let result = Validation::all_vec(validations);
assert_eq!(result, Validation::Success(vec![1, 2, 3]));

let validations = vec![
    Validation::<i32, _>::failure(vec!["error1"]),
    Validation::<i32, _>::failure(vec!["error2"]),
];
let result = Validation::all_vec(validations);
assert_eq!(result, Validation::Failure(vec!["error1", "error2"]));
Source§

impl<T, E> Validation<T, E>

Source

pub fn all<V, E2>( validations: V, ) -> Validation<<V as ValidateAll<E2>>::Output, E2>
where E2: Semigroup, V: ValidateAll<E2>,

Combine all validations in a tuple

This is a convenience method that delegates to the ValidateAll trait. It works with tuples of validations up to size 12.

§Examples
use stillwater::{Validation, validation::ValidateAll};

let result = (
    Validation::<_, Vec<&str>>::success(1),
    Validation::<_, Vec<&str>>::success(2),
    Validation::<_, Vec<&str>>::success(3),
).validate_all();
assert_eq!(result, Validation::Success((1, 2, 3)));

Trait Implementations§

Source§

impl<T, E> Clone for Validation<T, E>
where T: Clone, E: Clone,

Source§

fn clone(&self) -> Validation<T, E>

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<T, E> Debug for Validation<T, E>
where T: Debug, E: Debug,

Source§

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

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

impl<T, E> PartialEq for Validation<T, E>
where T: PartialEq, E: PartialEq,

Source§

fn eq(&self, other: &Validation<T, E>) -> 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<T, E> Eq for Validation<T, E>
where T: Eq, E: Eq,

Source§

impl<T, E> StructuralPartialEq for Validation<T, E>

Auto Trait Implementations§

§

impl<T, E> Freeze for Validation<T, E>
where T: Freeze, E: Freeze,

§

impl<T, E> RefUnwindSafe for Validation<T, E>

§

impl<T, E> Send for Validation<T, E>
where T: Send, E: Send,

§

impl<T, E> Sync for Validation<T, E>
where T: Sync, E: Sync,

§

impl<T, E> Unpin for Validation<T, E>
where T: Unpin, E: Unpin,

§

impl<T, E> UnwindSafe for Validation<T, E>
where T: UnwindSafe, E: 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<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
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.