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
use super::{Pending, UserIdentification};
use crate::{
    model::{GameMode, Score},
    routing::Route,
    Osu,
};

#[cfg(feature = "cache")]
use crate::client::cached::OsuCached;

/// Retrieve the top scores of a [`User`](crate::model::User).
pub struct GetUserBest<'a> {
    fut: Option<Pending<'a>>,
    osu: &'a Osu,

    limit: Option<u32>,
    mode: Option<GameMode>,
    user: Option<UserIdentification>,
}

/// Retrieve the most recent scores of a [`User`](crate::model::User).
pub struct GetUserRecent<'a> {
    fut: Option<Pending<'a>>,
    osu: &'a Osu,

    limit: Option<u32>,
    mode: Option<GameMode>,
    user: Option<UserIdentification>,
}

macro_rules! impl_user_score {
    ($name: ident, $limit: literal, $metric: ident) => {
        impl<'a> $name<'a> {
            pub(crate) fn new(osu: &'a Osu, user: impl Into<UserIdentification>) -> Self {
                Self {
                    fut: None,
                    osu,
                    limit: None,
                    mode: None,
                    user: Some(user.into()),
                }
            }

            /// Optional, amount of results, defaults to 10.
            ///
            /// # Upper limit
            ///
            ///   - `GetUserBest`: 100
            ///   - `GetUserRecent`: 50
            pub fn limit(mut self, limit: u32) -> Self {
                self.limit.replace(limit.max(0).min($limit));

                self
            }

            /// Optional, defaults to `GameMode::STD`.
            pub fn mode(mut self, mode: GameMode) -> Self {
                self.mode.replace(mode);

                self
            }

            fn start(&mut self) {
                let route = Route::$name {
                    limit: self.limit.take(),
                    mode: self.mode.take(),
                    user: self.user.take().unwrap(),
                };

                #[cfg(feature = "metrics")]
                self.osu.0.metrics.$metric.inc();

                #[cfg(feature = "cache")]
                self.fut
                    .replace(Box::pin(self.osu.request_bytes(route, OsuCached::Score)));

                #[cfg(not(feature = "cache"))]
                self.fut.replace(Box::pin(self.osu.request_bytes(route)));
            }
        }
    };
}

impl_user_score!(GetUserBest, 100, top_scores);
poll_vec_req!(GetUserBest<'_>, Score);

impl_user_score!(GetUserRecent, 50, recent_scores);
poll_vec_req!(GetUserRecent<'_>, Score);