pub struct NameStr<'a>(/* private fields */);Expand description
A validated TNID name string.
This type wraps a string slice and ensures it meets all TNID name requirements:
- Length between 1-4 characters (inclusive)
- ASCII only
- Only characters from the allowed set: digits 0-4 and lowercase letters a-z
§Examples
use tnid::NameStr;
// Runtime validation with new()
let name = NameStr::new("user").unwrap();
assert_eq!(name.as_str(), "user");
// Invalid names return None
assert!(NameStr::new("").is_none());
assert!(NameStr::new("CAPS").is_none());Implementations§
Source§impl<'a> NameStr<'a>
impl<'a> NameStr<'a>
Sourcepub const fn new_const(s: &'static str) -> Self
pub const fn new_const(s: &'static str) -> Self
Creates a new NameStr with compile-time validation when used in a const context.
This method performs validation and will panic if the name is invalid. When used
in a const context (like defining a TnidName implementation),
the panic will occur at compile time. If used at runtime, it will panic the program.
Prefer using new() for runtime validation which returns an Option
instead of panicking.
§Panics
Panics if the name:
- Is not 1-4 characters long
- Contains non-ASCII characters
- Contains characters outside the allowed set (0-4, a-z)
§Examples
use tnid::{NameStr, TnidName};
// Used in a const context for TNIDName (validated at compile time)
struct User;
impl TnidName for User {
const ID_NAME: NameStr<'static> = NameStr::new_const("user");
}This will fail to compile:
ⓘ
use tnid::{NameStr, TNIDName, TNID};
struct Invalid;
impl TnidName for Invalid {
const ID_NAME: NameStr<'static> = NameStr::new_const("INVALID");
}
// This actually uses the const and triggers the compile-time check
let _ = Invalid::ID_NAME;Sourcepub fn new(s: &'a str) -> Option<Self>
pub fn new(s: &'a str) -> Option<Self>
Creates a new NameStr with runtime validation.
Returns Some(NameStr) if the string is a valid TNID name, or None if it’s invalid.
§Examples
use tnid::NameStr;
// Valid names (1-4 chars, digits 0-4 and lowercase a-z only)
assert!(NameStr::new("user").is_some());
assert!(NameStr::new("post").is_some());
assert!(NameStr::new("a").is_some());
assert!(NameStr::new("test").is_some());
assert!(NameStr::new("id0").is_some());
// Too short or too long
assert!(NameStr::new("").is_none());
assert!(NameStr::new("toolong").is_none());
// Invalid characters
assert!(NameStr::new("User").is_none()); // uppercase not allowed
assert!(NameStr::new("id5").is_none()); // digits 5-9 not allowed
assert!(NameStr::new("a-b").is_none()); // special chars not allowed
assert!(NameStr::new("café").is_none()); // non-ASCII not allowedAuto Trait Implementations§
impl<'a> Freeze for NameStr<'a>
impl<'a> RefUnwindSafe for NameStr<'a>
impl<'a> Send for NameStr<'a>
impl<'a> Sync for NameStr<'a>
impl<'a> Unpin for NameStr<'a>
impl<'a> UnwindSafe for NameStr<'a>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more