vet/readme.md
2025-10-11 20:13:29 -07:00

2.3 KiB

Vet

A library for validation of arbitrary types.

Usage

Add a dependency entry in Cargo.toml.

[dependencies]
vet = "0.1"

Implement the Vet trait on your type.

use vet::{Valid, Vet};

// A valid username consists of between 3 and 32 alphanumeric characters
#[derive(Debug)]
struct Username(String);

#[derive(Debug, PartialEq)]
enum InvalidUsername {
    TooShort, // Under 3 characters
    TooLong, // Over 3 characters
    InvalidChar, // Contains non-alphanumeric character
}

impl Vet for Username {
    type Error = InvalidUsername;

    // Arbitrary logic to validate the Username type
    fn is_valid(&self) -> Result<(), Self::Error> {
        if self.0.len() < 3 {
            return Err(Self::Error::TooShort);
        }
        if self.0.len() > 32 {
            return Err(Self::Error::TooLong);
        }
        if self.0.chars().any(|c| !c.is_alphanumeric()) {
            return Err(Self::Error::InvalidChar);
        }
        Ok(())
    }
}

Vetted types provide safety guarantees for the types contents.

fn main() {
    let args: Vec<String> = env::args().collect();
    let username = Username(args[1].clone());

    // If successfully vetted, the username will be wrapped in a `Valid` struct
    match username.vet() {
        Ok(username) => create_account(username),
        Err(InvalidUsername::TooShort) => eprintln!("Username too short! (3 min)"),
        Err(InvalidUsername::TooLong) => eprintln!("Username too long! (32 max)"),
        Err(InvalidUsername::InvalidChar) => eprintln!("Username contains invalid characters!"),
    }
}

// Any `Valid<Username>` passed is guaranteed
// to have met the arbitrary validation checks.
fn create_account(username: Valid<Username>) {
    println!("Account {:?} created", username);
}

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.