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
//! This library provides functionality for converting usernames to and from Minecraft UUIDs,
//! including support for offline and online players.  
//! You may choose to disable either the `offline` or `online` features if you don't need them.
//!
//! To start, head over to [`PlayerUuid`] or look at some of the examples in this crate.

use serde::{Deserialize, Serialize};
use thiserror::Error;
use uuid::Version;
pub use uuid::{self, Uuid};

/// This library's own error enum, which is returned by every function that returns a [`Result`](std::result::Result).
#[derive(Debug, Error)]
pub enum Error {
    /// An error that signifies that the user has provided an invalid UUID, be it in the wrong format or a non-existent UUID if in an online context.
    #[error("invalid uuid")]
    InvalidUuid,

    /// An error that signifies that the user has provided an invalid username to Mojang's API.
    #[error("invalid username")]
    InvalidUsername,

    /// A Transport error from [`ureq`].
    #[cfg(feature = "online")]
    #[error("ureq transport error: {0}")]
    Transport(ureq::Transport),

    /// An error that signifies that the Mojang API returned an unexpected result.
    #[error("mojang api returned unexpected result")]
    MojangAPIError,
}

type Result<T> = std::result::Result<T, Error>;

/// A struct that represents a UUID with an online format (UUID v4).
#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq, Ord, PartialOrd, Serialize, Deserialize)]
#[serde(try_from = "Uuid")]
#[serde(into = "Uuid")]
pub struct OnlineUuid(Uuid);

/// A struct that represents a UUID with an offline format (UUID v3).
#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq, Ord, PartialOrd, Serialize, Deserialize)]
#[serde(try_from = "Uuid")]
#[serde(into = "Uuid")]
pub struct OfflineUuid(Uuid);

/// An enum that can represent both kinds of UUIDs.
#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq, Ord, PartialOrd, Serialize, Deserialize)]
#[serde(try_from = "Uuid")]
#[serde(into = "Uuid")]
pub enum PlayerUuid {
    Online(OnlineUuid),
    Offline(OfflineUuid),
}

#[cfg(feature = "online")]
#[derive(Deserialize)]
struct OnlineUuidResponse {
    name: String,
    id: PlayerUuid,
}

impl OnlineUuid {
    /// Uses the Mojang API to fetch the username belonging to this UUID.
    ///
    /// # Errors
    /// If there is no user that corresponds to the provided UUID, an [`Error::InvalidUuid`] is returned.  
    /// Otherwise, an [`Error::Transport`] can be returned in case of network failure.
    ///
    /// # Examples
    /// To fetch the user belonging to an arbitrary UUID, you can do:
    /// ```rust
    /// use uuid::Uuid;
    /// use uuid_mc::{PlayerUuid, OnlineUuid};
    ///
    /// # fn main() -> std::result::Result<(), Box<dyn std::error::Error>> {
    /// let uuid = Uuid::try_parse("069a79f4-44e9-4726-a5be-fca90e38aaf5")?;
    /// let player_uuid = PlayerUuid::new_with_uuid(uuid)?;
    ///
    /// let name = player_uuid.unwrap_online().get_username()?;
    /// assert_eq!(name, "Notch");
    /// # Ok(())
    /// # }
    /// ```
    #[cfg(feature = "online")]
    pub fn get_username(&self) -> Result<String> {
        let response = ureq::get(&format!(
            "https://sessionserver.mojang.com/session/minecraft/profile/{}",
            self.0
        ))
        .call();

        match response {
            Ok(data) => {
                let response: OnlineUuidResponse =
                    data.into_json().map_err(|_| Error::MojangAPIError)?;
                Ok(response.name)
            }
            Err(ureq::Error::Status(_, _)) => Err(Error::InvalidUsername),
            Err(ureq::Error::Transport(x)) => Err(Error::Transport(x)),
        }
    }

    /// Returns the inner [Uuid].
    pub fn as_uuid(&self) -> &Uuid {
        &self.0
    }

    /// Returns the inner UUID, as a byte array. This is just a convenience function around `self.as_uuid().as_bytes()`.
    pub fn as_bytes(&self) -> &[u8; 16] {
        self.as_uuid().as_bytes()
    }
}

impl OfflineUuid {
    /// Returns the inner [Uuid].
    pub fn as_uuid(&self) -> &Uuid {
        &self.0
    }

    /// Returns the inner UUID, as a byte array. This is just a convenience function around `self.as_uuid().as_bytes()`.
    pub fn as_bytes(&self) -> &[u8; 16] {
        self.as_uuid().as_bytes()
    }
}

impl PlayerUuid {
    /// Creates a new instance using the username of an online player, by polling the Mojang API.
    ///
    /// # Errors
    /// If there is no user that corresponds to the provided username, an [`Error::InvalidUsername`] is returned.  
    /// Otherwise, an [`Error::Transport`] can be returned in case of network failure.
    ///
    /// # Examples
    /// To fetch the UUID of an online user:
    /// ```rust
    /// use uuid::Uuid;
    /// use uuid_mc::PlayerUuid;
    ///
    /// # fn main() -> std::result::Result<(), Box<dyn std::error::Error>> {
    /// let uuid = PlayerUuid::new_with_online_username("Notch")?;
    /// let uuid = uuid.as_uuid();
    /// let expected = Uuid::try_parse("069a79f4-44e9-4726-a5be-fca90e38aaf5")?;
    /// assert_eq!(uuid, &expected);
    /// # Ok(())
    /// # }
    #[cfg(feature = "online")]
    pub fn new_with_online_username(username: &str) -> Result<Self> {
        let response = ureq::get(&format!(
            "https://api.mojang.com/users/profiles/minecraft/{}",
            username
        ))
        .call();

        match response {
            Ok(data) => {
                let response: OnlineUuidResponse =
                    data.into_json().map_err(|_| Error::MojangAPIError)?;
                Ok(response.id)
            }
            Err(ureq::Error::Status(_, _)) => Err(Error::InvalidUsername),
            Err(ureq::Error::Transport(x)) => Err(Error::Transport(x)),
        }
    }

    /// Creates a new instance using the username of an offline player.
    ///
    /// # Examples
    /// To fetch the UUID of an offline user:
    /// ```rust
    /// use uuid::Uuid;
    /// use uuid_mc::PlayerUuid;
    ///
    /// # fn main() -> std::result::Result<(), Box<dyn std::error::Error>> {
    /// let uuid = PlayerUuid::new_with_offline_username("boolean_coercion");
    /// let uuid = uuid.as_uuid();
    /// let expected = Uuid::try_parse("db62bdfb-eddc-3acc-a14e-c703aba52549")?;
    /// assert_eq!(uuid, &expected);
    /// # Ok(())
    /// # }
    #[cfg(feature = "offline")]
    pub fn new_with_offline_username(username: &str) -> Self {
        let mut hash = md5::compute(format!("OfflinePlayer:{}", username)).0;
        hash[6] = hash[6] & 0x0f | 0x30; // uuid version 3
        hash[8] = hash[8] & 0x3f | 0x80; // RFC4122 variant

        let uuid = Uuid::from_bytes(hash);
        Self::Offline(OfflineUuid(uuid))
    }

    /// Creates a new instance using an already existing [`Uuid`].
    ///
    /// # Errors
    /// In case the provided Uuid is neither offline (v3) or online (v4), an [`Error::InvalidUuid`] is returned.
    ///
    /// # Examples
    /// To test whether a given Uuid is of the offline or online format:
    /// ```rust
    /// use uuid::Uuid;
    /// use uuid_mc::PlayerUuid;
    ///
    /// # fn main() -> std::result::Result<(), Box<dyn std::error::Error>> {
    /// let uuid_offline = Uuid::try_parse("db62bdfb-eddc-3acc-a14e-c703aba52549")?;
    /// let uuid_online = Uuid::try_parse("61699b2e-d327-4a01-9f1e-0ea8c3f06bc6")?;
    /// let player_uuid_offline = PlayerUuid::new_with_uuid(uuid_offline)?;
    /// let player_uuid_online = PlayerUuid::new_with_uuid(uuid_online)?;
    ///
    /// assert!(matches!(player_uuid_offline, PlayerUuid::Offline(_)));
    /// assert!(matches!(player_uuid_online, PlayerUuid::Online(_)));
    /// # Ok(())
    /// # }
    pub fn new_with_uuid(uuid: Uuid) -> Result<Self> {
        match uuid.get_version() {
            Some(Version::Random) => Ok(Self::Online(OnlineUuid(uuid))),
            Some(Version::Md5) => Ok(Self::Offline(OfflineUuid(uuid))),
            _ => Err(Error::InvalidUuid),
        }
    }

    /// Returns the inner [`Uuid`].
    pub fn as_uuid(&self) -> &Uuid {
        match self {
            Self::Online(uuid) => uuid.as_uuid(),
            Self::Offline(uuid) => uuid.as_uuid(),
        }
    }

    /// Returns the inner UUID, as a byte array. This is just a convenience function around `self.as_uuid().as_bytes()`.
    pub fn as_bytes(&self) -> &[u8; 16] {
        self.as_uuid().as_bytes()
    }

    /// Similar to [`Result::unwrap`](std::result::Result::unwrap), this function returns the inner [`OfflineUuid`].
    ///
    /// # Panics
    /// If the inner UUID is not an offline one.
    pub fn unwrap_offline(self) -> OfflineUuid {
        match self {
            Self::Online(_) => panic!("unwrap_offline called on an online uuid"),
            Self::Offline(uuid) => uuid,
        }
    }

    /// Similar to [`Result::unwrap`](std::result::Result::unwrap), this function returns the inner [`OnlineUuid`].
    ///
    /// # Panics
    /// If the inner UUID is not an online one.
    pub fn unwrap_online(self) -> OnlineUuid {
        match self {
            Self::Online(uuid) => uuid,
            Self::Offline(_) => panic!("unwrap_online called on an offline uuid"),
        }
    }

    /// Returns the contained [`OfflineUuid`] if it is present, or [`None`] otherwise.
    pub fn offline(self) -> Option<OfflineUuid> {
        match self {
            Self::Online(_) => None,
            Self::Offline(uuid) => Some(uuid),
        }
    }

    /// Returns the contained [`OnlineUuid`] if it is present, or [`None`] otherwise.
    pub fn online(self) -> Option<OnlineUuid> {
        match self {
            Self::Online(uuid) => Some(uuid),
            Self::Offline(_) => None,
        }
    }
}

impl TryFrom<Uuid> for PlayerUuid {
    type Error = Error;

    fn try_from(value: Uuid) -> std::result::Result<Self, Self::Error> {
        Self::new_with_uuid(value)
    }
}

impl TryFrom<Uuid> for OnlineUuid {
    type Error = Error;

    fn try_from(value: Uuid) -> std::result::Result<Self, Self::Error> {
        PlayerUuid::new_with_uuid(value)?
            .online()
            .ok_or(Error::InvalidUuid)
    }
}

impl TryFrom<Uuid> for OfflineUuid {
    type Error = Error;

    fn try_from(value: Uuid) -> std::result::Result<Self, Self::Error> {
        PlayerUuid::new_with_uuid(value)?
            .offline()
            .ok_or(Error::InvalidUuid)
    }
}

impl From<PlayerUuid> for Uuid {
    fn from(other: PlayerUuid) -> Self {
        *other.as_uuid()
    }
}

impl From<OnlineUuid> for Uuid {
    fn from(other: OnlineUuid) -> Self {
        *other.as_uuid()
    }
}

impl From<OfflineUuid> for Uuid {
    fn from(other: OfflineUuid) -> Self {
        *other.as_uuid()
    }
}

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

    #[cfg(feature = "offline")]
    #[test]
    fn offline_uuids() {
        let values = vec![
            ("boolean_coercion", "db62bdfb-eddc-3acc-a14e-c703aba52549"),
            ("BooleanCoercion", "44d050b1-46c8-37a8-b511-7023ae304192"),
            ("bool", "e9fd750e-29c2-3d85-80c9-64618059d454"),
            ("BOOL", "c5d06acf-0ef6-3a68-bf0b-b57806bcbef5"),
            ("BoOl", "e38f2cf4-72d2-3a84-8278-fed6908d2746"),
            ("booleancoercion", "072a2e03-56ce-3960-9391-c56afe17e317"),
        ];

        values
            .into_iter()
            .map(|(username, uuid)| {
                (
                    *PlayerUuid::new_with_offline_username(username).as_uuid(),
                    Uuid::try_parse(uuid).unwrap(),
                )
            })
            .for_each(|(uuid1, uuid2)| assert_eq!(uuid1, uuid2));
    }

    #[cfg(feature = "online")]
    #[test]
    fn online_uuids() {
        let values = vec![
            ("Notch", "069a79f4-44e9-4726-a5be-fca90e38aaf5"),
            ("dinnerbone", "61699b2e-d327-4a01-9f1e-0ea8c3f06bc6"),
            ("Dinnerbone", "61699b2e-d327-4a01-9f1e-0ea8c3f06bc6"),
        ];

        values
            .into_iter()
            .map(|(username, uuid)| {
                (
                    *PlayerUuid::new_with_online_username(username)
                        .unwrap()
                        .as_uuid(),
                    Uuid::try_parse(uuid).unwrap(),
                )
            })
            .for_each(|(uuid1, uuid2)| assert_eq!(uuid1, uuid2));
    }

    #[cfg(feature = "online")]
    #[test]
    fn online_uuids_to_names() {
        let values = vec![
            ("Notch", "069a79f4-44e9-4726-a5be-fca90e38aaf5"),
            ("Dinnerbone", "61699b2e-d327-4a01-9f1e-0ea8c3f06bc6"),
        ];

        values
            .into_iter()
            .map(|(username, uuid)| {
                (
                    username,
                    PlayerUuid::new_with_uuid(Uuid::try_parse(uuid).unwrap())
                        .unwrap()
                        .unwrap_online()
                        .get_username()
                        .unwrap(),
                )
            })
            .for_each(|(name1, name2)| assert_eq!(name1, name2));
    }
}