use std::{
cmp::Ordering,
fmt::{Debug, Display, Formatter},
hash::{Hash, Hasher},
};
use compact_str::CompactString;
use lady_deirdre::lexis::{TokenRef, NIL_TOKEN_REF};
use crate::runtime::{Origin, RustOrigin};
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Ident {
Rust(&'static RustIdent),
Script(ScriptIdent),
}
impl Debug for Ident {
#[inline(always)]
fn fmt(&self, formatter: &mut Formatter<'_>) -> std::fmt::Result {
match self {
Ident::Rust(ident) => Debug::fmt(*ident, formatter),
Ident::Script(ident) => Debug::fmt(ident, formatter),
}
}
}
impl Display for Ident {
#[inline(always)]
fn fmt(&self, formatter: &mut Formatter<'_>) -> std::fmt::Result {
match self {
Ident::Rust(ident) => Display::fmt(*ident, formatter),
Ident::Script(ident) => Display::fmt(ident, formatter),
}
}
}
impl From<&'static RustIdent> for Ident {
#[inline(always)]
fn from(value: &'static RustIdent) -> Self {
Self::Rust(value)
}
}
impl From<ScriptIdent> for Ident {
#[inline(always)]
fn from(value: ScriptIdent) -> Self {
Self::Script(value)
}
}
impl AsRef<str> for Ident {
#[inline(always)]
fn as_ref(&self) -> &str {
match self {
Self::Rust(ident) => ident.as_ref(),
Self::Script(ident) => ident.as_ref(),
}
}
}
impl Ident {
#[inline(always)]
pub(crate) fn from_string(string: impl Into<CompactString>) -> Self {
Self::Script(ScriptIdent {
origin: NIL_TOKEN_REF,
string: string.into(),
})
}
#[inline(always)]
pub fn origin(&self) -> Origin {
match self {
Self::Rust(ident) => Origin::from(ident.origin),
Self::Script(ident) => Origin::from(ident.origin),
}
}
}
#[derive(Clone, Copy)]
pub struct RustIdent {
pub origin: &'static RustOrigin,
pub string: &'static str,
}
impl Debug for RustIdent {
#[inline(always)]
fn fmt(&self, formatter: &mut Formatter<'_>) -> std::fmt::Result {
Debug::fmt(self.string, formatter)
}
}
impl Display for RustIdent {
#[inline(always)]
fn fmt(&self, formatter: &mut Formatter<'_>) -> std::fmt::Result {
Display::fmt(self.string, formatter)
}
}
impl AsRef<str> for RustIdent {
#[inline(always)]
fn as_ref(&self) -> &str {
self.string
}
}
impl PartialEq for RustIdent {
#[inline(always)]
fn eq(&self, other: &Self) -> bool {
self.string.eq(other.string)
}
}
impl Eq for RustIdent {}
impl Hash for RustIdent {
#[inline(always)]
fn hash<H: Hasher>(&self, state: &mut H) {
self.string.hash(state)
}
}
impl PartialOrd for RustIdent {
#[inline(always)]
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Ord for RustIdent {
#[inline(always)]
fn cmp(&self, other: &Self) -> Ordering {
self.string.cmp(&other.string)
}
}
#[derive(Clone)]
pub struct ScriptIdent {
origin: TokenRef,
string: CompactString,
}
impl Debug for ScriptIdent {
#[inline(always)]
fn fmt(&self, formatter: &mut Formatter<'_>) -> std::fmt::Result {
Debug::fmt(&self.string, formatter)
}
}
impl Display for ScriptIdent {
#[inline(always)]
fn fmt(&self, formatter: &mut Formatter<'_>) -> std::fmt::Result {
Display::fmt(&self.string, formatter)
}
}
impl AsRef<str> for ScriptIdent {
#[inline(always)]
fn as_ref(&self) -> &str {
self.string.as_ref()
}
}
impl PartialEq for ScriptIdent {
#[inline(always)]
fn eq(&self, other: &Self) -> bool {
self.string.eq(&other.string)
}
}
impl Eq for ScriptIdent {}
impl Hash for ScriptIdent {
#[inline(always)]
fn hash<H: Hasher>(&self, state: &mut H) {
self.string.hash(state)
}
}
impl PartialOrd for ScriptIdent {
#[inline(always)]
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Ord for ScriptIdent {
#[inline(always)]
fn cmp(&self, other: &Self) -> Ordering {
self.string.cmp(&other.string)
}
}
impl ScriptIdent {
#[inline(always)]
pub(crate) fn from_string(token_ref: TokenRef, string: impl Into<CompactString>) -> Self {
Self {
origin: token_ref,
string: string.into(),
}
}
}