Skip to main content

Module combinator

Module combinator 

Source
Expand description

Combinators for composing Validators.

And, Or, and Not build compound rules from simpler ones, entirely at the type level. Because they are themselves Validators, they nest: And<A, Or<B, C>> is a valid rule.

And and Or require their two sub-rules to share one error type, which is the case for every built-in rule (all report ValidationError). Not works with any sub-rule and reports its own ValidationError.

§Examples

use type_lib::combinator::And;
use type_lib::rules::{Ascii, NonEmpty};
use type_lib::Refined;

// A token: non-empty and ASCII-only.
type Token<'a> = Refined<&'a str, And<NonEmpty, Ascii>>;

assert!(Token::new("abc123").is_ok());
assert!(Token::new("").is_err()); // fails NonEmpty
assert!(Token::new("café").is_err()); // fails Ascii

Structs§

And
Passes only when both sub-rules A and B pass.
Not
Passes exactly when the sub-rule A fails.
Or
Passes when either sub-rule A or B passes.