valistr/lib.rs
1//! This crate provides a procedural macro to create immutable string wrapper types with values validated with regexes.
2//!
3//! ## Usage
4//!
5//! To use this crate, use `#[valistr(regex)]` attribute on a unit struct. The regex should be a string literal. Anchors `^` and `$` are automatically added to the regex if they are not present.
6//!
7//! The following traits are implemented for the struct:
8//! - `Deref<Target = String>`
9//! - `DerefMut<Target = String>`
10//! - `Debug`
11//! - `Display`
12//! - `TryFrom<&str>`
13//! - `TryFrom<String>`
14//!
15//! The following methods are generated for the struct:
16//! - `fn new(value: impl Into<String>) -> Option<Self>`
17//! - `fn validator() -> &'static Regex`
18//! - `fn get_<name>(&self) -> Option<&str>` for each named capture group `<name>` in the regex.
19//! - The method is generated only if the name matches `[a-z][a-z0-9_]*`. This constraint guarantees the generated method name is a valid and clean Rust identifier.
20//!
21//! ## Example
22//!
23//! ```rust
24//! use valistr::valistr;
25//!
26//! #[valistr("[A-Za-z]*")]
27//! struct AsciiLetters;
28//!
29//! assert!(AsciiLetters::new("Hello").is_some());
30//! assert!(AsciiLetters::new("Hello, world!").is_none());
31//! ```
32//!
33pub use valistr_proc_macro::valistr;