2 unstable releases

new 0.2.0 Jan 26, 2025
0.1.0 Jan 15, 2025

#1904 in Procedural macros

Download history 130/week @ 2025-01-15 96/week @ 2025-01-22

226 downloads per month
Used in seventy

Apache-2.0

22KB
270 lines

Seventy

Rust newtype sanitization & validation

docs.rs     crates.io     github

Seventy is a simple newtype sanitizer and validator.

  • Why newtypes?

    Newtypes provide compile-time guarantees that your program is using the correct values by wrapping existing types.

  • Why sanitize?

    Newtypes are sanitized during construction, ensuring the values conform to the formats you expect.

  • Why validate?

    Newtypes are validated during construction, ensuring it's impossible to create a newtype with values that don't meet the defined rules.

There is no error handling. If you need to know why the newtype couldn't be created, this crate isn't for you.

Usage

The example below first trims the string and then validates if the trimmed string is both alphanumeric and between 5 to 20 characters long. The display upgrade automatically implements the Display trait.

use seventy::{
    builtins::{compare::*, string::*},
    seventy, Newtype,
};

#[seventy(
    upgrades(display),
    sanitize(trim),
    validate(alphanumeric, length::chars(within(5..=20))),
)]
pub struct Username(String);

assert_eq!(
    Username::try_new("   username   ").unwrap().into_inner(),
    "username"
);

assert!(Username::try_new("   u$ername   ").is_err());

See the examples directory for more!

Dependencies

~220–670KB
~16K SLoC