use std::{
borrow::{Borrow, Cow},
fmt, hash,
ops::Deref,
};
use oxc_allocator::{Allocator, CloneIn, Dummy, FromIn, StringBuilder as ArenaStringBuilder};
#[cfg(feature = "serialize")]
use oxc_estree::{ESTree, Serializer as ESTreeSerializer};
#[cfg(feature = "serialize")]
use serde::{Serialize, Serializer as SerdeSerializer};
use crate::CompactStr;
#[repr(transparent)]
#[derive(Clone, Copy, Eq)]
pub struct Atom<'a>(&'a str);
impl Atom<'static> {
#[expect(clippy::inline_always)]
#[inline(always)] pub const fn new_const(s: &'static str) -> Self {
Atom(s)
}
#[inline]
pub const fn empty() -> Self {
Self::new_const("")
}
}
impl<'a> Atom<'a> {
#[expect(clippy::inline_always)]
#[inline(always)] pub fn as_str(&self) -> &'a str {
self.0
}
#[inline]
pub fn into_string(self) -> String {
String::from(self.as_str())
}
#[inline]
pub fn into_compact_str(self) -> CompactStr {
CompactStr::new(self.as_str())
}
#[inline]
pub fn to_compact_str(self) -> CompactStr {
CompactStr::new(self.as_str())
}
#[expect(clippy::inline_always)]
#[inline(always)]
pub fn from_strs_array_in<const N: usize>(
strings: [&str; N],
allocator: &'a Allocator,
) -> Atom<'a> {
Self::from(allocator.alloc_concat_strs_array(strings))
}
#[inline]
pub fn from_cow_in(value: &Cow<'a, str>, allocator: &'a Allocator) -> Atom<'a> {
match value {
Cow::Borrowed(s) => Atom::from(*s),
Cow::Owned(s) => Atom::from_in(s, allocator),
}
}
}
impl<'new_alloc> CloneIn<'new_alloc> for Atom<'_> {
type Cloned = Atom<'new_alloc>;
#[inline]
fn clone_in(&self, allocator: &'new_alloc Allocator) -> Self::Cloned {
Atom::from_in(self.as_str(), allocator)
}
}
impl<'a> Dummy<'a> for Atom<'a> {
#[expect(clippy::inline_always)]
#[inline(always)]
fn dummy(_allocator: &'a Allocator) -> Self {
Atom::empty()
}
}
impl<'alloc> FromIn<'alloc, &Atom<'alloc>> for Atom<'alloc> {
#[expect(clippy::inline_always)]
#[inline(always)] fn from_in(s: &Atom<'alloc>, _: &'alloc Allocator) -> Self {
*s
}
}
impl<'alloc> FromIn<'alloc, &str> for Atom<'alloc> {
#[inline]
fn from_in(s: &str, allocator: &'alloc Allocator) -> Self {
Self::from(allocator.alloc_str(s))
}
}
impl<'alloc> FromIn<'alloc, String> for Atom<'alloc> {
#[inline]
fn from_in(s: String, allocator: &'alloc Allocator) -> Self {
Self::from_in(s.as_str(), allocator)
}
}
impl<'alloc> FromIn<'alloc, &String> for Atom<'alloc> {
#[inline]
fn from_in(s: &String, allocator: &'alloc Allocator) -> Self {
Self::from_in(s.as_str(), allocator)
}
}
impl<'alloc> FromIn<'alloc, Cow<'_, str>> for Atom<'alloc> {
#[inline]
fn from_in(s: Cow<'_, str>, allocator: &'alloc Allocator) -> Self {
Self::from_in(&*s, allocator)
}
}
impl<'a> From<&'a str> for Atom<'a> {
#[expect(clippy::inline_always)]
#[inline(always)] fn from(s: &'a str) -> Self {
Self(s)
}
}
impl<'alloc> From<ArenaStringBuilder<'alloc>> for Atom<'alloc> {
#[inline]
fn from(s: ArenaStringBuilder<'alloc>) -> Self {
Self::from(s.into_str())
}
}
impl<'a> From<Atom<'a>> for &'a str {
#[expect(clippy::inline_always)]
#[inline(always)] fn from(s: Atom<'a>) -> Self {
s.as_str()
}
}
impl From<Atom<'_>> for CompactStr {
#[inline]
fn from(val: Atom<'_>) -> Self {
val.into_compact_str()
}
}
impl From<Atom<'_>> for String {
#[inline]
fn from(val: Atom<'_>) -> Self {
val.into_string()
}
}
impl<'a> From<Atom<'a>> for Cow<'a, str> {
#[inline]
fn from(value: Atom<'a>) -> Self {
Cow::Borrowed(value.as_str())
}
}
impl Deref for Atom<'_> {
type Target = str;
#[expect(clippy::inline_always)]
#[inline(always)] fn deref(&self) -> &Self::Target {
self.as_str()
}
}
impl AsRef<str> for Atom<'_> {
#[expect(clippy::inline_always)]
#[inline(always)] fn as_ref(&self) -> &str {
self.as_str()
}
}
impl Borrow<str> for Atom<'_> {
#[expect(clippy::inline_always)]
#[inline(always)] fn borrow(&self) -> &str {
self.as_str()
}
}
impl<T: AsRef<str>> PartialEq<T> for Atom<'_> {
#[inline]
fn eq(&self, other: &T) -> bool {
self.as_str() == other.as_ref()
}
}
impl PartialEq<Atom<'_>> for &str {
#[inline]
fn eq(&self, other: &Atom<'_>) -> bool {
*self == other.as_str()
}
}
impl PartialEq<str> for Atom<'_> {
#[inline]
fn eq(&self, other: &str) -> bool {
self.as_str() == other
}
}
impl PartialEq<Atom<'_>> for Cow<'_, str> {
#[inline]
fn eq(&self, other: &Atom<'_>) -> bool {
self.as_ref() == other.as_str()
}
}
impl hash::Hash for Atom<'_> {
#[inline]
fn hash<H: hash::Hasher>(&self, hasher: &mut H) {
self.as_str().hash(hasher);
}
}
impl fmt::Debug for Atom<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(self.as_str(), f)
}
}
impl fmt::Display for Atom<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(self.as_str(), f)
}
}
#[cfg(feature = "serialize")]
impl Serialize for Atom<'_> {
#[inline] fn serialize<S: SerdeSerializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
Serialize::serialize(self.as_str(), serializer)
}
}
#[cfg(feature = "serialize")]
impl ESTree for Atom<'_> {
#[inline] fn serialize<S: ESTreeSerializer>(&self, serializer: S) {
ESTree::serialize(self.as_str(), serializer);
}
}
#[macro_export]
macro_rules! format_atom {
($alloc:expr, $($arg:tt)*) => {{
use ::std::{write, fmt::Write};
use $crate::{Atom, __internal::ArenaStringBuilder};
let mut s = ArenaStringBuilder::new_in($alloc);
write!(s, $($arg)*).unwrap();
Atom::from(s)
}}
}