NameStr

Struct NameStr 

Source
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>

Source

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;
Source

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 allowed
Source

pub fn as_str(&self) -> &str

Returns the validated name as a string slice.

§Examples
use tnid::NameStr;

let name = NameStr::new("user").unwrap();
assert_eq!(name.as_str(), "user");

Auto 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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V