Skip to content

Nutype 0.4.0

Choose a tag to compare

@greyblake greyblake released this 21 Nov 20:24
· 229 commits to master since this release

Acknowledgements

A heartfelt thanks to Daniyil Glushko for his invaluable assistance and exceptional work on this release. Daniyil, located in Zaporizhzhia, Ukraine, is a proficient Rust developer open to remote opportunities. I highly recommend reaching out to him for Rust development roles.

Changes

  • [FEATURE] Support of arbitrary inner types with custom sanitizers and validators.
  • [FEATURE] Add numeric validator greater
  • [FEATURE] Add numeric validator less
  • [BREAKING] Removal of asterisk derive
  • [BREAKING] Use commas to separate high level attributes
  • [BREAKING] Traits are derived with #[nutype(derive(Debug))]. The regular #[derive(Debug)] syntax is not supported anymore.
  • [BREAKING] Validator with has been renamed to predicate to reflect the boolean nature of its range
  • [BREAKING] String validator min_len has been renamed to len_char_min to reflect that is based on UTF8 chars.
  • [BREAKING] String validator max_len has been renamed to len_char_max to reflect that is based on UTF8 chars.
  • [BREAKING] Rename numeric validator max to less_or_equal
  • [BREAKING] Rename numeric validator min to greater_or_equal
  • [BREAKING] Rename error variants to follow the following formula: <ValidationRule>Violated. This implies the following renames:
    • TooShort -> LenCharMinViolated
    • TooLong -> LenCharMaxViolated
    • Empty -> NotEmptyViolated
    • RegexMismatch -> RegexViolated
    • Invalid -> PredicateViolated
    • TooBig -> LessOrEqualViolated
    • TooSmall -> GreaterOrEqualViolated
    • NotFinite -> FiniteViolated
  • Better error messages: in case of unknown attribute, validator or sanitizer the possible values are listed.
  • [FIX] Make derived Deserialize work with RON format

Feature highlights

Arbitrary inner type

Previously #[nutype] worked only with String, integers and floats.
Now it's possible to use it with any arbitrary type (e.g. Vec<String>):

#[nutype(
    validate(predicate = |friends| !friends.is_empty() ),
)]
pub struct Frieds(Vec<String>);

New numeric validators

Instead of former min and max integers and floats can now be validated with:

  • greater_or_equal - Inclusive lower bound
  • greater - Exclusive lower bound
  • less_or_equal - Inclusive upper bound
  • less - Exclusive upper bound

Example:

#[nutype(
    validate(
        greater_or_equal = 0,
        less_or_equal = 59,
    ),
)]
pub struct Minute(u8);

Derive

Deriving of traits now has to be done explicitly with #[nutype(derive(...))] instead of #[derive(...)]:

Example:

#[nutype(
    validate(with = |n| n % 2 == 1),
    derive(Debug, Clone, Copy)
)]
pub struct OddNumber(u64);

This makes it clear, that deriving is fully handled by #[nutype] and prevents a potential confusion.

Links