Crate non

Crate non 

Source
Expand description

Type-safe wrappers for strings with compile-time guarantees.

This crate provides zero-cost wrapper types around String that encode specific invariants at the type level, preventing invalid states at compile time.

§Types

  • NonEmpty - A string that is guaranteed to contain at least one character
  • NonBlank - A string that is guaranteed to be non-empty and contain at least one non-whitespace character
  • ASCII - A string that is guaranteed to contain only ASCII characters
  • ExactLength - A string that is guaranteed to have exactly N characters
  • Trimmed - A string that is guaranteed to be trimmed (no leading or trailing whitespace)
  • Alphanumeric - A string that is guaranteed to only contain alphanumeric characters
  • LowerCase - A string that is guaranteed to be all lowercase
  • UpperCase - A string that is guaranteed all uppercase

§Examples

use non::{NonEmpty, NonBlank, ASCII, ExactLength};

// NonEmpty rejects empty strings
let valid = NonEmpty::new("hello".to_string()).unwrap();
let invalid = NonEmpty::new(String::new());
assert!(invalid.is_none());

// NonBlank rejects whitespace-only strings
let valid = NonBlank::new("hello".to_string()).unwrap();
let invalid = NonBlank::new("   ".to_string());
assert!(invalid.is_none());

// ASCII rejects non-ASCII characters
let valid = ASCII::new("hello".to_string()).unwrap();
let invalid = ASCII::new("cześć".to_string());
assert!(invalid.is_none());

// ExactLength rejects strings that don't have exactly N chars
let valid = ExactLength::<2>::new("hi".to_string()).unwrap();
let invalid = ExactLength::<3>::new("helo world".to_string());
assert!(invalid.is_none());

§Deref Behavior

All types implement Deref<Target = String>, giving you access to all String methods:

use non::NonEmpty;

let s = NonEmpty::new("hello world".to_string()).unwrap();
assert_eq!(s.len(), 11);
assert!(s.contains("world"));

§Conversions

All types can be converted back to String using Into:

use non::NonEmpty;

let non_empty = NonEmpty::new("hello".to_string()).unwrap();
let s: String = non_empty.into();

§Composing Wrappers

You can compose multiple wrappers together:

use non::{NonEmpty, ASCII};

// Create a non-empty ASCII string
let non_empty = NonEmpty::new("hello".to_string()).unwrap();
let ascii_non_empty = ASCII::new(non_empty).unwrap();

// Type is: ASCII<NonEmpty<String>>
assert_eq!(ascii_non_empty.as_ref(), "hello");

§Stripping Wrappers

Use the Strip trait to remove one layer of wrapping:

use non::{NonEmpty, ASCII, Strip};

let non_empty = NonEmpty::new("hello".to_string()).unwrap();
let ascii_non_empty = ASCII::new(non_empty).unwrap();

// Strip the NonEmpty layer: ASCII<NonEmpty<String>> -> ASCII<String>
let ascii_only = ascii_non_empty.strip();
assert_eq!(ascii_only, ASCII::new("hello".to_string()).unwrap());

§no_std Support

This crate supports no_std environments with alloc. To use in no_std:

[dependencies]
non = { version = "0.1", default-features = false }

§Features

  • std (default) - Enables standard library support

Structs§

ASCII
A string that is known to be ascii encoded
Alphanumeric
A string that is known to contain only alphanumeric characters
ExactLength
A string that is known to have exactly N characters
LowerCase
A string that is known to be all lowercase
NonBlank
A string that is known to not be only whitespace
NonEmpty
A string that is known to not be empty
Trimmed
A string that is known to be trimmed
UpperCase
A string that is known to be all uppercase

Traits§

Strip
Strips one layer of wrapper from nested types.