1 use std::{borrow::Cow, fmt, io}; 2 3 use crate::{ 4 result::FResult, 5 serialize::{Deserialize, Serialize}, 6 }; 7 8 #[derive(Clone, Debug, PartialEq, Eq)] 9 pub(crate) struct Ident(Cow<'static, str>); 10 11 impl Ident { new_str(s: &'static str) -> Self12 pub(crate) fn new_str(s: &'static str) -> Self { 13 Self(Cow::Borrowed(s)) 14 } 15 new_string(s: String) -> Self16 pub(crate) fn new_string(s: String) -> Self { 17 Self(Cow::Owned(s)) 18 } 19 as_str(&self) -> &str20 pub(crate) fn as_str(&self) -> &str { 21 self.0.as_ref() 22 } 23 is_prefix_unit(&self) -> bool24 pub(crate) fn is_prefix_unit(&self) -> bool { 25 // when changing this also make sure to change number output formatting 26 // lexer identifier splitting 27 ["$", "\u{a3}", "\u{a5}"].contains(&&*self.0) 28 } 29 serialize(&self, write: &mut impl io::Write) -> FResult<()>30 pub(crate) fn serialize(&self, write: &mut impl io::Write) -> FResult<()> { 31 self.0.as_ref().serialize(write) 32 } 33 deserialize(read: &mut impl io::Read) -> FResult<Self>34 pub(crate) fn deserialize(read: &mut impl io::Read) -> FResult<Self> { 35 Ok(Self(Cow::Owned(String::deserialize(read)?))) 36 } 37 } 38 39 impl From<String> for Ident { from(value: String) -> Self40 fn from(value: String) -> Self { 41 Self(Cow::Owned(value)) 42 } 43 } 44 45 impl From<&'static str> for Ident { from(value: &'static str) -> Self46 fn from(value: &'static str) -> Self { 47 Self(Cow::Borrowed(value)) 48 } 49 } 50 51 impl fmt::Display for Ident { fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result52 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 53 write!(f, "{}", self.0) 54 } 55 } 56