use crate::common::errors::DomainError; #[derive(Debug, Clone, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)] pub struct Username(String); impl Username { pub fn new(value: impl Into) -> Result { let value = value.into(); if value.len() < 2 || value.len() > 64 { return Err(DomainError::Validation( "Username must be 2-64 characters".into(), )); } if !value .chars() .all(|c| c.is_alphanumeric() || c == '_' || c == '-' || c == '.') { return Err(DomainError::Validation( "Username may only contain alphanumeric, underscore, dash, or dot".into(), )); } Ok(Self(value)) } pub fn as_str(&self) -> &str { &self.0 } } impl std::fmt::Display for Username { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "{}", self.0) } }