Skip to main content

Accumulator

Struct Accumulator 

Source
pub struct Accumulator { /* private fields */ }
Expand description

Type used to build up a list of validation failures.

When validating fields of a struct while validating that struct, push the field’s name (or index in a sequence) onto the prefix member of the accumulator before passing it in to the validator. This allows nested fields to report where the failure happened.

use validatrix::Accumulator;

fn accumulate(accum: &mut Accumulator) {
    accum.with_key("some_field", |a| if true == false { a.add_failure("pigs have flown") });

    // equivalent to the above
    if true == false {
        accum.add_failure_at("some_field", "hell has frozen over");
    }
}

Implementations§

Source§

impl Accumulator

Source

pub fn add_failure(&mut self, message: impl Into<String>)

Add an extra failure to this accumulator.

Source

pub fn add_failure_at( &mut self, prefix: impl Into<Key>, message: impl Into<String>, )

Accumulate an extra failure at the given key.

Source

pub fn validate_member_at( &mut self, field: impl Into<Key>, member: &impl Validate, )

Accumulate any validation errors for a Validate field with key field.

Source

pub fn validate_member_at_ctx<T: ValidateContext>( &mut self, field: impl Into<Key>, member: &T, context: &T::Context, )

Like Self::validate_member_at, but for a crate::ValidateContext field with the given context.

Source

pub fn with_key(&mut self, prefix: impl Into<Key>, f: impl FnOnce(&mut Self))

Perform manual validation inside the given closure for a member with the given prefix.

The closure takes an accumulator as an argument, which will be this accumulator with the added prefix.

Source

pub fn with_keys(&mut self, prefixes: &[Key], f: impl FnOnce(&mut Self))

Convenience method for Accumulator::with_key-like behaviour at multiple keys’ depth.

Source

pub fn validate_iter<'a, V: Validate + 'a, I: IntoIterator<Item = &'a V>>( &mut self, items: I, )

Iterate over a collection of Validate-able items, validating them all in turn. As this tracks the items’ index in the iterable, the whole collection should be passed rather than a filtered version.

Source

pub fn validate_iter_ctx<'a, V: ValidateContext + 'a, I: IntoIterator<Item = &'a V>>( &mut self, items: I, context: &V::Context, )

Like Self::validate_iter, but for a collection of crate::ValidateContext items with the given context.

Source

pub fn validate_iter_at<'a, V: Validate + 'a, I: IntoIterator<Item = &'a V>>( &mut self, prefix: impl Into<Key>, items: I, )

Convenience method to do Self::validate_iter for a given key.

Source

pub fn validate_iter_at_ctx<'a, V: ValidateContext + 'a, I: IntoIterator<Item = &'a V>>( &mut self, prefix: impl Into<Key>, items: I, context: &V::Context, )

Like Self::validate_iter_at, but for a collection of crate::ValidateContext items with the given context.

Source

pub fn len(&self) -> usize

Number of failures logged by this accumulator.

Source

pub fn is_empty(&self) -> bool

Whether this accumulator has 0 failures.

Trait Implementations§

Source§

impl Debug for Accumulator

Source§

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

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

impl From<Accumulator> for Result<(), Error>

Source§

fn from(value: Accumulator) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

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> 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, 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.