1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
use std::{
    borrow::{Borrow, Cow},
    fmt, hash,
    ops::{Deref, Index},
};

use compact_str::CompactString;
#[cfg(feature = "serialize")]
use serde::{Serialize, Serializer};

use crate::Span;
use oxc_allocator::{Allocator, CloneIn, FromIn};

#[cfg(feature = "serialize")]
#[wasm_bindgen::prelude::wasm_bindgen(typescript_custom_section)]
const TS_APPEND_CONTENT: &'static str = r#"
export type Atom = string;
export type CompactStr = string;
"#;

/// Maximum length for inline string, which can be created with [`CompactStr::new_const`].
pub const MAX_INLINE_LEN: usize = 16;

/// An inlinable string for oxc_allocator.
///
/// Use [CompactStr] with [Atom::to_compact_str] or [Atom::into_compact_str] for
/// the lifetimeless form.
#[derive(Clone, Eq)]
#[cfg_attr(feature = "serialize", derive(Serialize))]
#[cfg_attr(feature = "serialize", serde(transparent))]
pub struct Atom<'a>(&'a str);

impl Atom<'static> {
    #[inline]
    pub const fn empty() -> Self {
        Atom("")
    }
}

impl<'a> Atom<'a> {
    #[inline]
    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())
    }
}

impl<'old_alloc, 'new_alloc> CloneIn<'new_alloc> for Atom<'old_alloc> {
    type Cloned = Atom<'new_alloc>;

    fn clone_in(&self, alloc: &'new_alloc Allocator) -> Self::Cloned {
        Atom::from_in(self.as_str(), alloc)
    }
}

impl<'a, 'b> FromIn<'a, &'b Atom<'a>> for Atom<'a> {
    fn from_in(s: &'b Atom<'a>, _: &'a Allocator) -> Self {
        Self::from(s.0)
    }
}

impl<'a, 'b> FromIn<'a, &'b str> for Atom<'a> {
    fn from_in(s: &'b str, alloc: &'a Allocator) -> Self {
        Self::from(oxc_allocator::String::from_str_in(s, alloc).into_bump_str())
    }
}

impl<'a> FromIn<'a, String> for Atom<'a> {
    fn from_in(s: String, alloc: &'a Allocator) -> Self {
        Self::from_in(s.as_str(), alloc)
    }
}

impl<'a> FromIn<'a, &String> for Atom<'a> {
    fn from_in(s: &String, alloc: &'a Allocator) -> Self {
        Self::from_in(s.as_str(), alloc)
    }
}

impl<'a, 'b> FromIn<'a, Cow<'b, str>> for Atom<'a> {
    fn from_in(s: Cow<'b, str>, alloc: &'a Allocator) -> Self {
        Self::from_in(&*s, alloc)
    }
}

impl<'a> From<&'a str> for Atom<'a> {
    fn from(s: &'a str) -> Self {
        Self(s)
    }
}

impl<'a> From<Atom<'a>> for CompactStr {
    #[inline]
    fn from(val: Atom<'a>) -> Self {
        val.into_compact_str()
    }
}

impl<'a> From<Atom<'a>> for String {
    #[inline]
    fn from(val: Atom<'a>) -> 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<'a> Deref for Atom<'a> {
    type Target = str;

    fn deref(&self) -> &Self::Target {
        self.as_str()
    }
}

impl<'a> AsRef<str> for Atom<'a> {
    fn as_ref(&self) -> &str {
        self.as_str()
    }
}

impl<'a> Borrow<str> for Atom<'a> {
    fn borrow(&self) -> &str {
        self.as_str()
    }
}

impl<'a, T: AsRef<str>> PartialEq<T> for Atom<'a> {
    fn eq(&self, other: &T) -> bool {
        self.as_str() == other.as_ref()
    }
}

impl<'a> PartialEq<Atom<'a>> for &str {
    fn eq(&self, other: &Atom<'a>) -> bool {
        *self == other.as_str()
    }
}

impl<'a> PartialEq<str> for Atom<'a> {
    fn eq(&self, other: &str) -> bool {
        self.as_str() == other
    }
}

impl<'a> PartialEq<Atom<'a>> for Cow<'_, str> {
    fn eq(&self, other: &Atom<'a>) -> bool {
        self.as_ref() == other.as_str()
    }
}
impl<'a> PartialEq<&Atom<'a>> for Cow<'_, str> {
    fn eq(&self, other: &&Atom<'a>) -> bool {
        self.as_ref() == other.as_str()
    }
}

impl<'a> hash::Hash for Atom<'a> {
    fn hash<H: hash::Hasher>(&self, hasher: &mut H) {
        self.as_str().hash(hasher);
    }
}

impl<'a> fmt::Debug for Atom<'a> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Debug::fmt(self.as_str(), f)
    }
}

impl<'a> fmt::Display for Atom<'a> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Display::fmt(self.as_str(), f)
    }
}

/// Lifetimeless version of [`Atom<'_>`] which owns its own string data allocation.
///
/// [`CompactStr`] is immutable. Use [`CompactStr::into_string`] for a mutable
/// [`String`].
///
/// Currently implemented as just a wrapper around [`compact_str::CompactString`],
/// but will be reduced in size with a custom implementation later.
#[derive(Clone, Eq, PartialOrd, Ord)]
#[cfg_attr(feature = "serialize", derive(serde::Deserialize))]
pub struct CompactStr(CompactString);

impl CompactStr {
    /// Create a new [`CompactStr`].
    ///
    /// If `&str` is `'static` and no more than [`MAX_INLINE_LEN`] bytes,
    /// prefer [`CompactStr::new_const`] which creates the [`CompactStr`] at
    /// compile time.
    ///
    /// # Examples
    /// ```
    /// let s = CompactStr::new("long string which can't use new_const for");
    /// ```
    #[inline]
    pub fn new(s: &str) -> Self {
        Self(CompactString::new(s))
    }

    /// Create a [`CompactStr`] at compile time.
    ///
    /// String must be no longer than [`MAX_INLINE_LEN`] bytes.
    ///
    /// Prefer this over [`CompactStr::new`] or [`CompactStr::from`] where
    /// string is `'static` and not longer than [`MAX_INLINE_LEN`] bytes.
    ///
    /// # Panics
    /// Panics if string is longer than [`MAX_INLINE_LEN`] bytes.
    ///
    /// # Examples
    /// ```
    /// const S: CompactStr = CompactStr::new_const("short");
    /// ```
    #[inline]
    pub const fn new_const(s: &'static str) -> Self {
        assert!(s.len() <= MAX_INLINE_LEN);
        Self(CompactString::const_new(s))
    }

    /// Get string content as a `&str` slice.
    #[inline]
    pub fn as_str(&self) -> &str {
        self.0.as_str()
    }

    /// Convert a [`CompactStr`] into a [`String`].
    #[inline]
    pub fn into_string(self) -> String {
        self.0.into_string()
    }

    /// Get length of [`CompactStr`].
    ///
    /// # Examples
    /// ```
    /// use oxc_span::CompactStr;
    ///
    /// assert_eq!(CompactStr::new("").len(), 0);
    /// assert_eq!(CompactStr::new_const("").len(), 0);
    /// assert_eq!(CompactStr::new("hello").len(), 5);
    /// ```
    #[inline]
    pub fn len(&self) -> usize {
        self.0.len()
    }

    /// Check if a [`CompactStr`] is empty (0 length).
    ///
    /// # Examples
    /// ```
    /// use oxc_span::CompactStr;
    ///
    /// assert!(CompactStr::new("").is_empty());
    /// assert!(!CompactStr::new("hello").is_empty());
    /// ```
    #[inline]
    pub fn is_empty(&self) -> bool {
        self.0.is_empty()
    }
}

impl From<&str> for CompactStr {
    fn from(s: &str) -> Self {
        Self(CompactString::from(s))
    }
}

impl From<String> for CompactStr {
    fn from(s: String) -> Self {
        Self(CompactString::from(s))
    }
}

impl<'s> From<&'s CompactStr> for Cow<'s, str> {
    fn from(value: &'s CompactStr) -> Self {
        Self::Borrowed(value.as_str())
    }
}

impl From<CompactStr> for Cow<'_, str> {
    fn from(value: CompactStr) -> Self {
        value.0.into()
    }
}
impl From<Cow<'_, str>> for CompactStr {
    fn from(value: Cow<'_, str>) -> Self {
        match value {
            Cow::Borrowed(s) => CompactStr::new(s),
            Cow::Owned(s) => CompactStr::from(s),
        }
    }
}

impl Deref for CompactStr {
    type Target = str;

    fn deref(&self) -> &Self::Target {
        self.as_str()
    }
}

impl AsRef<str> for CompactStr {
    fn as_ref(&self) -> &str {
        self.as_str()
    }
}

impl Borrow<str> for CompactStr {
    fn borrow(&self) -> &str {
        self.as_str()
    }
}

impl<T: AsRef<str>> PartialEq<T> for CompactStr {
    fn eq(&self, other: &T) -> bool {
        self.as_str() == other.as_ref()
    }
}

impl PartialEq<CompactStr> for &str {
    fn eq(&self, other: &CompactStr) -> bool {
        *self == other.as_str()
    }
}

impl PartialEq<CompactStr> for str {
    fn eq(&self, other: &CompactStr) -> bool {
        self == other.as_str()
    }
}

impl PartialEq<str> for CompactStr {
    fn eq(&self, other: &str) -> bool {
        self.as_str() == other
    }
}

impl PartialEq<CompactStr> for Cow<'_, str> {
    fn eq(&self, other: &CompactStr) -> bool {
        self.as_ref() == other.as_str()
    }
}

impl Index<Span> for CompactStr {
    type Output = str;

    fn index(&self, index: Span) -> &Self::Output {
        &self.0[index]
    }
}

impl hash::Hash for CompactStr {
    fn hash<H: hash::Hasher>(&self, hasher: &mut H) {
        self.as_str().hash(hasher);
    }
}

impl fmt::Debug for CompactStr {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Debug::fmt(self.as_str(), f)
    }
}

impl fmt::Display for CompactStr {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Display::fmt(self.as_str(), f)
    }
}

#[cfg(feature = "serialize")]
impl Serialize for CompactStr {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        serializer.serialize_str(self.as_str())
    }
}

#[cfg(feature = "schemars")]
impl schemars::JsonSchema for CompactStr {
    fn is_referenceable() -> bool {
        false
    }

    fn schema_name() -> std::string::String {
        "String".to_string()
    }

    fn schema_id() -> Cow<'static, str> {
        Cow::Borrowed("String")
    }

    fn json_schema(gen: &mut schemars::gen::SchemaGenerator) -> schemars::schema::Schema {
        <&str>::json_schema(gen)
    }
}

#[cfg(test)]
mod test {
    use super::CompactStr;

    #[test]
    fn test_compactstr_eq() {
        let foo = CompactStr::new("foo");
        assert_eq!(foo, "foo");
        assert_eq!(&foo, "foo");
        assert_eq!("foo", foo);
        assert_eq!("foo", &foo);
    }
}