non
Type-safe wrappers for strings with compile-time guarantees.
Overview
non provides zero-cost wrapper types around String that encode
specific invariants at the type level, preventing invalid states at runtime.
Wrappers guarantee things like non-empty strings, ASCII-only strings, exact length, and more — all enforced when creating the wrapper.
Features
NonEmpty— ensures the string is not emptyNonBlank— ensures the string contains at least one non-whitespace characterASCII— ensures the string contains only ASCII charactersExactLength<N>— ensures the string is exactlyNcharacters longLowerCase— ensures the string is all lowercaseUpperCase— ensures the string is all uppercaseTrimmed— ensures no leading or trailing whitespaceAlphanumeric— ensures string contains only alphanumeric characters
Supports composition of wrappers, e.g. ASCII<NonEmpty<String>>.
Works in no_std environments.
Usage
Add to your Cargo.toml:
[]
= "0.1"
Basic usage:
use ;
let valid = new.unwrap;
assert!;
let valid = new.unwrap;
assert!;
let valid = ASCIInew.unwrap;
assert!;
let valid = new.unwrap;
assert!;
You can compose wrappers:
use ;
let non_empty = new.unwrap;
let ascii_non_empty = ASCIInew.unwrap;
assert_eq!;
Deref and Conversion
All wrappers deref to the inner string, so you can use string methods:
use NonEmpty;
let s = new.unwrap;
assert!;
Convert back to String easily:
use NonEmpty;
let non_empty = new.unwrap;
let s: String = non_empty.into;
Stripping Wrappers
Use the Strip trait to remove one wrapper layer:
use ;
let non_empty = new.unwrap;
let ascii_non_empty = ASCIInew.unwrap;
let ascii_only = ascii_non_empty.strip;
assert_eq!;
no_std support
Works in no_std environments with alloc enabled:
[]
= { = "0.1", = false }
License
Licensed under MIT. See LICENSE for details.