use crate::parser::errors::ParserError; use std::str::FromStr; use tinystr::TinyStr4; #[derive(Debug, PartialEq, Eq, Clone, Hash, PartialOrd, Ord, Copy)] pub struct Script(TinyStr4); impl Script { pub fn from_bytes(v: &[u8]) -> Result { let slen = v.len(); let s = TinyStr4::from_bytes(v).map_err(|_| ParserError::InvalidSubtag)?; if slen != 4 || !s.is_ascii_alphabetic() { return Err(ParserError::InvalidSubtag); } Ok(Self(s.to_ascii_titlecase())) } pub fn as_str(&self) -> &str { self.0.as_str() } /// # Safety /// /// This function accepts any u64 that is exected to be a valid /// `TinyStr4` and a valid `Script` subtag. pub const unsafe fn from_raw_unchecked(v: u32) -> Self { Self(TinyStr4::new_unchecked(v)) } } impl From