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
#![doc = include_str!("../README.md")]

use std::borrow::{Borrow, Cow};
use std::cmp::Ordering;
use std::fmt;
use std::fmt::Formatter;
use std::str::FromStr;

use serde::de::Error as _;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use thiserror::Error;
/// Used internally by the `ident` macro. Not public API.
#[doc(hidden)]
pub use valence_ident_macros::parse_ident_str;

/// Creates a new [`Ident`] at compile time from a string literal. A compile
/// error is raised if the string is not a valid resource identifier.
///
/// The type of the expression returned by this macro is `Ident<&'static str>`.
/// The expression is usable in a `const` context.
///
/// # Examples
///
/// ```
/// # use valence_ident::{ident, Ident};
/// let my_ident: Ident<&'static str> = ident!("apple");
///
/// println!("{my_ident}");
/// ```
#[macro_export]
macro_rules! ident {
    ($string:literal) => {
        $crate::Ident::<&'static str>::new_unchecked($crate::parse_ident_str!($string))
    };
}

/// A wrapper around a string type `S` which guarantees the wrapped string is a
/// valid resource identifier.
///
/// A resource identifier is a string divided into a "namespace" part and a
/// "path" part. For instance `minecraft:apple` and `valence:frobnicator` are
/// both valid identifiers. A string must match the regex
/// `^([a-z0-9_.-]+:)?[a-z0-9_.-\/]+$` to be successfully parsed.
///
/// While parsing, if the namespace part is left off (the part before and
/// including the colon) then "minecraft:" is inserted at the beginning of the
/// string.
#[derive(Copy, Clone, Eq, Ord, Hash)]
pub struct Ident<S> {
    string: S,
}

/// The error type created when an [`Ident`] cannot be parsed from a
/// string. Contains the string that failed to parse.
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Error)]
#[error("invalid resource identifier \"{0}\"")]
pub struct IdentError(pub String);

impl<'a> Ident<Cow<'a, str>> {
    pub fn new(string: impl Into<Cow<'a, str>>) -> Result<Self, IdentError> {
        parse(string.into())
    }
}

impl<S> Ident<S> {
    /// Used internally by the `ident` macro. Not public API.
    #[doc(hidden)]
    pub const fn new_unchecked(string: S) -> Self {
        Self { string }
    }

    pub fn as_str(&self) -> &str
    where
        S: AsRef<str>,
    {
        self.string.as_ref()
    }

    pub fn as_str_ident(&self) -> Ident<&str>
    where
        S: AsRef<str>,
    {
        Ident {
            string: self.as_str(),
        }
    }

    pub fn to_string_ident(&self) -> Ident<String>
    where
        S: AsRef<str>,
    {
        Ident {
            string: self.as_str().to_owned(),
        }
    }

    pub fn into_inner(self) -> S {
        self.string
    }

    /// Returns the namespace part of this resource identifier (the part before
    /// the colon).
    pub fn namespace(&self) -> &str
    where
        S: AsRef<str>,
    {
        self.namespace_and_path().0
    }

    /// Returns the path part of this resource identifier (the part after the
    /// colon).
    pub fn path(&self) -> &str
    where
        S: AsRef<str>,
    {
        self.namespace_and_path().1
    }

    pub fn namespace_and_path(&self) -> (&str, &str)
    where
        S: AsRef<str>,
    {
        self.as_str()
            .split_once(':')
            .expect("invalid resource identifier")
    }
}

impl<'a> Ident<Cow<'a, str>> {
    pub fn borrowed(&self) -> Ident<Cow<str>> {
        Ident::new_unchecked(Cow::Borrowed(self.as_str()))
    }
}

fn parse(string: Cow<str>) -> Result<Ident<Cow<str>>, IdentError> {
    let check_namespace = |s: &str| {
        !s.is_empty()
            && s.chars()
                .all(|c| matches!(c, 'a'..='z' | '0'..='9' | '_' | '.' | '-'))
    };

    let check_path = |s: &str| {
        !s.is_empty()
            && s.chars()
                .all(|c| matches!(c, 'a'..='z' | '0'..='9' | '_' | '.' | '-' | '/'))
    };

    match string.split_once(':') {
        Some((namespace, path)) if check_namespace(namespace) && check_path(path) => {
            Ok(Ident { string })
        }
        None if check_path(&string) => Ok(Ident {
            string: format!("minecraft:{string}").into(),
        }),
        _ => Err(IdentError(string.into())),
    }
}

impl<S: AsRef<str>> AsRef<str> for Ident<S> {
    fn as_ref(&self) -> &str {
        self.string.as_ref()
    }
}

impl<S> AsRef<S> for Ident<S> {
    fn as_ref(&self) -> &S {
        &self.string
    }
}

impl<S: Borrow<str>> Borrow<str> for Ident<S> {
    fn borrow(&self) -> &str {
        self.string.borrow()
    }
}

impl From<Ident<&str>> for String {
    fn from(value: Ident<&str>) -> Self {
        value.as_str().to_owned()
    }
}

impl From<Ident<String>> for String {
    fn from(value: Ident<String>) -> Self {
        value.into_inner()
    }
}

impl<'a> From<Ident<Cow<'a, str>>> for Cow<'a, str> {
    fn from(value: Ident<Cow<'a, str>>) -> Self {
        value.into_inner()
    }
}

impl<'a> From<Ident<Cow<'a, str>>> for Ident<String> {
    fn from(value: Ident<Cow<'a, str>>) -> Self {
        Self {
            string: value.string.into(),
        }
    }
}

impl<'a> From<Ident<String>> for Ident<Cow<'a, str>> {
    fn from(value: Ident<String>) -> Self {
        Self {
            string: value.string.into(),
        }
    }
}

impl<'a> From<Ident<&'a str>> for Ident<Cow<'a, str>> {
    fn from(value: Ident<&'a str>) -> Self {
        Ident {
            string: value.string.into(),
        }
    }
}

impl<'a> From<Ident<&'a str>> for Ident<String> {
    fn from(value: Ident<&'a str>) -> Self {
        Ident {
            string: value.string.into(),
        }
    }
}

impl FromStr for Ident<String> {
    type Err = IdentError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Ok(Ident::new(s)?.into())
    }
}

impl FromStr for Ident<Cow<'static, str>> {
    type Err = IdentError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Ident::<String>::try_from(s).map(From::from)
    }
}

impl<'a> TryFrom<&'a str> for Ident<String> {
    type Error = IdentError;

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

impl TryFrom<String> for Ident<String> {
    type Error = IdentError;

    fn try_from(value: String) -> Result<Self, Self::Error> {
        Ok(Ident::new(value)?.into())
    }
}

impl<'a> TryFrom<Cow<'a, str>> for Ident<String> {
    type Error = IdentError;

    fn try_from(value: Cow<'a, str>) -> Result<Self, Self::Error> {
        Ok(Ident::new(value)?.into())
    }
}

impl<'a> TryFrom<&'a str> for Ident<Cow<'a, str>> {
    type Error = IdentError;

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

impl<'a> TryFrom<String> for Ident<Cow<'a, str>> {
    type Error = IdentError;

    fn try_from(value: String) -> Result<Self, Self::Error> {
        Self::new(value)
    }
}

impl<'a> TryFrom<Cow<'a, str>> for Ident<Cow<'a, str>> {
    type Error = IdentError;

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

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

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

impl<S, T> PartialEq<Ident<T>> for Ident<S>
where
    S: PartialEq<T>,
{
    fn eq(&self, other: &Ident<T>) -> bool {
        self.string == other.string
    }
}

impl<S, T> PartialOrd<Ident<T>> for Ident<S>
where
    S: PartialOrd<T>,
{
    fn partial_cmp(&self, other: &Ident<T>) -> Option<Ordering> {
        self.string.partial_cmp(&other.string)
    }
}

impl<T: Serialize> Serialize for Ident<T> {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        self.string.serialize(serializer)
    }
}

impl<'de, S> Deserialize<'de> for Ident<S>
where
    S: Deserialize<'de>,
    Ident<S>: TryFrom<S, Error = IdentError>,
{
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        Ident::try_from(S::deserialize(deserializer)?).map_err(D::Error::custom)
    }
}

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

    #[test]
    fn check_namespace_and_path() {
        let id = ident!("namespace:path");
        assert_eq!(id.namespace(), "namespace");
        assert_eq!(id.path(), "path");
    }

    #[test]
    fn parse_valid() {
        ident!("minecraft:whatever");
        ident!("_what-ever55_:.whatever/whatever123456789_");
        ident!("valence:frobnicator");
    }

    #[test]
    #[should_panic]
    fn parse_invalid_0() {
        Ident::new("").unwrap();
    }

    #[test]
    #[should_panic]
    fn parse_invalid_1() {
        Ident::new(":").unwrap();
    }

    #[test]
    #[should_panic]
    fn parse_invalid_2() {
        Ident::new("foo:bar:baz").unwrap();
    }

    #[test]
    fn equality() {
        assert_eq!(ident!("minecraft:my.identifier"), ident!("my.identifier"));
    }
}