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
use std::borrow::Cow;
use std::cmp::Ordering;
use std::fmt::{Debug, Display};
use std::ops::Deref;
use std::str::FromStr;

use interner::global::{GlobalString, StaticPooledString, StringPool};
use stylecs_shared::InvalidIdentifier;

/// A name that contains only `a-z`, `A-Z`, `0-9`, or `_` characters.
#[derive(Eq, PartialEq, Ord, PartialOrd, Hash, Clone)]
pub struct Identifier(GlobalString);

#[doc(hidden)]
pub static IDENTIFIERS: StringPool = StringPool::new();
static PRIVATE: StaticPooledString = IDENTIFIERS.get_static("_");

impl Identifier {
    /// Returns tne identifier used to designate a private authority.
    pub fn private() -> Self {
        Self(PRIVATE.clone())
    }

    /// Validates `name` and returns an `Identifier` if name does not contain
    /// any disallowed characters.
    ///
    /// # Errors
    ///
    /// Returns [`InvalidIdentifier`] if `name` contains any character that is
    /// not one of:
    ///
    /// - `a-z`
    /// - `A-Z`
    /// - `0-9`
    /// - `_`
    pub fn new<'a>(name: impl Into<Cow<'a, str>>) -> Result<Self, InvalidIdentifier> {
        let name = name.into();
        stylecs_shared::validate_identifier(&name).map(|_| Self(IDENTIFIERS.get(name)))
    }

    /// Validates `name` and returns an error if any invalid characters are
    /// encountered.
    ///
    /// # Errors
    ///
    /// Returns [`InvalidIdentifier`] if `name` contains any character that is
    /// not one of:
    ///
    /// - `a-z`
    /// - `A-Z`
    /// - `0-9`
    /// - `_`
    pub const fn validate(name: &str) -> Result<(), InvalidIdentifier> {
        stylecs_shared::validate_identifier(name)
    }
}

impl<'a> TryFrom<&'a str> for Identifier {
    type Error = InvalidIdentifier;

    fn try_from(value: &'a str) -> Result<Self, Self::Error> {
        Self::new(value)
    }
}

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

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

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

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

impl Deref for Identifier {
    type Target = str;

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

/// A globally unique name.
///
/// This structure has an [`authority`](Self::authority) and a [`name`](Self::name).
#[derive(Debug, Eq, PartialEq, Ord, PartialOrd, Hash, Clone)]
pub struct Name {
    /// The unique name of the source of this name. For example, this could be
    /// the name of the crate it was defined within.
    ///
    /// [`Identifier::private()`] is used as the authority when
    /// [`Name::private()`] is called.
    pub authority: Identifier,
    /// The locally unique name.
    ///
    /// This name only needs to be unique within its `authority`. For example,
    /// two authorities can define their own `color` components without
    /// conflicts.
    pub name: Identifier,
}

impl Name {
    /// Returns a new [`Name`] with `_` used as the authority.
    ///
    /// This is equivalent to calling `Name::new("_", name)`.
    ///
    /// # Errors
    ///
    /// Returns [`InvalidIdentifier`] if any invalid [`Identifier`] characters
    /// are encountered.
    pub fn private(name: impl Identifiable) -> Result<Self, InvalidIdentifier> {
        Self::new(Identifier::private(), name)
    }

    /// Returns a new [`Name`] using `authority` and `name`.
    ///
    /// Each `name` should be unique within the `authority` namespace.
    ///
    /// # Errors
    ///
    /// Returns [`InvalidIdentifier`] if any invalid [`Identifier`] characters
    /// are encountered.
    pub fn new(
        authority: impl Identifiable,
        name: impl Identifiable,
    ) -> Result<Self, InvalidIdentifier> {
        Ok(Self {
            authority: authority.into_identifier()?,
            name: name.into_identifier()?,
        })
    }
}

impl<'a> From<Name> for Cow<'a, Name> {
    fn from(value: Name) -> Self {
        Cow::Owned(value)
    }
}

impl<'a> From<&'a Name> for Cow<'a, Name> {
    fn from(value: &'a Name) -> Self {
        Cow::Borrowed(value)
    }
}

impl Display for Name {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        if PRIVATE != self.authority.0 {
            f.write_str(&self.authority)?;
            f.write_str("::")?;
        }
        f.write_str(&self.name)
    }
}

impl FromStr for Name {
    type Err = InvalidIdentifier;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        if let Some((authority, name)) = s.split_once("::") {
            let authority = Identifier::new(authority)?;
            let name = Identifier::new(name)?;
            Ok(Self { authority, name })
        } else {
            let name = Identifier::new(s)?;
            Ok(Self {
                authority: Identifier::private(),
                name,
            })
        }
    }
}

#[test]
fn name_strings() {
    let private = Name::private("private").unwrap();
    let private_string = private.to_string();
    assert_eq!(private_string, "private");
    let parsed: Name = private_string.parse().unwrap();
    assert_eq!(parsed, private);

    let qualified = Name::new("authority", "name").unwrap();
    let qualified_string = qualified.to_string();
    assert_eq!(qualified_string, "authority::name");
    let parsed: Name = qualified_string.parse().unwrap();
    assert_eq!(parsed, qualified);
}

pub trait Identifiable {
    fn into_identifier(self) -> Result<Identifier, InvalidIdentifier>;
}

impl<'a> Identifiable for &'a str {
    fn into_identifier(self) -> Result<Identifier, InvalidIdentifier> {
        Identifier::new(self)
    }
}

impl Identifiable for String {
    fn into_identifier(self) -> Result<Identifier, InvalidIdentifier> {
        Identifier::new(self)
    }
}

impl Identifiable for Identifier {
    fn into_identifier(self) -> Result<Identifier, InvalidIdentifier> {
        Ok(self)
    }
}

/// Returns a [`StaticName`], which allows for a name to be defined statically.
///
/// This allows a minor optimization such that [`Identifier`]s created always
/// exist.
#[macro_export]
macro_rules! static_name {
    ($private_name:expr) => {
        $crate::static_name!("_", $private_name)
    };
    ($authority:expr, $name:expr) => {
        $crate::StaticName::new(
            match $crate::Identifier::validate($authority) {
                Ok(_) => $crate::IDENTIFIERS.get_static($authority),
                Err(_) => panic!("invalid character in authority"),
            },
            match $crate::Identifier::validate($name) {
                Ok(_) => $crate::IDENTIFIERS.get_static($name),
                Err(_) => panic!("invalid character in name"),
            },
        )
    };
}

/// A statically defined [`Name`].
///
/// # Creating a static private name
///
/// ```rust
/// use stylecs::{static_name, Name, StaticName};
///
/// static PRIVATE: StaticName = static_name!("private");
/// assert_eq!(PRIVATE.to_name(), Name::private("private").unwrap());
/// ```
///
/// # Why use [`StaticName`]?
///
/// This type enables a minor optimization. Each [`Identifier`] guarantees that
/// only one copy of the string it points to exists. This allows for
/// optimizations when using names as keys in a hash map. To support this, each
/// time an [`Identifier`] is created, the global list must be first checked and
/// a copy of the existing value returned if it already exists.
///
/// This type allows performing this initialization once upon first access of
/// the [`StaticName`]. This type can be converted to [`Name`] using
/// `Into`/`From`.
pub struct StaticName {
    authority: StaticPooledString,
    name: StaticPooledString,
}

impl StaticName {
    #[doc(hidden)]
    pub const fn new(authority: StaticPooledString, name: StaticPooledString) -> Self {
        Self { authority, name }
    }

    /// Returns this static instance as a regular [`Name`].
    pub fn to_name(&self) -> Name {
        Name::from(self)
    }
}

impl<'a> From<&'a StaticName> for Name {
    fn from(value: &'a StaticName) -> Self {
        Self {
            authority: Identifier(value.authority.get().clone()),
            name: Identifier(value.name.get().clone()),
        }
    }
}

/// A [`Name`] type used for efficient lookups in ordered collections.
///
/// This type's [`Ord`] implementation provides a stable ordering that is
/// efficient and does not rely on string comparisons. However, it does not sort
/// ascending, while `Name`'s [`Ord`] implementation sorts ascending.
///
/// There is no benefit to using this type in a hash-based collection, so this
/// type does not implement [`Hash`].
#[derive(Clone)]
pub enum NameKey<'a> {
    /// A borrowed name.
    Borrowed(&'a Name),
    /// An owned name.
    Owned(Name),
}

impl<'a> From<NameKey<'a>> for Name {
    fn from(name: NameKey<'a>) -> Self {
        match name {
            NameKey::Borrowed(name) => name.clone(),
            NameKey::Owned(name) => name,
        }
    }
}

impl<'a> Deref for NameKey<'a> {
    type Target = Name;

    fn deref(&self) -> &Self::Target {
        match self {
            NameKey::Borrowed(name) => name,
            NameKey::Owned(name) => name,
        }
    }
}

impl<'a> Eq for NameKey<'a> {}

impl<'a, 'b> PartialEq<NameKey<'b>> for NameKey<'a> {
    fn eq(&self, other: &NameKey<'b>) -> bool {
        **self == **other
    }
}

impl<'a> Ord for NameKey<'a> {
    fn cmp(&self, other: &Self) -> Ordering {
        // Prioritize comparing the name, as in general the component names
        // shouldn't conflict.
        let a = &**self;
        let b = &**other;
        match a.name.as_ptr().cmp(&b.name.as_ptr()) {
            order @ (Ordering::Greater | Ordering::Less) => order,
            Ordering::Equal => a.authority.as_ptr().cmp(&b.authority.as_ptr()),
        }
    }
}

impl<'a, 'b> PartialOrd<NameKey<'b>> for NameKey<'a> {
    fn partial_cmp(&self, other: &NameKey<'b>) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl<'a> From<&'a Name> for NameKey<'a> {
    fn from(value: &'a Name) -> Self {
        Self::Borrowed(value)
    }
}

impl From<Name> for NameKey<'_> {
    fn from(value: Name) -> Self {
        Self::Owned(value)
    }
}

impl Debug for NameKey<'_> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        Debug::fmt(&**self, f)
    }
}

impl Display for NameKey<'_> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        Display::fmt(&**self, f)
    }
}