Expand description

Encode the validity of your data in the type system

Example usage:

#[derive(Debug)]
struct Email(String);
 
impl Validate for Email {
    type Error = ();
 
    fn validate(&self) -> Result<(), Self::Error> {
        // perform some actual email validation
        Ok(()) 
    }
}
 
// This requires a Valid<Email>, which guarantees the data has been validated
fn send_email(email: Valid<Email>) {
    println!("Sending email to {:?}", email.0);
}
 
fn main() {
   let email = Email("example@example.com".into()); 
   let valid_email = Valid::new(email).unwrap();
   // at this point, validation must have succeeded
   send_email(valid_email);
}

Structs

A wrapper struct that guarantees that the inner value has been checked with its Validate implementation

Traits

Definition of what it means for a value to be “valid”