use std::fmt; use std::fmt::{Display, Formatter}; use thiserror::Error; const MAX_COUNT: usize = 128; #[derive(Debug, Clone, PartialEq)] pub struct Name(String); #[derive(Error, Debug, Clone, PartialEq)] pub enum NameError { #[error("input cannot be empty")] Empty, #[error("input length ({0}) exceeds maximum {}", MAX_COUNT)] TooLong(usize), } impl Name { pub fn new(raw: &str) -> Result { let len = raw.chars().count(); if len == 0 { return Err(NameError::Empty); } else if len > MAX_COUNT { return Err(NameError::TooLong(len)); } Ok(Self(raw.into())) } pub fn into_string(self) -> String { self.0 } } impl AsRef for Name { fn as_ref(&self) -> &str { &self.0 } } impl Display for Name { fn fmt(&self, f: &mut Formatter) -> fmt::Result { write!(f, "{}", self.0) } }