feat: less clone on symbol

This commit is contained in:
2025-05-15 18:19:16 +08:00
parent 3e7a8a1c05
commit 864be73e77
12 changed files with 195 additions and 183 deletions

View File

@@ -1,11 +1,7 @@
use std::fmt::{Display, Formatter, Result as FmtResult};
use std::ops::Deref;
use std::sync::LazyLock;
use derive_more::Constructor;
use ecow::EcoString;
use regex::Regex;
#[derive(Clone, Debug, PartialEq, Constructor)]
pub struct Catchable {
@@ -18,46 +14,3 @@ impl Display for Catchable {
}
}
#[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord, Constructor)]
pub struct Symbol(EcoString);
impl<T: Into<EcoString>> From<T> for Symbol {
fn from(value: T) -> Self {
Symbol(value.into())
}
}
impl Display for Symbol {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
if self.normal() {
write!(f, r#""{}""#, self.0)
} else {
write!(f, "{}", self.0)
}
}
}
static REGEX: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r#"^[a-zA-Z\_][a-zA-Z0-9\_\'\-]*$"#).unwrap());
impl Symbol {
fn normal(&self) -> bool {
!REGEX.is_match(self)
}
}
impl Deref for Symbol {
type Target = str;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl Symbol {
pub fn into_inner(self) -> EcoString {
self.0
}
pub fn as_inner(&self) -> &EcoString {
&self.0
}
}