Release notes: Release Nutype 0.4.0 · greyblake/nutype · GitHub
Nutype is a Rust crate featuring a procedural macro designed to enhance the regular newtype pattern with additional capabilities such as sanitization and validation. This enhancement ensures that values can only be instantiated after successfully passing predefined checks.
Example 1:
Ensure that Hour
type hold value that lays in the 0..=23 range:
#[nutype(
validate(
greater_or_equal = 0,
less_or_equal = 23,
),
)]
pub struct Hour(u8);
Example 2:
Ensure that GuestList
is never empty and its values are alphabetically sorted:
#[nutype(
derive(Debug, PartialEq, Deref, AsRef),
sanitize(with = |mut guests| { guests.sort(); guests }),
validate(predicate = |guests| !guests.is_empty() ),
)]
pub struct GuestList(Vec<String>);