rosu-v2 0.11.0

An osu! API v2 wrapper
Documentation
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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
use std::fmt;

use itoa::Buffer;
use serde::Serialize;
use smallstr::SmallString;

use crate::{
    model::{
        beatmap::{BeatmapsetExtended, MostPlayedMap},
        event::Event,
        kudosu::KudosuHistory,
        score::Score,
        user::{User, UserBeatmapsetsKind, UserExtended, Username},
        DeserializedList, GameMode,
    },
    request::{
        serialize::{maybe_bool_as_u8, maybe_mode_as_str, user_id_type},
        Query, Request,
    },
    routing::Route,
    Osu,
};

/// Either a user id as `u32` or a username as [`Username`].
///
/// Use the `From` implementations to create this enum.
///
/// # Example
///
/// ```
/// use rosu_v2::request::UserId;
///
/// let user_id: UserId = 123_456.into();
/// let user_id: UserId = "my username".into();
/// ```
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub enum UserId {
    /// Represents a user through their user id
    Id(u32),
    /// Represents a user through their username
    Name(Username),
}

impl From<u32> for UserId {
    #[inline]
    fn from(id: u32) -> Self {
        Self::Id(id)
    }
}

impl From<&str> for UserId {
    #[inline]
    fn from(name: &str) -> Self {
        Self::Name(SmallString::from_str(name))
    }
}

impl From<&String> for UserId {
    #[inline]
    fn from(name: &String) -> Self {
        Self::Name(SmallString::from_str(name))
    }
}

impl From<String> for UserId {
    #[inline]
    fn from(name: String) -> Self {
        Self::Name(SmallString::from_string(name))
    }
}

impl fmt::Display for UserId {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Id(id) => write!(f, "{id}"),
            Self::Name(name) => f.write_str(name),
        }
    }
}

/// Get the [`UserExtended`] of the authenticated user.
///
/// Note that the client has to be initialized with the `Identify` scope
/// through the OAuth process in order for this endpoint to not return an error.
///
/// See [`OsuBuilder::with_authorization`](crate::OsuBuilder::with_authorization).
#[must_use = "requests must be configured and executed"]
pub struct GetOwnData<'a> {
    osu: &'a Osu,
    mode: Option<GameMode>,
}

impl<'a> GetOwnData<'a> {
    pub(crate) const fn new(osu: &'a Osu) -> Self {
        Self { osu, mode: None }
    }

    /// Specify the mode for which the user data should be retrieved
    #[inline]
    pub const fn mode(mut self, mode: GameMode) -> Self {
        self.mode = Some(mode);

        self
    }
}

into_future! {
    |self: GetOwnData<'_>| -> UserExtended {
        Request::new(Route::GetOwnData { mode: self.mode })
    }
}

/// Get all friends of the authenticated user as a vec of [`User`].
///
/// Note that the client has to be initialized with the `FriendsRead` scope
/// through the OAuth process in order for this endpoint to not return an error.
///
/// See [`OsuBuilder::with_authorization`](crate::OsuBuilder::with_authorization).
#[must_use = "requests must be configured and executed"]
pub struct GetFriends<'a> {
    osu: &'a Osu,
}

impl<'a> GetFriends<'a> {
    pub(crate) const fn new(osu: &'a Osu) -> Self {
        Self { osu }
    }
}

into_future! {
    |self: GetFriends<'_>| -> Vec<User> {
        Request::new(Route::GetFriends)
    }
}

/// Get a [`UserExtended`].
#[must_use = "requests must be configured and executed"]
pub struct GetUser<'a> {
    osu: &'a Osu,
    user_id: UserId,
    mode: Option<GameMode>,
}

impl<'a> GetUser<'a> {
    pub(crate) const fn new(osu: &'a Osu, user_id: UserId) -> Self {
        Self {
            osu,
            user_id,
            mode: None,
        }
    }

    /// Specify the mode for which the user data should be retrieved
    #[inline]
    pub const fn mode(mut self, mode: GameMode) -> Self {
        self.mode = Some(mode);

        self
    }

    /// Auxiliary function so that [`GetUser`]'s future can be created without
    /// an actual [`GetUser`] instance.
    ///
    /// Used for username caching.
    pub(crate) fn create_request(user_id: UserId, mode: Option<GameMode>) -> Request {
        #[derive(Serialize)]
        pub struct UserQuery {
            #[serde(rename(serialize = "key"), serialize_with = "user_id_type")]
            user_id: UserId,
        }

        let user_query = UserQuery { user_id };
        let query = Query::encode(&user_query);
        let user_id = user_query.user_id;

        let route = Route::GetUser { user_id, mode };

        Request::with_query(route, query)
    }
}

into_future! {
    |self: GetUser<'_>| -> UserExtended {
        Self::create_request(self.user_id, self.mode)
    }
}

/// Get the [`BeatmapsetExtended`]s of a user.
#[must_use = "requests must be configured and executed"]
#[derive(Serialize)]
pub struct GetUserBeatmapsets<'a> {
    #[serde(skip)]
    osu: &'a Osu,
    #[serde(skip)]
    map_kind: UserBeatmapsetsKind,
    limit: Option<usize>,
    offset: Option<usize>,
    #[serde(skip)]
    user_id: UserId,
}

impl<'a> GetUserBeatmapsets<'a> {
    pub(crate) const fn new(osu: &'a Osu, user_id: UserId, kind: UserBeatmapsetsKind) -> Self {
        Self {
            osu,
            user_id,
            map_kind: kind,
            limit: None,
            offset: None,
        }
    }

    /// Limit the amount of results in the response
    #[inline]
    pub const fn limit(mut self, limit: usize) -> Self {
        self.limit = Some(limit);

        self
    }

    /// Set an offset for the requested elements
    /// e.g. skip the first `offset` amount in the list
    #[inline]
    pub const fn offset(mut self, offset: usize) -> Self {
        self.offset = Some(offset);

        self
    }

    /// Only include mapsets of the specified type
    #[inline]
    pub const fn kind(mut self, kind: UserBeatmapsetsKind) -> Self {
        self.map_kind = kind;

        self
    }
}

into_future! {
    |self: GetUserBeatmapsets<'_>| -> Vec<BeatmapsetExtended> {
        GetUserBeatmapsetsData {
            map_kind: UserBeatmapsetsKind = self.map_kind,
            query: String = Query::encode(&self),
        }
    } => |user_id, data| {
        Request::with_query(
            Route::GetUserBeatmapsets {
                user_id,
                map_type: data.map_kind.as_str(),
            },
            data.query,
        )
    }
}

/// Get a user's kudosu history as a vec of [`KudosuHistory`].
#[must_use = "requests must be configured and executed"]
#[derive(Serialize)]
pub struct GetUserKudosu<'a> {
    #[serde(skip)]
    osu: &'a Osu,
    limit: Option<usize>,
    offset: Option<usize>,
    #[serde(skip)]
    user_id: UserId,
}

impl<'a> GetUserKudosu<'a> {
    pub(crate) const fn new(osu: &'a Osu, user_id: UserId) -> Self {
        Self {
            osu,
            user_id,
            limit: None,
            offset: None,
        }
    }

    /// Limit the amount of results in the response
    #[inline]
    pub const fn limit(mut self, limit: usize) -> Self {
        self.limit = Some(limit);

        self
    }

    /// Set an offset for the requested elements
    /// e.g. skip the first `offset` amount in the list
    #[inline]
    pub const fn offset(mut self, offset: usize) -> Self {
        self.offset = Some(offset);

        self
    }
}

into_future! {
    |self: GetUserKudosu<'_>| -> Vec<KudosuHistory> {
        GetUserKudosuData {
            query: String = Query::encode(&self),
        }
    } => |user_id, data| {
        Request::with_query(Route::GetUserKudosu { user_id }, data.query)
    }
}

/// Get the most played beatmaps of a user as a vec of [`MostPlayedMap`].
#[must_use = "requests must be configured and executed"]
#[derive(Serialize)]
pub struct GetUserMostPlayed<'a> {
    #[serde(skip)]
    osu: &'a Osu,
    limit: Option<usize>,
    offset: Option<usize>,
    #[serde(skip)]
    user_id: UserId,
}

impl<'a> GetUserMostPlayed<'a> {
    pub(crate) const fn new(osu: &'a Osu, user_id: UserId) -> Self {
        Self {
            osu,
            user_id,
            limit: None,
            offset: None,
        }
    }

    /// The API provides at most 51 results per requests.
    #[inline]
    pub const fn limit(mut self, limit: usize) -> Self {
        self.limit = Some(limit);

        self
    }

    /// Set an offset for the requested elements
    /// e.g. skip the first `offset` amount in the list
    #[inline]
    pub const fn offset(mut self, offset: usize) -> Self {
        self.offset = Some(offset);

        self
    }
}

into_future! {
    |self: GetUserMostPlayed<'_>| -> Vec<MostPlayedMap> {
        GetUserMostPlayedData {
            query: String = Query::encode(&self),
        }
    } => |user_id, data| {
        let route = Route::GetUserBeatmapsets {
            user_id,
            map_type: "most_played",
        };

        Request::with_query(route, data.query)
    }
}

/// Get a vec of [`Event`] of a user.
#[must_use = "requests must be configured and executed"]
#[derive(Serialize)]
pub struct GetRecentActivity<'a> {
    #[serde(skip)]
    osu: &'a Osu,
    limit: Option<usize>,
    offset: Option<usize>,
    #[serde(skip)]
    user_id: UserId,
}

impl<'a> GetRecentActivity<'a> {
    pub(crate) const fn new(osu: &'a Osu, user_id: UserId) -> Self {
        Self {
            osu,
            user_id,
            limit: None,
            offset: None,
        }
    }

    /// Limit the amount of results in the response
    #[inline]
    pub const fn limit(mut self, limit: usize) -> Self {
        self.limit = Some(limit);

        self
    }

    /// Set an offset for the requested elements
    /// e.g. skip the first `offset` amount in the list
    #[inline]
    pub const fn offset(mut self, offset: usize) -> Self {
        self.offset = Some(offset);

        self
    }
}

into_future! {
    |self: GetRecentActivity<'_>| -> Vec<Event> {
        GetRecentActivityData {
            query: String = Query::encode(&self),
        }
    } => |user_id, data| {
        Request::with_query(Route::GetRecentActivity { user_id }, data.query)
    }
}

#[derive(Copy, Clone, Debug)]
pub(crate) enum ScoreType {
    Best,
    First,
    Pinned,
    Recent,
}

impl ScoreType {
    pub(crate) const fn as_str(self) -> &'static str {
        match self {
            Self::Best => "best",
            Self::First => "firsts",
            Self::Pinned => "pinned",
            Self::Recent => "recent",
        }
    }
}

/// Get a vec of [`Score`]s of a user.
///
/// If no score type is specified by either
/// [`best`](crate::request::GetUserScores::best),
/// [`firsts`](crate::request::GetUserScores::firsts),
/// or [`recent`](crate::request::GetUserScores::recent), it defaults to `best`.
#[must_use = "requests must be configured and executed"]
#[derive(Serialize)]
pub struct GetUserScores<'a> {
    #[serde(skip)]
    osu: &'a Osu,
    #[serde(skip)]
    score_type: ScoreType,
    limit: Option<usize>,
    offset: Option<usize>,
    #[serde(serialize_with = "maybe_bool_as_u8")]
    include_fails: Option<bool>,
    #[serde(serialize_with = "maybe_mode_as_str")]
    mode: Option<GameMode>,
    legacy_only: bool,
    #[serde(skip)]
    legacy_scores: bool,
    #[serde(skip)]
    user_id: UserId,
}

impl<'a> GetUserScores<'a> {
    pub(crate) const fn new(osu: &'a Osu, user_id: UserId) -> Self {
        Self {
            osu,
            user_id,
            score_type: ScoreType::Best,
            limit: None,
            offset: None,
            include_fails: None,
            mode: None,
            legacy_only: false,
            legacy_scores: false,
        }
    }

    /// The API provides at most 100 results per requests.
    #[inline]
    pub const fn limit(mut self, limit: usize) -> Self {
        self.limit = Some(limit);

        self
    }

    /// Set an offset for the requested elements
    /// e.g. skip the first `offset` amount in the list
    #[inline]
    pub const fn offset(mut self, offset: usize) -> Self {
        self.offset = Some(offset);

        self
    }

    /// Specify the mode of the scores
    #[inline]
    pub const fn mode(mut self, mode: GameMode) -> Self {
        self.mode = Some(mode);

        self
    }

    /// Specify whether failed scores can be included.
    ///
    /// Only relevant for [`recent`](GetUserScores::recent)
    #[inline]
    pub const fn include_fails(mut self, include_fails: bool) -> Self {
        self.include_fails = Some(include_fails);

        self
    }

    /// Get top scores of a user
    #[inline]
    pub const fn best(mut self) -> Self {
        self.score_type = ScoreType::Best;

        self
    }

    /// Get global #1 scores of a user.
    #[inline]
    pub const fn firsts(mut self) -> Self {
        self.score_type = ScoreType::First;

        self
    }

    /// Get the pinned scores of a user.
    #[inline]
    pub const fn pinned(mut self) -> Self {
        self.score_type = ScoreType::Pinned;

        self
    }

    /// Get recent scores of a user.
    #[inline]
    pub const fn recent(mut self) -> Self {
        self.score_type = ScoreType::Recent;

        self
    }

    /// Whether or not to exclude lazer scores.
    #[inline]
    pub const fn legacy_only(mut self, legacy_only: bool) -> Self {
        self.legacy_only = legacy_only;

        self
    }

    /// Specify whether the scores should contain legacy data or not.
    ///
    /// Legacy data consists of a different grade calculation, less
    /// populated statistics, legacy mods, and a different score kind.
    #[inline]
    pub const fn legacy_scores(mut self, legacy_scores: bool) -> Self {
        self.legacy_scores = legacy_scores;

        self
    }
}

into_future! {
    |self: GetUserScores<'_>| -> Vec<Score> {
        GetUserScoresData {
            query: String = Query::encode(&self),
            score_type: ScoreType = self.score_type,
            legacy_scores: bool = self.legacy_scores,
        }
    } => |user_id, data| {
        let route = Route::GetUserScores {
            user_id,
            score_type: data.score_type,
        };

        let mut req = Request::with_query(route, data.query);

        if data.legacy_scores {
            req.api_version(0);
        }

        req
    }
}

/// Get a vec of [`User`].
#[must_use = "requests must be configured and executed"]
pub struct GetUsers<'a> {
    osu: &'a Osu,
    query: String,
}

impl<'a> GetUsers<'a> {
    pub(crate) fn new<I>(osu: &'a Osu, user_ids: I) -> Self
    where
        I: IntoIterator<Item = u32>,
    {
        let mut query = String::new();
        let mut buf = Buffer::new();

        let mut iter = user_ids.into_iter().take(50);

        if let Some(user_id) = iter.next() {
            query.push_str("ids[]=");
            query.push_str(buf.format(user_id));

            for user_id in iter {
                query.push_str("&ids[]=");
                query.push_str(buf.format(user_id));
            }
        }

        Self { osu, query }
    }
}

into_future! {
    |self: GetUsers<'_>| -> DeserializedList<User> {
        Request::with_query(Route::GetUsers, self.query)
    } => |users, _| -> Vec<User> {
        Ok(users.0)
    }
}