Skip to main content

EnsureExt

Trait EnsureExt 

Source
pub trait EnsureExt<T> {
    // Required methods
    fn ensure<P, E>(
        self,
        predicate: P,
        error: E,
    ) -> Validation<T, NonEmptyVec<E>>
       where P: Predicate<T>;
    fn ensure_with<P, E, F>(
        self,
        predicate: P,
        error_fn: F,
    ) -> Validation<T, NonEmptyVec<E>>
       where P: Predicate<T>,
             F: FnOnce(&T) -> E;
}
Expand description

Extension trait for applying predicates to values with validation.

This trait provides a fluent API for validating values against predicates, returning Validation results that can accumulate errors.

§Example

use debtmap::effects::validation::EnsureExt;
use debtmap::errors::AnalysisError;
use stillwater::predicate::lt;

let complexity: u32 = 50;
let validated = complexity.ensure(lt(100_u32), AnalysisError::validation("Complexity too high"));
assert!(validated.is_success());

let high_complexity: u32 = 150;
let validated = high_complexity.ensure(lt(100_u32), AnalysisError::validation("Complexity too high"));
assert!(validated.is_failure());

Required Methods§

Source

fn ensure<P, E>(self, predicate: P, error: E) -> Validation<T, NonEmptyVec<E>>
where P: Predicate<T>,

Validate that this value satisfies the given predicate.

Returns Validation::Success(self) if the predicate passes, or Validation::Failure with the provided error if it fails.

Source

fn ensure_with<P, E, F>( self, predicate: P, error_fn: F, ) -> Validation<T, NonEmptyVec<E>>
where P: Predicate<T>, F: FnOnce(&T) -> E,

Validate with an error-generating function.

The function receives a reference to the value and generates an appropriate error message.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl<T> EnsureExt<T> for T