use serde::Serialize;
use crate::{span::Span, Spanned};
use std::{
cmp::{Ord, Ordering},
fmt,
hash::{Hash, Hasher},
sync::Arc,
};
pub trait Named {
fn name(&self) -> &BaseIdent;
}
#[derive(Clone)]
pub struct BaseIdent {
name_override_opt: Option<Arc<String>>,
span: Span,
is_raw_ident: bool,
}
impl BaseIdent {
pub fn as_str(&self) -> &str {
self.name_override_opt
.as_deref()
.map(|x| x.as_str())
.unwrap_or_else(|| self.span.as_str())
}
pub fn is_raw_ident(&self) -> bool {
self.is_raw_ident
}
pub fn name_override_opt(&self) -> Option<&str> {
self.name_override_opt.as_deref().map(|x| x.as_str())
}
pub fn new(span: Span) -> Ident {
let span = span.trim();
Ident {
name_override_opt: None,
span,
is_raw_ident: false,
}
}
pub fn new_no_trim(span: Span) -> Ident {
Ident {
name_override_opt: None,
span,
is_raw_ident: false,
}
}
pub fn new_with_raw(span: Span, is_raw_ident: bool) -> Ident {
let span = span.trim();
Ident {
name_override_opt: None,
span,
is_raw_ident,
}
}
pub fn new_with_override(name_override: String, span: Span) -> Ident {
Ident {
name_override_opt: Some(Arc::new(name_override)),
span,
is_raw_ident: false,
}
}
pub fn new_no_span(name: String) -> Ident {
Ident {
name_override_opt: Some(Arc::new(name)),
span: Span::dummy(),
is_raw_ident: false,
}
}
pub fn dummy() -> Ident {
Ident {
name_override_opt: Some(Arc::new("foo".into())),
span: Span::dummy(),
is_raw_ident: false,
}
}
}
pub type Ident = BaseIdent;
impl Serialize for Ident {
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
use serde::ser::SerializeStruct;
let mut state = serializer.serialize_struct("Ident", 2)?;
state.serialize_field("to_string", &self.to_string())?;
state.serialize_field("span", &self.span)?;
state.end()
}
}
impl Hash for Ident {
fn hash<H: Hasher>(&self, state: &mut H) {
self.as_str().hash(state);
}
}
impl PartialEq for Ident {
fn eq(&self, other: &Self) -> bool {
self.as_str() == other.as_str()
}
}
impl Ord for Ident {
fn cmp(&self, other: &Self) -> Ordering {
self.as_str().cmp(other.as_str())
}
}
impl PartialOrd for Ident {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Eq for Ident {}
impl Spanned for Ident {
fn span(&self) -> Span {
self.span.clone()
}
}
impl fmt::Display for Ident {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
write!(formatter, "{}", self.as_str())
}
}
impl fmt::Debug for Ident {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
write!(formatter, "{}", self.as_str())
}
}
#[derive(Debug, Clone)]
pub struct IdentUnique(BaseIdent);
impl From<Ident> for IdentUnique {
fn from(item: Ident) -> Self {
IdentUnique(item)
}
}
impl From<&Ident> for IdentUnique {
fn from(item: &Ident) -> Self {
IdentUnique(item.clone())
}
}
impl From<&IdentUnique> for Ident {
fn from(item: &IdentUnique) -> Self {
Ident {
name_override_opt: item.0.name_override_opt().map(|s| Arc::new(s.to_string())),
span: item.0.span(),
is_raw_ident: item.0.is_raw_ident(),
}
}
}
impl Hash for IdentUnique {
fn hash<H: Hasher>(&self, state: &mut H) {
self.0.span().hash(state);
self.0.as_str().hash(state);
}
}
impl PartialEq for IdentUnique {
fn eq(&self, other: &Self) -> bool {
self.0.as_str() == other.0.as_str() && self.0.span() == other.0.span()
}
}
impl Ord for IdentUnique {
fn cmp(&self, other: &Self) -> Ordering {
self.0
.span()
.cmp(&other.0.span())
.then(self.0.as_str().cmp(other.0.as_str()))
}
}
impl PartialOrd for IdentUnique {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Eq for IdentUnique {}
impl Spanned for IdentUnique {
fn span(&self) -> Span {
self.0.span()
}
}
impl fmt::Display for IdentUnique {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
write!(formatter, "{}", self.0.as_str())
}
}